diff options
Diffstat (limited to 'Assets/Scripts/Unit/Component/UnitState.cs')
-rw-r--r-- | Assets/Scripts/Unit/Component/UnitState.cs | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/Assets/Scripts/Unit/Component/UnitState.cs b/Assets/Scripts/Unit/Component/UnitState.cs new file mode 100644 index 00000000..d5b91cec --- /dev/null +++ b/Assets/Scripts/Unit/Component/UnitState.cs @@ -0,0 +1,115 @@ +using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+// 角色状态机
+[DisallowMultipleComponent]
+public class UnitState : UnitComponent
+{
+ public enum EUnitState
+ {
+ Idle = 1,
+ Move = 1 << 2,
+ Spawn = 1 << 3,
+ Die = 1 << 4,
+ Dead = 1 << 5,
+ Skill = 1 << 6,
+ //
+ HitAir = 1 << 7,
+ HitAirHit = 1 << 8,
+ Knockdown = 1 << 9,
+ //
+ HitGuard = 1 << 10,
+ //
+ Walk = 1 << 11,
+ }
+
+ [SerializeField] private EUnitState m_State;
+ public EUnitState CurrentState { get { return m_State; } }
+
+ private delegate void ExitStateHandler(EUnitState nextState);
+ private delegate void EnterStateHandler(EUnitState prevState);
+
+ private Dictionary<EUnitState, ExitStateHandler> m_ExitStateHandlerDic = new Dictionary<EUnitState, ExitStateHandler>();
+ private Dictionary<EUnitState, EnterStateHandler> m_EnterStateHandlerDic = new Dictionary<EUnitState, EnterStateHandler>();
+
+ #region state param
+ public struct IdleParam {}
+
+ public struct MoveParam
+ {
+ }
+
+ public struct SkillParam
+ {
+
+ }
+ #endregion
+
+ void InitState()
+ {
+ m_EnterStateHandlerDic.Add(EUnitState.Idle, OnIdleEnter);
+ m_EnterStateHandlerDic.Add(EUnitState.Move, OnMoveEnter);
+
+ m_ExitStateHandlerDic.Add(EUnitState.Idle, OnIdleExit);
+ m_ExitStateHandlerDic.Add(EUnitState.Move, OnMoveExit);
+ }
+
+ public void ChangeState<T>(EUnitState nextState, T param, bool bForce = false)
+ {
+ if (!IsChange(nextState, bForce))
+ return;
+
+ StopAllCoroutines();
+
+ m_ExitStateHandlerDic[m_State](nextState);
+
+ EUnitState prevState = m_State;
+ m_State = nextState;
+ m_EnterStateHandlerDic[m_State](prevState);
+
+ StartCoroutine(m_State.ToString(), param);
+ }
+
+ bool IsChange(EUnitState newState, bool bForce)
+ {
+ if (newState != m_State || bForce)
+ return true;
+ return false;
+ }
+
+ #region Idle
+ void OnIdleEnter(EUnitState prevState)
+ {
+
+ }
+ IEnumerator Idle(IdleParam param)
+ {
+ m_Owner.unitAnimation.Play();
+ yield return null;
+ }
+ void OnIdleExit(EUnitState nextState)
+ {
+
+ }
+ #endregion
+
+ #region Move
+ void OnMoveEnter(EUnitState prevState)
+ {
+
+ }
+
+ IEnumerator Move(MoveParam param)
+ {
+ yield return null;
+ }
+
+ void OnMoveExit(EUnitState nextState)
+ {
+
+ }
+
+ #endregion
+
+}
|