cocos2dx3.2开发 RPG《Flighting》(十三)让英雄炫酷点—技能以及技能效果

一、前言

怪物已经变聪明了,我们的英雄不变得厉害点怎么行呢

二、正文

其实技能这个问题纠结了我好久,因为每个角色的技能都不一样(参考英雄联盟),我当初也想把技能做得多姿多彩的。所以我就想在代码中嵌入lua,可惜小弟不才,虽然最后弄明白了怎么搞,但是发现已经太迟了(应为要把之前的自定义的类都转成lua接口)。所以采用了一种很笨很笨的方法。

将技能之前,先看SkillMessage

还记得XXXMessage是什么东东吧。对了,就是保存从excel表读取的数据的一种对象。

#ifndef _SKILL_MESSAGE_H_
#define _SKILL_MESSAGE_H_
#include <string>
using namespace std;
class SkillMessage{
public:
	int id;<span style="white-space:pre">	</span>//唯一id标识
	string name;<span style="white-space:pre">	</span>//技能的名称
	string desc;<span style="white-space:pre">	</span>//技能描述
	string icon;<span style="white-space:pre">	</span>//技能的图标
	int begin_e;<span style="white-space:pre">	</span>//技能使用时的效果
	int state_e;<span style="white-space:pre">	</span>//技能持续中的特效
	int hit_e;<span style="white-space:pre">	</span>//打中别人之后,别人身上跑的效果
	int lastTime;<span style="white-space:pre">	</span>//技能状态持续时间
	int cd;<span style="white-space:pre">	</span>//冷却时间
	string skill_bullet_img;<span style="white-space:pre">	</span>//当在技能状态时,子弹的图片
};
#endif

SkillMessage的各个属性解释已经标出来了,相应的excel表自己构造就可以吧。


上面提到了技能效果,虽然看上去很酷,但是其实实现起来很简单,这里我们的技能都是用连帧动画

EffectUtil负责获取动画

class EffectUtil{
public:
	static EffectUtil* getInstance();
	Animate* getSkillEffectById(int id,int loop=1);

private:
	static EffectUtil* m_instance;
};
主要看getSkillEffectById方法
Animate*  EffectUtil::getSkillEffectById(int id,int loop){
	SpriteFrameCache* cache = SpriteFrameCache::getInstance();
	cache->addSpriteFramesWithFile(String::createWithFormat("Skill/Effect/%d.plist",id)->getCString(),
		String::createWithFormat("Skill/Effect/%d.png",id)->getCString());
	SpriteFrame* temp;
	Vector<SpriteFrame*> v;
	int index = 1;
	do{
		CCLOG("INDEX = %d",index);
		temp = cache->getSpriteFrameByName(String::createWithFormat("%d_%d.png",id,index)->getCString());
		index++;
		if(temp == nullptr){
			break;
		}else{
			v.pushBack(temp);
		}
	}while(true);

	Animation* animation = Animation::createWithSpriteFrames(v);
	animation->setLoops(loop);
	animation->setDelayPerUnit(0.1f);
	Animate* ret = Animate::create(animation);
	return ret;
}
基本用过连帧动画的都知道上面是怎么一回事。 

这里只是简单解释一下就好了。首先把需要的连帧许梿加载到cache,然后将一个个SpriteFrame从cache中取出,并且放入容器(Vector),最后就可以用Animation::createWithSpriteFrames方法构造出一个Animation对象,Animation对象相当于动画的配置文件。最后构造出Animate动画

这样子我们就可以通过两个参数:1、动画的唯一id 2、动画是否循环播放来获得我们需要的动画(Animate)


前面做了那么铺垫,下面我们开始分步骤实现:

第一步:在场景中添加技能框

这里我单独地把左上角的技能框弄成一个Node(Skill)

class Skill : public Node{
public:
    bool init(Hero* h);
	static Skill* create(Hero* h);
	void onTouchDown(Ref* pSender,ui::TouchEventType type);
	Hero* getHero();
private:
	ui::Button* skillIcon;
	Hero* m_hero;
};

skillIcon就是技能的图标啦,技能框的创建需要英雄类,获得英雄的技能图片不是问题

