Unity3D架构系列之- FSM有限状态机设计(三)

在设计二中,我们实现了有限状态机管理类,接下来,我们实现FSState这个类,这里类主要是状态的基本操作以及事件触发。在这里我们定义了在FiniteStateMachine类里声明的三个委托。在FSState里面使用的代码如下:

1
2
3
protected FiniteStateMachine.EnterState mEnterDelegate;
protected FiniteStateMachine.PushState mPushDelegate;
protected FiniteStateMachine.PopState mPopDelegate;

这个FSState是独立的一个类,不继承Mono,我们定义了一个构造函数,将我们的委托进行了初始化:

1
2
3
4
5
6
7
8
9
10
public FSState(IState obj, FiniteStateMachine owner, string name, FiniteStateMachine.EnterState e, FiniteStateMachine.PushState pu, FiniteStateMachine.PopState po) {
                mStateObject = obj;
                mStateName = name;
                mOwner = owner;
                mEnterDelegate = e;
                mPushDelegate = pu;
                mPopDelegate = po;
                mTranslationEvents = new Dictionary<string, FSEvent>();
 
        }

我们声明了FSEvent事件处理函数用于将事件的名字和事件加入的Dictionary里面

1
2
3
4
5
6
public FSEvent On(string eventName) {
                FSEvent newEvent = new FSEvent(eventName, nullthis, mOwner, mEnterDelegate, mPushDelegate, mPopDelegate);
                mTranslationEvents.Add(eventName, newEvent);
                return newEvent;
 
        }

加入到列表后,我们需要从表里面取出去执行,这就需要Trigger触发函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public void Trigger(string name) {
                mTranslationEvents[name].Execute(nullnullnull);
        }
 
 
        public void Trigger(string eventName, object param1) {
                mTranslationEvents[eventName].Execute(param1, nullnull);
        }
         
        public void Trigger(string eventName, object param1, object param2) {
                mTranslationEvents[eventName].Execute(param1, param2, null);
        }
         
        public void Trigger(string eventName, object param1, object param2, object param3) {
                mTranslationEvents[eventName].Execute(param1, param2, param3);
 
        }

以上也是FSState类的核心代码,闲话少说,FSState类的整个代码,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.Collections;
using System.Collections.Generic;
 
public class FSState {
    protected FiniteStateMachine.EnterState mEnterDelegate;
    protected FiniteStateMachine.PushState mPushDelegate;
    protected FiniteStateMachine.PopState mPopDelegate;
 
    protected IState mStateObject;
    protected string mStateName;
    protected FiniteStateMachine mOwner;
    protected Dictionary<string, FSEvent> mTranslationEvents;
 
    public FSState(IState obj, FiniteStateMachine owner, string name, FiniteStateMachine.EnterState e, FiniteStateMachine.PushState pu, FiniteStateMachine.PopState po) {
        mStateObject = obj;
        mStateName = name;
        mOwner = owner;
        mEnterDelegate = e;
        mPushDelegate = pu;
        mPopDelegate = po;
        mTranslationEvents = new Dictionary<string, FSEvent>();
    }
 
    public IState StateObject {
        get {
            return mStateObject;
        }
    }
 
    public string StateName {
        get {
            return mStateName;
        }
    }
 
    public FSEvent On(string eventName) {
        FSEvent newEvent = new FSEvent(eventName, nullthis, mOwner, mEnterDelegate, mPushDelegate, mPopDelegate);
        mTranslationEvents.Add(eventName, newEvent);
        return newEvent;
    }
 
    public void Trigger(string name) {
        mTranslationEvents[name].Execute(nullnullnull);
    }
 
    public void Trigger(string eventName, object param1) {
        mTranslationEvents[eventName].Execute(param1, nullnull);
    }
     
    public void Trigger(string eventName, object param1, object param2) {
        mTranslationEvents[eventName].Execute(param1, param2, null);
    }
     
