summaryrefslogtreecommitdiff
path: root/WorldlineKeepers/Assets/Scripts/Tools/Statemachine/BasicStatemachine.cs
blob: 15ab82c8fc81721f0d054cbb961b9d2de29d81d9 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json.Utilities;

namespace WK
{
    /// <summary>
    /// 一般状态机
    /// </summary>
    public class BasicStatemachine
    {
        public delegate void StateEvent();
        public delegate bool StateFinishChecker();
        public class State
        {
            public BasicStatemachine owner;
            public int stateId;

            public virtual void BeginState()
            {
                //reserve function
            }
            public virtual void EndState()
            {
                //reserve function
            }
            //时间单位是毫秒      
            public virtual bool UpdateState(int deltaTimeMS)
            {
                return true;
            }
        }

        private Dictionary<int/*state id*/, State> stateDic = new Dictionary<int/*state id*/, State>();
        private int currentStateId = -1;
        private int stateIdDistributor = 0;

        private int recordLastStateId = -1;

        public int RegisterState(State newState)
        {
            if (newState != null)
            {
                if (stateIdDistributor + 1 > int.MaxValue)
                {
                    LogHelper.LogError("状态机添加状态失败:一个状态机中添加了过多的状态!");
                    return -1;
                }

                stateIdDistributor++;
                if (!stateDic.ContainsKey(stateIdDistributor))
                {
                    stateDic.Add(stateIdDistributor, newState);
                    newState.owner = this;
                    newState.stateId = stateIdDistributor;
                    return stateIdDistributor;
                }
            }
            LogHelper.LogError("状态机添加状态失败:无效的新状态或新状态已存在!");
            return -1;
        }

        public bool RemoveState(State toBeRemoveState)
        {
            if (toBeRemoveState != null)
                return RemoveState(toBeRemoveState.stateId);
            LogHelper.LogError("状态机删除状态失败:无效的状态!");
            return false;
        }

        public bool RemoveState(int stateId)
        {
            if (stateDic.ContainsKey(stateId))
            {
                stateDic.Remove(stateId);
                return true;
            }
            LogHelper.LogError("状态机删除状态失败:该状态不存在!");
            return false;
        }

        public bool Begin(int beginStateId)
        {
            if (!HasBegin())
            {
                ForceGoToState(beginStateId, false);
                return true;
            }

            return false;
        }

        public bool Stop()
        {
            if (HasBegin())
            {
                ForceGoToState(-1);
                return true;
            }

            return false;
        }

        public bool GoToState(int newStateId, bool skipBeginFunc = false, bool forceLoad = false)
        {
            if (HasBegin())
            {
                return ForceGoToState(newStateId, skipBeginFunc, forceLoad);
            }
            return false;
        }

        private bool ForceGoToState(int newStateId)
        {
            return ForceGoToState(newStateId, false);
        }
        private bool ForceGoToState(int newStateId, bool skipBeginFunc, bool bForce = false)
        {
            if (currentStateId != newStateId || bForce)
            {
                OnStateEnd(currentStateId);
                currentStateId = newStateId;
                if (!skipBeginFunc) OnStateBegin(currentStateId);
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filterState">剔除的state</param>
        public void RecordCurrentState(int filterState)
        {
            if (currentStateId != filterState)
            {
                recordLastStateId = currentStateId;
            }
        }

        public void ClearRecordState()
        {
            recordLastStateId = -1;
        }

        public void RevertToRecordState(int fallbackState, bool skipBeginFunc, bool bForce = false)
        {
            if (recordLastStateId >= 0 && stateDic.ContainsKey(recordLastStateId))
            {
                GoToState(recordLastStateId, skipBeginFunc, bForce);
            }
            else if (fallbackState >= 0 && stateDic.ContainsKey(fallbackState))
            {
                GoToState(fallbackState, skipBeginFunc, bForce);
            }
        }


        //时间单位是毫秒
        public void OnUpdate(int mSecDeltaTime)
        {
            if (HasBegin())
            {
                UpdateState(currentStateId, mSecDeltaTime);
            }
        }

        public bool HasBegin()
        {
            if (currentStateId != -1 && stateDic.ContainsKey(currentStateId))
                return true;
            else
                return false;
        }

        public bool IsInState(int stateId)
        {
            if (HasBegin())
            {
                return currentStateId == stateId;
            }
            return false;
        }


        public int GetCurrentStateId()
        {
            return currentStateId;
        }

        public State GetState(int stateId)
        {
            if (stateDic.ContainsKey(stateId))
            {
                return stateDic[stateId];
            }
            return null;
        }

        public void Clean()
        {
            stateDic.Clear();
            currentStateId = -1;
            stateIdDistributor = 0;
            recordLastStateId = -1;
        }

        /// <summary>
        /// 重置当前状态(置为-1)
        /// </summary>
        public void ResetCurrentState()
        {
            currentStateId = -1;
        }

        private void OnStateBegin(int stateId)
        {
            if (HasBegin())
            {
                State state = GetState(stateId);
                if (state != null)
                    state.BeginState();
            }
        }

        private void OnStateEnd(int stateId)
        {
            if (HasBegin())
            {
                State state = GetState(stateId);
                if (state != null)
                    state.EndState();
            }
        }
        //时间单位是毫秒
        private void UpdateState(int stateId, int mSecDeltaTime)
        {
            if (HasBegin())
            {
                State state = GetState(stateId);
                if (state != null)
                    state.UpdateState(mSecDeltaTime);
            }
        }
    }

}