using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace Sog { public class StateMachine { private Entity m_Owner; private State m_oCurrentState; private State m_oPreviousState; private State m_oGlobalState; public StateMachine(Entity Obj) { m_Owner = Obj; m_oCurrentState = null; m_oPreviousState = null; m_oGlobalState = null; } public void SetCurrentState(State s) { m_oCurrentState = s; } public void SetPreviousState(State s) { m_oPreviousState = s; } public void SetGlobalState(State s) { m_oGlobalState = s; } public void Update() { if (m_oGlobalState != null) { m_oGlobalState.Excute(m_Owner); } if (m_oCurrentState != null) { m_oCurrentState.Excute(m_Owner); } } public void ChangeState(State NewState) { if (NewState == null) { return; } if(m_oCurrentState != null) { m_oCurrentState.Exit(m_Owner); m_oPreviousState = m_oCurrentState; } m_oCurrentState = NewState; m_oCurrentState.Enter(m_Owner); } public void RevertToPreviousState() // 如果用来翻转状态, reload时m_oPreviousState也需要重新set { ChangeState(m_oPreviousState); } public bool IsInState(State s) { return m_oCurrentState.Equals(s); } public State CurrentState() { return m_oCurrentState; } public State PreviousState() { return m_oPreviousState; } public State GlobalState() { return m_oGlobalState; } } }