怪物动画状态

怪物动画

增加Born状态

StateBorn

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
namespace DarknessWarGodLearning
{
public class StateBorn : IState
{
public void Enter(EntityBase entity, params object[] args)
{
entity.currentAniState = AniState.Born;
}

public void Exit(EntityBase entity, params object[] args)
{
}

public void Process(EntityBase entity, params object[] args)
{
//播放出生动画
entity.SetAction(Constants.ActionBorn);
TimerSvc.Instance.AddTimeTask(tid =>
{
entity.SetAction(Constants.ActionDefault);
},500);
}
}
}

AniState中添加Born,在StateMgr中将此状态加入FSM略。

EntityBase中添加进入此状态的方法:

1
2
3
4
5
6
7
/// <summary>
/// 将此Entity转换为Born状态
/// </summary>
public void Born()
{
stateMgr.ChangeStates(this, AniState.Born, null);
}

BattleMgr中激活怪物时设置为Born状态,并使用计时来重置为Idle状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public void ActiveCurrentBatchMonsters()
{
TimerSvc.Instance.AddTimeTask( tid =>
{
foreach (var item in entityMonsterDic)
{
item.Value.controller.gameObject.SetActive(true);
item.Value.Born();
TimerSvc.Instance.AddTimeTask(id =>
{
//出生1秒钟后进入IDle
item.Value.Idle();
}, 1000);
}
}, 500);
}

StateBorn中使用计时来将动画重置为Idle状态,见上面的代码。

增加Die状态

和增加Born状态相似,添加StateDie状态类,在StateMgr中初始化此状态,在EntityBase中添加进入此状态的方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
namespace DarknessWarGodLearning
{
public class StateDie : IState
{
public void Enter(EntityBase entity, params object[] args)
{
entity.currentAniState = AniState.Die;
}

public void Exit(EntityBase entity, params object[] args)
{
}

public void Process(EntityBase entity, params object[] args)
{
entity.SetAction(Constants.ActionDie);
TimerSvc.Instance.AddTimeTask(tid =>
{
entity.controller.gameObject.SetActive(false);//不销毁,先Disable
}, Constants.DieAniLength);//这里设为5秒
}
}
}

修改SkillMgr,将伤害作用到怪物实体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
private void CalcDamage(EntityBase caster,EntityBase target,SkillCfg skillCfg,int damage)
{
int dmgSum = damage;
if (skillCfg.dmgType == DamageType.AD)
{
//...

}else if (skillCfg.dmgType == DamageType.AP)
{
//...
}
else { }
//最终伤害
if (dmgSum < 0)
{
dmgSum = 0;
return;
}

if(target.HP<dmgSum)
{
target.HP = 0;
//目标死亡,将目标GameObject的Active设为False
target.Die();
}
else
{
target.HP -= dmgSum;
target.Hit();//目标受击
}
}

增加Hit状态

StateHit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using UnityEngine;

namespace DarknessWarGodLearning
{
public class StateHit : IState
{
public void Enter(EntityBase entity, params object[] args)
{
entity.currentAniState = AniState.Hit;
}

public void Exit(EntityBase entity, params object[] args)
{
}

public void Process(EntityBase entity, params object[] args)
{
entity.SetDir(Vector2.zero);//受击后停止动作
entity.SetAction(Constants.ActionHit);
TimerSvc.Instance.AddTimeTask(tid =>
{
entity.SetAction(Constants.ActionDefault);
entity.Idle();
},(int)(GetHitAniLength(entity)*1000));
}
private float GetHitAniLength(EntityBase entity)
{
AnimationClip[] clips = entity.controller.playerAnimator.runtimeAnimatorController.animationClips;
for (int i = 0; i < clips.Length; i++)
{
string clipName = clips[i].name;
if (clipName.Contains("hit"))//和美术商量好所有的受击动画都包含hit字母
{
return clips[i].length;
}
}
//保护值
return 1;
}
}
}