summaryrefslogtreecommitdiff
path: root/Assets/Scripts/Unit/Component/UnitState.cs
blob: d5b91cec5768e45fa19f85404e9431f15bddc1b6 (plain)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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 

}