Skill* Skill::create(Hero* h){
	Skill* skill = new Skill();
	if(skill && skill->init(h)){
		skill->autorelease();
		return skill;
	}else{
		CC_SAFE_DELETE(skill);
		return nullptr;
	}
}

bool Skill::init(Hero* h){
	m_hero = h;
	skillIcon = ui::Button::create("Skill/" + h->getSkill().icon);
	skillIcon->addTouchEventListener(this,toucheventselector(Skill::onTouchDown));
	this->addChild(skillIcon);
	return true;
}

还有一个重要的函数就是onTouchDown,因为我吧skillIcon定位一个ui::Button所以点击技能图标的时候会触发下面的函数

void  Skill::onTouchDown(Ref* pSender,ui::TouchEventType type){
	if(type == TouchEventType::TOUCH_EVENT_BEGAN){
	<span style="white-space:pre">	</span>m_hero->skill();
	}
}
其实也是辗转地调用英雄的skill()接口


万事俱备只欠东风

我们现在只需要在FlightLayer里面放入技能框就好了

bool FlightLayer::init(){
	this->scheduleUpdate();
	initListener();
	m_cur_controlPtr = nullptr;
	m_cur_control = nullptr;
	m_skill = nullptr;
	Size visibleSize = Director::getInstance()->getVisibleSize();
	Sprite* BG = Sprite::create("flightBG.jpg");
	BG->setAnchorPoint(Point(0.5f,0.5f));
	BG->setPosition(visibleSize.width/2,visibleSize.height/2);
	this->addChild(BG);
	//m_skill就是技能框,一开始没有选中任何英雄,所以技能框是null
	m_skill = nullptr;


	return true;
}
在update函数里面时刻检测是否有选择英雄

void FlightLayer::updateSkill(){
	if(m_cur_control==nullptr){<span style="white-space:pre">	</span>//如果没有选中的英雄
		if(m_skill){<span style="white-space:pre">			</span>//技能框不为空
			m_skill->removeFromParentAndCleanup(true);//清除技能框
			m_skill = nullptr;
		}
	}
	if(m_skill == nullptr){<span style="white-space:pre">	</span>//如果技能框为空
		if(m_cur_control){<span style="white-space:pre">	</span>//但是有选中的英雄
			Hero* h = dynamic_cast<Hero*>(m_cur_control);//添加技能框
			m_skill = Skill::create(h);
			m_skill->setPosition(50,650);
			addChild(m_skill,4);
		}
	}else{<span style="white-space:pre">			</span>//如果技能框不为空
		if(m_cur_control){<span style="white-space:pre">	</span>//但是有选择的英雄
			if(m_cur_control != m_skill->getHero()){
				m_skill->removeFromParentAndCleanup(true);//如果不同的英雄,删除技能框
				m_skill = nullptr;
			}
		}
	}
}
这里的逻辑只要思考一下应该没什么太大问题吧。

现在大家应该可以看到技能框了

第二步:技能的效果实现

前面说了,每个英雄的每个技能都是不一样的,多姿多彩。前面的技能框已经帮我们触发了skill()接口了

我们看看skill接口的实现


void Hero::skill(){
	SkillImpl impl;
	impl.runSkill(m_skill.id,this);
}
SkillImp是什么东东?哈哈下面我一贴代码大家可能就知道了

#ifndef _SKILL_IMPL_
#define _SKILL_IMPL_
class Hero;
class SkillImpl{
public:
	void runSkill(int id,Hero* hero);
};
#endif
void SkillImpl::runSkill(int id,Hero* hero){
	switch(id){
	case 4001://skill_kb
	{
		hero->setHp(hero->getHp()/2);
		hero->setSpeed(hero->getSpeed()*2);
		hero->setAtkSpeed(hero->getAtkSpeed()*2);
		hero->setAtkHateValue(hero->getAtkHateValue()*3);
		break;
	}

	case 4002://skill_zl
	{
		Role_Ptr initTarget = hero->getAttackTarget();
		std::list<Role_Ptr> list = hero->getLayer()->getRolesArray();
		for(auto it = list.begin();it!=list.end();it++){
			if((**it)->getRoleType() == Role::ROLE_TYPE_HERO){
				hero->setAttackTarget(*it);
				hero->sendBullet();
			}
		}
		hero->setAttackTarget(initTarget);
		break;
	}

	case 4003://skill_ld
	{
		hero->setAtk(hero->getAtk()*2);
		hero->setBulletSpeed(hero->getBulletSpeed()*2);
		break;
	}
	default:
		break;
	}
}

