From a13f10139d33264fc9ebc5a15c75faf16fc7757e Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 7 Jul 2021 18:47:37 +0800 Subject: +Action Tool --- Assets/Scripts/Unit/Component/UnitState.cs | 115 +++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 Assets/Scripts/Unit/Component/UnitState.cs (limited to 'Assets/Scripts/Unit/Component/UnitState.cs') 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 m_ExitStateHandlerDic = new Dictionary(); + private Dictionary m_EnterStateHandlerDic = new Dictionary(); + + #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(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 + +} -- cgit v1.1-26-g67d0