1 using System; 2 using System.Collections; 3 using System.Collections.Generic; 4 using UnityEngine; 5 6 public class FSM : MonoBehaviour 7 { 8 //状态列表 9 private List<FSMState> stateList; 10 //当前状态 11 private FSMState currentState; 12 [Tooltip("默认状态编号")] 13 public FSMStateID defaultStateID; 14 //默认状态 15 FSMState defaultState; 16 17 private void Start() 18 { 19 ConfigFSM(); 20 InitDefaultState(); 21 } 22 //配置状态机 23 private void ConfigFSM() 24 { 25 stateList = new List<FSMState>(); 26 //添加状态 27 //stateList.Add(xxState); 28 //添加条件 29 //xxState.AddMap(xx,xx); 30 } 31 32 private void InitDefaultState() 33 { 34 defaultState = stateList.Find(s => s.StateID == defaultStateID); 35 36 currentState = defaultState; 37 38 currentState.EnterState(this); 39 40 } 41 42 private void Update() 43 { 44 currentState.Reason(this); 45 currentState.Action(this); 46 } 47 48 //切换状态 49 public void ChangeState(FSMStateID nextStateID) 50 { 51 FSMState nextState = nextStateID == FSMStateID.Default ? defaultState : stateList.Find(s => s.StateID == nextStateID); 52 53 currentState.ExitState(this); 54 currentState = nextState; 55 currentState.EnterState(this); 56 } 57 58 }
1 using System; 2 using System.Collections; 3 using System.Collections.Generic; 4 using UnityEngine; 5 6 public abstract class FSMState{ 7 8 //状态ID 9 public FSMStateID StateID { get; set; } 10 11 public FSMState() 12 { 13 triggerList = new List<FSMTrigger>(); 14 map = new Dictionary<FSMTriggerID, FSMStateID>(); 15 Init(); 16 } 17 18 protected abstract void Init(); 19 //状态要有条件列表 20 List<FSMTrigger> triggerList; 21 private Dictionary<FSMTriggerID, FSMStateID> map; 22 23 //添加映射 24 public void AddMap(FSMTriggerID triggerID, FSMStateID stateID) 25 { 26 map.Add(triggerID, stateID); 27 AddTriggerObject(triggerID); 28 } 29 30 //反射创建对象 31 private void AddTriggerObject(FSMTriggerID trigger) 32 { 33 Type type = Type.GetType(trigger + "Trigger"); 34 var obj = Activator.CreateInstance(type) as FSMTrigger; 35 triggerList.Add(obj); 36 } 37 38 //条件检测 39 public void Reason(FSM fsm) 40 { 41 for (int i = 0; i < triggerList.Count; i++) 42 { 43 if (triggerList[i].HandleTrigger(fsm)) 44 { 45 //切换状态 46 FSMStateID stateId = map[triggerList[i].TriggerID]; 47 //通过状态机切换状态 48 49 return; 50 } 51 } 52 } 53 54 public virtual void EnterState(FSM fsm) { } 55 public virtual void Action(FSM fsm) { } 56 public virtual void ExitState(FSM fsm) { } 57 }
1 using System; 2 using System.Collections; 3 using System.Collections.Generic; 4 using UnityEngine; 5 6 public abstract class FSMTrigger{ 7 //条件编号 8 public FSMTriggerID TriggerID { get; set; } 9 10 public FSMTrigger() 11 { 12 Init(); 13 } 14 //初始化(要求子类必须初始化条件编号) 15 protected abstract void Init(); 16 17 public abstract bool HandleTrigger(FSM fsm); 18 }
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public enum FSMStateID{ 6 None, 7 Default, 8 Dead, 9 Idle, 10 Pursuit, 11 Attacking, 12 Patrolling 13 }
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public enum FSMTriggerID 6 { 7 NoHealth, 8 SawTarget, 9 ReachTarget, 10 CompleteTarget, 11 KilledTarget, 12 WithoutAttackRange 13 }