有限状态机(FSM)

作者:追风剑情 发布于:2015-5-23 17:40 分类:Unity3d

一、简单有限状态机(FSM)实现


  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. /**
  5. * 简单状态机实现
  6. */
  7. public class SimpleFSM : FSM {
  8. //枚举,定义状态机的四种状态,巡逻,追逐,攻击,死亡
  9. public enum FSMState
  10. {
  11. Patrolling,
  12. Chasing,
  13. Attacking,
  14. Dead,
  15. }
  16.  
  17. //AI角色的当前状态
  18. public FSMState curState;
  19.  
  20. protected override void Initialize(){
  21. //设置FSM的当前状态为巡逻状态;
  22. curState = FSMState.Patrolling;
  23. }
  24.  
  25. protected override void FSMUpdate() {
  26. //判断当前状态,调用函数进行状态更新
  27. switch(curState){
  28. case FSMState.Patrolling:
  29. UpdatePatrolState();
  30. break;
  31. case FSMState.Chasing:
  32. UpdateChaseState();
  33. break;
  34. case FSMState.Attacking:
  35. UpdateAttackState();
  36. break;
  37. case FSMState.Dead:
  38. UpdateDeadState();
  39. break;
  40. }
  41. }
  42.  
  43. //更新巡逻状态
  44. protected void UpdatePatrolState() {
  45.  
  46. }
  47.  
  48. //更新追逐状态
  49. protected void UpdateChaseState() {
  50.  
  51. }
  52.  
  53. //更新攻击状态
  54. protected void UpdateAttackState() {
  55.  
  56. }
  57.  
  58. //更新死亡状态
  59. protected void UpdateDeadState() {
  60.  
  61. }
  62. }


二、通用有限状态机(FSM)框架实现