    public void Trigger(string eventName, object param1, object param2, object param3) {
        mTranslationEvents[eventName].Execute(param1, param2, param3);
    }
 
     
    public FSState On<T>(string eventName, Func<T, bool> action) {
        FSEvent newEvent = new FSEvent(eventName, nullthis, mOwner, mEnterDelegate, mPushDelegate, mPopDelegate);
        newEvent.mAction = delegate (object o1, object o2, object o3) {
            T param1;
            try { param1 = (T)o1; } catch { param1 = default(T); }
            action(param1);
            return true;
        };
        mTranslationEvents.Add(eventName, newEvent);
        return this;
    }
     
    public FSState On<T>(string eventName, Action<T> action) {
        FSEvent newEvent = new FSEvent(eventName, nullthis, mOwner, mEnterDelegate, mPushDelegate, mPopDelegate);
        newEvent.mAction = delegate (object o1, object o2, object o3) {
            T param1;
            try { param1 = (T)o1; } catch { param1 = default(T); }
            action(param1);
            return true;
        };
        mTranslationEvents.Add(eventName, newEvent);
        return this;
    }
     
    public FSState On<T1, T2>(string eventName, Func<T1, T2, bool> action) {
        FSEvent newEvent = new FSEvent(eventName, nullthis, mOwner, mEnterDelegate, mPushDelegate, mPopDelegate);
        newEvent.mAction = delegate (object o1, object o2, object o3) {
            T1 param1;
            T2 param2;
            try { param1 = (T1)o1; } catch { param1 = default(T1); }
            try { param2 = (T2)o2; } catch { param2 = default(T2); }
            action(param1, param2);
            return true;
        };
        mTranslationEvents.Add(eventName, newEvent);
        return this;
    }
     
    public FSState On<T1, T2>(string eventName, Action<T1, T2> action) {
        FSEvent newEvent = new FSEvent(eventName, nullthis, mOwner, mEnterDelegate, mPushDelegate, mPopDelegate);
        newEvent.mAction = delegate (object o1, object o2, object o3) {
            T1 param1;
            T2 param2;
            try { param1 = (T1)o1; } catch { param1 = default(T1); }
            try { param2 = (T2)o2; } catch { param2 = default(T2); }
            action(param1, param2);
            return true;
        };
        mTranslationEvents.Add(eventName, newEvent);
        return this;
    }
 
    public FSState On<T1, T2, T3>(string eventName, Func<T1, T2, T3, bool> action) {
        FSEvent newEvent = new FSEvent(eventName, nullthis, mOwner, mEnterDelegate, mPushDelegate, mPopDelegate);
        newEvent.mAction = delegate (object o1, object o2, object o3) {
            T1 param1;
            T2 param2;
            T3 param3;
            try { param1 = (T1)o1; } catch { param1 = default(T1); }
            try { param2 = (T2)o2; } catch { param2 = default(T2); }
            try { param3 = (T3)o3; } catch { param3 = default(T3); }
            action(param1, param2, param3);
            return true;
        };
        mTranslationEvents.Add(eventName, newEvent);
        return this;
    }
     
    public FSState On<T1, T2, T3>(string eventName, Action<T1, T2, T3> action) {
        FSEvent newEvent = new FSEvent(eventName, nullthis, mOwner, mEnterDelegate, mPushDelegate, mPopDelegate);
        newEvent.mAction = delegate (object o1, object o2, object o3) {
            T1 param1;
            T2 param2;
            T3 param3;
            try { param1 = (T1)o1; } catch { param1 = default(T1); }
            try { param2 = (T2)o2; } catch { param2 = default(T2); }
            try { param3 = (T3)o3; } catch { param3 = default(T3); }
            action(param1, param2, param3);
            return true;
        };
        mTranslationEvents.Add(eventName, newEvent);
        return this;
    }
}

接下来会在设计四中继续讲解 FSEvent类

转自:http://jxwgame.blog.51cto.com/943299/1610360

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值