Cocos经典游戏教程之仿皇室战争

版权声明:

  • 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客"
  • 您可以自由转载,但必须加入完整的版权声明!

客户端

实现军队降落状态

  • 将军队空闲状态会进入移动状态改为进入ArmyCallState状态
OnEnter(){
	// TODO: 选择路点,进入ArmyCallState
	this.card.fsm.ChangeState(ArmyCallState);
}
  • 在ArmyCallState脚本中实现了ArmyCallState类(就是编写了ArmyCallState脚本)

在军队移动状态中增加播放行走的动画的功能

//取得军队移动方向与上方向之间的弧度
let radian = dir.signAngle(cc.Vec2.UP);
//将弧度转为角度
let angle = cc.misc.radiansToDegrees(radian);

//如果军队移动方向与上方向的夹角在 -45到45之间,说明军队移动的方向是偏向上方的
if (angle > -45 && angle < 45) {
	let clip = this.animation.getClips()[0]; //取得往上走的动画片段
	if (this.animation.currentClip != clip) { //如果当前播放的动画不是往上走的动画
		this.animation.play(clip.name); //播放往上走的动画
	}
}
else if (angle > 135 || angle < -135) { //下
	let clip = this.animation.getClips()[1]; //下走
	if (this.animation.currentClip != clip) {
		this.animation.play(clip.name);
	}
}
else if (angle >= -135 && angle <= -45) { //左
	let clip = this.animation.getClips()[2]; //左走
	if (this.animation.currentClip != clip) {
		this.animation.play(clip.name);
	}
}
else if (angle >= 45 && angle <= 135) { //右
	let clip = this.animation.getClips()[3]; //右走
	if (this.animation.currentClip != clip) {
		this.animation.play(clip.name);
	}
}

在军队移动状态中增加获取移动速度和路点索引

//取得初始路点(桥)
let initialWaypoint = WayMgr.instance.ways[this.wayIdx].nodes[0].position;
//取得终点(国王塔位置)
let destination = WayMgr.instance.ways[this.wayIdx].nodes[1].position;
//如果玩家到国王塔的距离 小于 桥到国王塔的距离
if(destination.sub(this.card.pos).mag()<destination.sub(initialWaypoint).mag()){
	this.wpIdx = 1; //就将路点索引设为1(就是直接往国王塔走)
}

let moveSpeed = this.card.propsTmp.mspd //获取模板属性中的移动速度(枚举值)
if(moveSpeed == MoveSpeed.Invalid){ //如果速度是无效
	this.spd = 0;
}
else if(moveSpeed == MoveSpeed.VeryFast){ //非常快
	this.spd = 50;
}
else if(moveSpeed == MoveSpeed.Fast){ //快
	this.spd = 40;
}
else if(moveSpeed == MoveSpeed.Mid){ //正常
	this.spd = 30;
}
else if(moveSpeed == MoveSpeed.Slow){ //慢
	this.spd = 20;
}
else if(moveSpeed == MoveSpeed.VerySlow){ //非常慢
	this.spd = 10;
}

在军队移动中添加追踪敌人功能

  • 获取敌方战斗玩家
  • 遍历敌方战斗玩家的战斗卡牌数组
  • 从敌方战斗卡牌数组中找到离this战斗卡牌最近的敌方战斗卡牌
  • 如果那个敌方战斗卡牌在this战斗卡牌的视野内就往敌方那里走
  • 如果那个敌方战斗卡牌在this战斗卡牌的攻击范围内就进入军队攻击状态
posted @ 2019-08-09 11:42  优梦创客  阅读(941)  评论(0编辑  收藏  举报