FSM.png



  1. using UnityEngine;
  2. using System.Collections;
  3. /**
  4. * 有限状态机基类
  5. */
  6. public class FSM : MonoBehaviour {
  7.  
  8. protected virtual void Initialize() {}
  9. protected virtual void FSMUpdate() {}
  10. protected virtual void FSMFixedUpdate() {}
  11.  
  12. void Start () {
  13. //初始化FSM
  14. Initialize ();
  15. }
  16.  
  17. void Update () {
  18. //每帧更新FSM
  19. FSMUpdate ();
  20. }
  21.  
  22. void FixedUpdate(){
  23. //以固定的时间周期更新FSM
  24. FSMFixedUpdate ();
  25. }
  26. }



  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. //定义枚举,为可能的转换分配编号
  6. public enum Transition
  7. {
  8. SawPlayer = 0, //看到玩家
  9. ReachPlayer, //接近玩家,即玩家在攻击范围内
  10. LostPlayer, //玩家离开视线
  11. NoHealth, //死亡
  12. }
  13.  
  14. //定义枚举,为可能的状态分配编号
  15. public enum FSMStateID
  16. {
  17. Patrolling = 0, //巡逻的状态编号为0
  18. Chasing, //追逐的状态编号为1
  19. Attacking, //攻击的状态编号为2
  20. Dead, //死亡的状态编号为3
  21. }
  22.  
  23. /**
  24. * 管理所有状态类
  25. */
  26. public class AdvancedFSM : FSM {
  27.  
  28. //FSM中的所有状态(多个FSMState)组成的列表
  29. private List<FSMState> fsmStates;
  30.  
  31. //当前状态编号
  32. private FSMStateID currentStateID;
  33. public FSMStateID CurrentStateID {get {return currentStateID;}}
  34.  
  35. //当前状态
  36. private FSMState currentState;
  37. public FSMState CurrentState {get {return currentState;}}
  38.  
  39. public AdvancedFSM() {
  40. //新建一个空的状态列表
  41. fsmStates = new List<FSMState> ();
  42. }
  43.  
  44. //向状态列表中加入一个新状态;
  45. public void AddFSMState(FSMState fsmState) {
  46. if (fsmState == null)
  47. Debug.LogError ("FSM ERROR: Null reference is not allowed");
  48.  
  49. if(fsmStates.Count == 0){
  50. fsmStates.Add(fsmState);
  51. currentState = fsmState;
  52. currentStateID = fsmState.ID;
  53. return;
  54. }
  55.  
  56. //检查要加入的状态是否已经在列表中,如果是,报告错误并返回;
  57. foreach(FSMState state in fsmStates){
  58. if(state.ID == fsmState.ID){
  59. Debug.LogError("FSM ERROR: Trying to add a state that was already inside the list");
  60. return;
  61. }
  62. }
  63.  
  64. //如果要加入的状态不在列表中,那么将它加入列表;
  65. fsmStates.Add (fsmState);
  66. }
  67.  
  68. //从状态列表中删除一个状态
  69. public void DeleteState(FSMStateID fsmState) {
  70. foreach (FSMState state in fsmStates) {
  71. if(state.ID == fsmState){
  72. fsmStates.Remove(state);
  73. return;
  74. }
  75. }
  76. Debug.LogError ("FSM ERROR: The state passed was not on the list. Impossible to delete it");
  77. }
  78.  
  79. //根据当前状态,和参娄传递的转换,转移到新的状态;
  80. public void PerformTransition(Transition trans){
  81. //根据当前的状态类,以trans为参娄调用它的GetOutputState方法
  82. //确定转移后新状态的编号
  83. FSMStateID id = currentState.GetOutputState (trans);
  84. //将当前状态编号设置为刚刚返回的新状态编号;
  85. currentStateID = id;
  86. //根据状态编号查找状态列表,这个查找是通过一个遍历过程实现的;
  87. //将当前状态设置为查找到的状态;
  88. foreach(FSMState state in fsmStates){
  89. if(state.ID == currentStateID){
  90. currentState = state;
  91. break;
  92. }
  93. }
  94. }
  95. }



  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. /**
  5. * 负责创建有限状态机,通过此类来控制AI角色
  6. */
  7. public class AIController : AdvancedFSM {
  8.  
  9. //初始化AI角色的FSM,在FSM基类的Start函数中调用;
  10. protected override void Initialize(){
  11. //Do something
  12.  
  13. //调用ConstructFSM函数,开始构造状态机;
  14. ConstructFSM ();
  15. }
  16.  
  17. //在FSM基类的Update函数中调用;
  18. protected override void FSMUpdate() {
  19. //Do something
  20. }
  21.  
  22. //在FSM基类的FixedUpdate函数中调用;
  23. protected override void FSMFixedUpdate() {
  24. //调用当前状态的Reason方法,确定当前发生的转换
  25. CurrentState.Reason (transform);
  26. //调用当前状态的Act方法,确定角色的行为
  27. //注意,如果Reason中检查到满足某个转换条件,就会进行状态转换,
  28. //因此,在这两个调用之间,CurrentState可能会发生变化
  29. CurrentState.Act (transform);
  30. }
  31.  
  32. //这个方法在每个状态类的Reason方法中被调用
  33. public void SetTransition(Transition t) {
  34. //Do something
  35.  
  36. //调用AdvanceFSM类的PerformTransition方法,设置新状态
  37. PerformTransition (t);
  38. }
  39.  
  40. //这个函数在初始化Initialize方法中调用,为AI角色构造FSM
  41. private void ConstructFSM(){
  42. //创建一个巡逻状态类
  43. PatrolState patrol = new PatrolState ();
  44. //调用巡逻状态类中的AddTransition函数
  45. //将这个状态下可能的两个“转换-状态”对(“看到玩家-追逐” 和 "生命值-死亡")
  46. //加入到PatrolState类的字典中
  47. patrol.AddTransition (Transition.SawPlayer, FSMStateID.Chasing);
  48. patrol.AddTransition (Transition.NoHealth, FSMStateID.Dead);
  49.  
  50. //创建一个追逐状态类
  51. ChaseState chase = new ChaseState();
  52. //将这个状态下可能的三个“转换-状态”对加入到ChaseState类的字典中
  53. chase.AddTransition(Transition.LostPlayer, FSMStateID.Patrolling);
  54. chase.AddTransition(Transition.ReachPlayer, FSMStateID.Attacking);
  55. chase.AddTransition(Transition.NoHealth, FSMStateID.Dead);
  56. //创建一个攻击状态类
  57. AttackState attack = new AttackState ();
  58. //将这个状态下可能的三个“转换-状态”对加入到AttackState类的字典中
  59. attack.AddTransition(Transition.LostPlayer, FSMStateID.Patrolling);
  60. attack.AddTransition(Transition.SawPlayer, FSMStateID.Chasing);
  61. attack.AddTransition(Transition.NoHealth, FSMStateID.Dead);
  62. //创建一个死亡状态类
  63. DeadState dead = new DeadState ();
  64. //将这个状态下可能的一个“转换-状态”对加入到DeadState类的字典中
  65. dead.AddTransition(Transition.NoHealth, FSMStateID.Dead);
  66. }
  67.  
  68. //当AI角色与其他物体碰撞时,调用这个函数
  69. void OnCollisionEnter(Collision collision) {
  70.  
  71. }
  72. }



  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. /**
  6. * 状态抽象类
  7. */
  8. public abstract class FSMState {
  9.  
  10. //字典,字典中的每一项都记录了一个 “转换-状态” 对的信息
  11. protected Dictionary<Transition, FSMStateID> map = new Dictionary<Transition, FSMStateID> ();
  12.  
  13. //状态编号ID
  14. protected FSMStateID stateID;
  15. public FSMStateID ID {get{return stateID;}}
  16.  
  17. #region 根据实际情况定义需要用到的,与各状态相关的变量
  18.  
  19. #endregion
  20.  
  21. //向字典中添加项;每项是一个“转换-状态”对;
  22. public void AddTransition (Transition transition, FSMStateID id) {
  23. if(map.ContainsKey(transition)){
  24. Debug.LogWarning("FSMState ERROR: transition is already inside the map");
  25. return;
  26. }
  27. map.Add (transition, id);
  28. Debug.Log ("Added : " + transition + " with ID : " + id);
  29. }
  30.  
  31. //从字典中删除某一项
  32. public void DeleteTransition(Transition trans){
  33. if (map.ContainsKey (trans)) {
  34. map.Remove(trans);
  35. return;
  36. }
  37. Debug.LogError ("FSMState ERROR: Transition passed was not on this State's List");
  38. }
  39.  
  40. //通过查询字典,确定在当前状态下,发生trans转换时,应该转换到的新状态编号;
  41. //并返回这个新状态编号;
  42. public FSMStateID GetOutputState(Transition trans){
  43. return map[trans];
  44. }
  45.  
  46. //Reason方法用来确定是否需要转换到其他状态,应该发生哪个转换;
  47. //至于此方法是否需要定义参数以及定义哪些参数,可根据实际情况决定
  48. //在此方法中若满足条件需要进行状态转移,可以AIPlayer.GetComponent<AIController>().SetTransition(xx,xx);
  49. public abstract void Reason(Transform AIPlayer);
  50.  
  51. //Act方法定义了在本状态的角色行为,例如移动,动画等
  52. //至于此方法是否需要定义参数以及定义哪些参数,可根据实际情况决定
  53. public abstract void Act(Transform AIPlayer);
  54. }


