using SHADE; using System; using System.Collections.Generic; using System.Linq; public class StateMachine : Script { private Dictionary stateDictionary; public BaseState currentState = null; public string currentStateName; public string currentAnimName; public void InitStateMachine(Dictionary dictionary) { stateDictionary = dictionary; currentState = stateDictionary.First().Value; currentStateName = currentState.GetStateName(); currentAnimName = currentState.GetAnimName(); currentState.OnEnter(); } public bool HasState(Type type) { if (!type.IsSubclassOf(typeof(BaseState))) { return false; } else { return stateDictionary.ContainsKey(type); } } public void SetState(Type type) { if (!type.IsSubclassOf(typeof(BaseState))) { return; } if (stateDictionary.ContainsKey(type)) { currentState.OnExit(); currentState = stateDictionary[type]; currentState.OnEnter(); } else { SetState(stateDictionary.First().Key); } } public BaseState GetState(Type type) { if (!stateDictionary.ContainsKey(type)) return null; return stateDictionary[type]; } public bool IsState(Type type) { return (currentState.GetType() == type); } protected override void update() { if (currentState != (null)) { currentStateName = currentState.GetStateName(); currentAnimName = currentState.GetAnimName(); currentState.update(); } } protected override void fixedUpdate() { if (currentState != (null)) { currentStateName = currentState.GetStateName(); currentAnimName = currentState.GetAnimName(); currentState.fixedUpdate(); } } protected override void onCollisionEnter(CollisionInfo info) { if (currentState != (null)) currentState.onCollisionEnter(info); } protected override void onCollisionStay(CollisionInfo info) { if (currentState != (null)) currentState.onCollisionStay(info); } protected override void onCollisionExit(CollisionInfo info) { if (currentState != (null)) currentState.onCollisionExit(info); } protected override void onTriggerEnter(CollisionInfo info) { if (currentState != (null)) currentState.onTriggerEnter(info); } protected override void onTriggerStay(CollisionInfo info) { if (currentState != (null)) currentState.onTriggerStay(info); } protected override void onTriggerExit(CollisionInfo info) { if (currentState != (null)) currentState.onTriggerExit(info); } }