最普通的if else 简单的不能再简单了
public override void AI_UpdateMSWithAI()
{
cd_atk--;
//如果目标非法,那么寻找另外一个目标
if (target == null || target.IsInValid())
{
this.AI_SearchNewTarget();
return;
}
if (isHurt) return;
// 有目标 ,先判断是否在攻击范围内
float dis = target.ClaculateDistance(x, y);
if (dis < 2)
{
//攻击范围内
this.AI_AttackTarget();
}
else
{
//不在攻击范围内 移动向目标
this.AI_MoveToTarget();
}
}
public virtual void AI_SearchNewTarget()
{
ArrayList heros = EnemyMgr.ins.GetEnemys();// HeroMgr.ins.GetHeros();
float minDis = 9999.0f;
foreach (Entity h in heros)
{//找出一个最近的玩家 作为锁定目标
if (h == this) continue;
float dis = h.ClaculateDistance(x, y);
if (dis < minDis)
{
target = h;
minDis = dis;
}
}
}
public virtual void AI_MoveToTarget()
{
dir = (int)Utils.GetAngle(this.pos, target.pos);
}
public virtual void AI_AttackTarget()
{
if (target.isDie)
{
target = null;
stand = true;
return;
}
if (cd_atk <= 0)
{
cd_atk = 80;// 2S
atk = true;
}
else
{
stand = true;
}
}
下一篇 重构为 FSM
源代码:https://git.oschina.net/dreamyouxi/XYGame