==================== 以上为框架代码============



  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. /**
  5. * 巡逻状态类
  6. */
  7. public class PatrolState : FSMState {
  8.  
  9. public PatrolState(){
  10. //设置状态编号
  11. stateID = FSMStateID.Patrolling;
  12. }
  13.  
  14. //这个方法决定是否需要转换状态,以及发生哪种转换
  15. public override void Reason(Transform AIPlayer) {
  16.  
  17. }
  18.  
  19. //这个方法定义了在这个状态下AI角色的行为
  20. public override void Act(Transform AIPlayer) {
  21.  
  22. }
  23. }



  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. /**
  5. * 追逐状态类
  6. */
  7. public class ChaseState : FSMState {
  8.  
  9. public ChaseState(){
  10. //设置状态编号
  11. stateID = FSMStateID.Chasing;
  12. }
  13. //这个方法决定是否需要转换状态,以及发生哪种转换
  14. public override void Reason(Transform AIPlayer) {
  15. }
  16. //这个方法定义了在这个状态下AI角色的行为
  17. public override void Act(Transform AIPlayer) {
  18. }
  19. }



  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. /**
  5. * 攻击状态类
  6. */
  7. public class AttackState : FSMState {
  8.  
  9. public AttackState(){
  10. //设置状态编号
  11. stateID = FSMStateID.Attacking;
  12. }
  13. //这个方法决定是否需要转换状态,以及发生哪种转换
  14. public override void Reason(Transform AIPlayer) {
  15. }
  16. //这个方法定义了在这个状态下AI角色的行为
  17. public override void Act(Transform AIPlayer) {
  18. }
  19. }



  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. /**
  5. * 死亡状态类
  6. */
  7. public class DeadState : FSMState {
  8.  
  9. public DeadState(){
  10. //设置状态编号
  11. stateID = FSMStateID.Dead;
  12. }
  13. //这个方法决定是否需要转换状态,以及发生哪种转换
  14. public override void Reason(Transform AIPlayer) {
  15. }
  16. //这个方法定义了在这个状态下AI角色的行为
  17. public override void Act(Transform AIPlayer) {
  18. }
  19. }




标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号