summaryrefslogtreecommitdiff
path: root/Assets/Scripts/Unit/Component/UnitState.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Assets/Scripts/Unit/Component/UnitState.cs')
-rw-r--r--Assets/Scripts/Unit/Component/UnitState.cs157
1 files changed, 112 insertions, 45 deletions
diff --git a/Assets/Scripts/Unit/Component/UnitState.cs b/Assets/Scripts/Unit/Component/UnitState.cs
index 3832c933..500096d3 100644
--- a/Assets/Scripts/Unit/Component/UnitState.cs
+++ b/Assets/Scripts/Unit/Component/UnitState.cs
@@ -8,20 +8,26 @@ 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,
+ Idle ,
+ Move ,
+ Spawn ,
+ Die ,
+ Dead ,
+ Skill ,
+ //
+ HitAir ,
+ HitAirHit ,
+ Knockdown ,
+ //
+ HitGuard ,
+ //
+ Walk ,
+ //
+ Rise ,
+ //
+ Jump ,
+ // 转身
+ Turn ,
}
[SerializeField] private EUnitState m_State;
@@ -31,47 +37,66 @@ public class UnitState : UnitComponent
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 override void Initialize()
+ {
+ base.Initialize();
+
+ InitState();
+ }
+
+ #region state param
+ public struct IdleParam {}
public struct MoveParam
{
+ public bool isRight;
+ public string key;
}
public struct SkillParam
{
}
+
+ public struct JumpParam
+ { }
+
+ public struct TurnParam
+ {
+ EUnitState nextState;
+ }
+
#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);
- }
+ m_ExitStateHandlerDic.Add(EUnitState.Skill, OnSkillExit);
+ m_ExitStateHandlerDic.Add(EUnitState.Jump, OnJumpExit);
+ }
- public void ChangeState<T>(EUnitState nextState, T param, bool bForce = false)
+ public void ChangeState<T>(EUnitState nextState, T param = default, bool bForce = false)
{
if (!IsChange(nextState, bForce))
return;
- StopAllCoroutines();
+ LogHelper.Log("UnitState: " + m_State.ToString() + " -> " + nextState.ToString());
- m_ExitStateHandlerDic[m_State](nextState);
+ StopAllCoroutines();
- EUnitState prevState = m_State;
- m_State = nextState;
- m_EnterStateHandlerDic[m_State](prevState);
+ EUnitState prevState = m_State;
+ if (m_ExitStateHandlerDic.ContainsKey(m_State))
+ {
+ m_ExitStateHandlerDic[m_State](nextState);
+ }
+ m_State = nextState;
- StartCoroutine(m_State.ToString(), param);
- }
+ StartCoroutine(m_State.ToString(), param);
+ }
- bool IsChange(EUnitState newState, bool bForce)
+ bool IsChange(EUnitState newState, bool bForce)
{
if (newState != m_State || bForce)
return true;
@@ -79,15 +104,13 @@ public class UnitState : UnitComponent
}
#region Idle
- void OnIdleEnter(EUnitState prevState)
- {
- }
- IEnumerator Idle(IdleParam param)
+ IEnumerator Idle(IdleParam param)
{
- //m_Owner.unitAnimation.Play();
- yield return null;
- }
+ m_Owner.unitAnimation.Play(UnitAnimation.EAnimState.Idle);
+ yield return null;
+ }
+
void OnIdleExit(EUnitState nextState)
{
@@ -95,14 +118,26 @@ public class UnitState : UnitComponent
#endregion
#region Move
- void OnMoveEnter(EUnitState prevState)
- {
-
- }
-
- IEnumerator Move(MoveParam param)
+ IEnumerator Move(MoveParam param)
{
- yield return null;
+ if (m_Owner.isTowardRight && !param.isRight
+ || !m_Owner.isTowardRight && param.isRight)
+ {
+ //m_Owner.unitAnimation.Play(UnitAnimation.EAnimState.Turn);
+ //yield return new WaitForActionReachEnd(m_Owner.unitAnimation);
+ //if (param.isRight)
+ // m_Owner.transform.rotation = Quaternion.Euler(0, 0, 0);
+ //else
+ // m_Owner.transform.rotation = Quaternion.Euler(0, 180, 0);
+ m_Owner.transform.rotation = Quaternion.Euler(0, param.isRight ? 0 : 180, 0);
+ }
+ if(Input.GetKey(param.key))
+ m_Owner.unitAnimation.Play(UnitAnimation.EAnimState.Move);
+ while (Input.GetKey(param.key))
+ {
+ yield return null;
+ }
+ ChangeState(EUnitState.Idle, new IdleParam());
}
void OnMoveExit(EUnitState nextState)
@@ -110,6 +145,38 @@ public class UnitState : UnitComponent
}
- #endregion
+ #endregion
+
+ #region Skill
+
+ IEnumerator Skill(SkillParam param)
+ {
+ m_Owner.unitAnimation.Play(UnitAnimation.EAnimState.Attack);
+ yield return new WaitForActionReachEnd(m_Owner.unitAnimation);
+ ChangeState(EUnitState.Idle, new IdleParam());
+ }
+
+ void OnSkillExit(EUnitState next)
+ {
+
+ }
+
+ #endregion
+
+ #region Jump
+
+ IEnumerator Jump(JumpParam param)
+ {
+ m_Owner.unitAnimation.Play(UnitAnimation.EAnimState.Jump);
+ yield return new WaitForActionReachEnd(m_Owner.unitAnimation);
+ ChangeState<IdleParam>(EUnitState.Idle);
+ }
+
+ void OnJumpExit(EUnitState next)
+ {
+
+ }
+
+ #endregion
}