在这里我们通过switch..case就可以修改每个英雄的属性

是不是很笨的方法,大家见谅见谅吧。。


第三步:技能的特效

现在我们实现的只是能得到技能的效果,但是我们要再更炫酷点

为了能够播放特效动画,我们再Hero类里面增加一个函数

void Role::runSkillEffect(int id,int loop){
	Sprite* sp = Sprite::create("Skill/null.png");
	sp->setAnchorPoint(Point(0.5f,0));
	sp->setPosition(0,0);
	this->addChild(sp,10);
	Animate* animate = EffectUtil::getInstance()->getSkillEffectById(id,loop);
	CallFunc* call = CallFunc::create([=](){sp->removeFromParentAndCleanup(true);});
	Sequence* action = Sequence::create(animate,call,NULL);
	sp->runAction(action);
}

这个函数的两个参数分别是技能特效的id,技能特效是否重复播放

由于我们之前说的EffectUtil得到的是一个Animate,Animate的播放必须依赖于Sprite,所以我们先添加一张空白的Sprite,再让这个空白的Sprite将我们动画跑起来

同样,我们修改一下skill接口的实现

void Hero::skill(){
	runSkillEffect(m_skill.begin_e);
	runStateEffect(m_skill.state_e);

	SkillImpl impl;
	impl.runSkill(m_skill.id,this);
}

这样,我们的英雄再使用技能的时候就会播放相应的特效了


最后,还需要做的就是被打中之后触发的特效

这个又要使用了我们的子弹类了。

我们在子弹类里面增加一个属性,使之能够记录攻击者的子弹效果

然后当我们的子弹到达目标之后,在目标的injured函数传递应该触发的效果

void Role::injured(int effect,int damage){
	runSkillEffect(effect);
}


基本就是这样吧。

第四步:技能冷却时间实现
注意每一个技能都有持续时间和冷却时间的。

我们先将持续时间的实现

void Hero::update_state(float dt){
	m_state_lastTime += dt;
	if(m_state_lastTime > m_skill.lastTime){
		if(skilling){
			m_state_sprite->removeFromParentAndCleanup(true);
			m_state_sprite = nullptr;
			recover();
		}
		skilling = false;
		m_state_lastTime = 0;
	}
}
这个只要说明一下,recover函数就是恢复原来的属性的(因为技能会修改属性,所以当持续时间完了之后,要恢复原来属性)


冷却时间的实现

首先我们先修改一下SkillBox,让它有一个计数器,可是技术剩下多少秒

	void updateCD(float delta);
	Label* cdTime;
bool Skill::init(Hero* h){
	m_hero = h;
	skillIcon = ui::Button::create("Skill/" + h->getSkill().icon);
	skillIcon->addTouchEventListener(this,toucheventselector(Skill::onTouchDown));
	this->addChild(skillIcon);

	int cd = m_hero->skillCD;
	cdTime = Label::create(__String::createWithFormat("%d",cd)->getCString(),"Arial",30);
	this->addChild(cdTime);
	if(cd == 0){
		cdTime->setVisible(false);
	}

	this->schedule(schedule_selector(Skill::updateCD),1.0f);

	return true;
}


Hero类里面也增加一个表示冷却时间的属性skillCD

并且在Hero类的update函数时刻更新

void Hero::update_skill_cd(float dt){

	if(skillCD <= 0){
		canUseSkill = true;
		skillCD = 0;
	}else{
		skillCD -= dt;
	}
}

好的。那么我们现在整个游戏就变得炫酷了。

我的csdn地址:http://blog.csdn.net/hezijian22

邮箱地址:578690286@qq.com

如有问题或指教,欢迎与我交流,谢谢。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值