unity之通用FSM有限状态机 —>流程管理的使用(二)

上图:

启动流程、检查更新流程  ...  游戏关卡流程  等等  全部继承自流程基类,而流程又属于状态的一种,所以流程基类继承 父类FsmState。

流程属于状态,状态需要有一个拥有者来进行管理,切换状态。那么流程的拥有者就是流程管理器。

 

流程管理类:

 1 using System;
 2 
 3 /// <summary>
 4 /// 状态枚举
 5 /// </summary>
 6 public enum TestProcedureStateEnum
 7 {
 8     Launch = 0,
 9     CheckVersion = 1,
10     Preload = 2,
11     ChangeScene = 3,
12     LogOn = 4,
13     SelectRole = 5,
14     EnterGame = 6,
15     PVE = 7,
16     PVP = 8,
17 }
18 
19 public class TestProcedureManager : IDisposable
20 {
21     /// <summary>
22     /// 当前状态机
23     /// </summary>
24     public TestFsm<TestProcedureManager> CurrFsm { get; private set; }
25 
26     /// <summary>
27     /// 当前状态机的状态
28     /// </summary>
29     public TestFsmState<TestProcedureManager> CurrFsmState { get; private set; }
30 
31     /// <summary>
32     /// 当前状态的枚举
33     /// </summary>
34     public TestProcedureStateEnum CurrStateEnum { get; private set; }
35 
36     private readonly TestFsmState<TestProcedureManager>[] m_Status = null;
37     public TestProcedureManager()
38     {
39         m_Status = new TestFsmState<TestProcedureManager>[Enum.GetValues(typeof(TestProcedureStateEnum)).Length];
40     }
41 
42     public void Init()
43     {
44         m_Status[(int)TestProcedureStateEnum.Launch] = new TestProcedureLaunch();
45         m_Status[(int)TestProcedureStateEnum.CheckVersion] = new TestProcedureCheckVersion();
46         m_Status[(int)TestProcedureStateEnum.Preload] = new TestProcedurePreload();
47         m_Status[(int)TestProcedureStateEnum.ChangeScene] = new TestProcedureChangeScene();
48         m_Status[(int)TestProcedureStateEnum.LogOn] = new TestProcedureLogOn();
49         m_Status[(int)TestProcedureStateEnum.SelectRole] = new TestProcedureSelectRole();
50         m_Status[(int)TestProcedureStateEnum.EnterGame] = new TestProcedureEnterGame();
51         m_Status[(int)TestProcedureStateEnum.PVE] = new TestProcedurePVE();
52         m_Status[(int)TestProcedureStateEnum.PVP] = new TestProcedurePVP();
53         CurrFsm = TestGameEntry.FsmMgr.CreateFsm(this, m_Status);
54         if (CurrFsm != null)
55         {
56             CurrFsmState = CurrFsm.GetState(CurrFsm.CurrState);
57             CurrStateEnum = (TestProcedureStateEnum)CurrFsm.CurrState;
58         }
59     }
60 
61     public void ChangeState(TestProcedureStateEnum state)
62     {
63         if (CurrFsm != null)
64         {
65             CurrFsm.ChangeState((sbyte)state);
66         }
67     }
68 
69     public void OnUpdate()
70     {
71         if (CurrFsm != null)
72         {
73             CurrFsm.OnUpdate();
74         }
75     }
76 
77     public void Dispose()
78     {
79         if (CurrFsm != null)
80         {
81             CurrFsm.ShutDown();
82         }
83     }
84 
85 }

 

 流程基类,拥有者就是流程管理器 TestProcedureManager

public class TestProcedureBase : TestFsmState<TestProcedureManager>
{

}

 

测试用例:

Q键:进入启动    W键:进入检查更新    E键:进入进入游戏

 1 void Update()
 2     {
 3         if (Input.GetKeyUp(KeyCode.Q))
 4         {
 5             TestGameEntry.ProcedureMgr.ChangeState(TestProcedureStateEnum.Launch);
 6         }
 7 
 8         if (Input.GetKeyUp(KeyCode.W))
 9         {
10             TestGameEntry.ProcedureMgr.ChangeState(TestProcedureStateEnum.CheckVersion);
11         }
12 
13         if (Input.GetKeyUp(KeyCode.E))
14         {
15             TestGameEntry.ProcedureMgr.ChangeState(TestProcedureStateEnum.EnterGame);
16         }
17     }

 

可看到 状态的进入 执行 离开。

 

 

是不是感觉像工厂的流水作业一样,逻辑清晰,流程解耦、一目了然。

 

posted @ 2022-05-04 10:19  赵不灰  阅读(513)  评论(0编辑  收藏  举报