怪物动画状态

增加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
|
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 => { 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); }, Constants.DieAniLength); } } }
|
修改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; 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")) { return clips[i].length; } } return 1; } } }
|