Unity3D有限状态机(FSM)学习笔记【3】FSState类
来源:互联网 发布:json对象合并成一个 编辑:程序博客网 时间:2024/05/16 11:08
本系列笔记转载自游戏蛮牛专栏作家Jackel的论坛文章,详细介绍了FSM的创建与使用,特与众分享。链接:http://www.manew.com/thread-37136-1-1.html
该类主要是状态的基本操作及事件的添加与触发。代码如下:
using System;using System.Collections;using System.Collections.Generic;public class FSState{protected FiniteStateMachine.EnterState mEnterDelegate;protected FiniteStateMachine.PushState mPushDelegate;protected FiniteStateMachine.PopStatemPopDelegate;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, null, this, mOwner, mEnterDelegate, mPushDelegate, mPopDelegate );mTranslationEvents.Add( eventName, newEvent );return newEvent;}public void Trigger( string name ){mTranslationEvents[ name ].Execute( null, null, null );}public void Trigger( string eventName, object param1 ){mTranslationEvents[ eventName ].Execute( param1, null, null );}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, null, this, 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, null, this, 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, null, this, 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, null, this, 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, null, this, 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, null, this, 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;}}该类定义了FiniteStateMachine中的三个委托,是一个独立的类,不继承Mono。FiniteStateMachine下一篇讲解
一、委托
1、FiniteStateMachine.EnterState
2、FiniteStateMachine.PushState
3、FiniteStateMachine.PopState
二、事件加入
事件加入有两个重要函数
1、On
public FSEvent On( string eventName ){FSEvent newEvent = new FSEvent( eventName, null, this, mOwner, mEnterDelegate, mPushDelegate, mPopDelegate );mTranslationEvents.Add( eventName, newEvent );return newEvent;}该方法用于将事件的名字和事件加入到Dictionary中。
2、Trigger
public void Trigger( string name ){mTranslationEvents[ name ].Execute( null, null, null );}改方法用于从Dictionary中取出事件的触发函数,去执行相应的操作。
0 0
- Unity3D有限状态机(FSM)学习笔记【3】FSState类
- Unity3D有限状态机(FSM)学习笔记【4】FiniteStateMachine类
- Unity3D有限状态机(FSM)学习笔记【5】FSEvent类
- Unity3D有限状态机(FSM)学习笔记【6】Dispatcher类
- Unity3D有限状态机(FSM)学习笔记【1】有限状态机总述
- Unity3D有限状态机(FSM)学习笔记【2】IState
- Unity3D有限状态机(FSM)学习笔记【7】使用实例
- fsm(有限状态机)入门笔记
- 有限状态机(FSM)学习总结
- 有限状态机(FSM)学习总结
- Unity3D-FSM有限状态机
- 算法学习笔记(九)有限状态机 FSM 的应用
- 关于FSM(有限状态机)的学习3---总结
- 关于FSM(有限状态机)的学习2
- FSM有限状态机学习
- Unity3D架构系列之-FSM有限状态机设计(一)
- Unity3D架构系列之- FSM有限状态机设计(二)
- Unity3D架构系列之- FSM有限状态机设计(三)
- uImage,zimage,bzimage,vmlinux,vmlinuz
- Jetty JNDI开发实战(上)
- Python 列表学习与使用
- Ubuntu 10.04 速配指南
- C语言基础知识之(十四):指针和字符数组、字符串数组
- Unity3D有限状态机(FSM)学习笔记【3】FSState类
- static详解
- HDU 4458 Shoot the Airplane (2012年杭州赛区现场赛F题)
- uva 11292 好长的英语题(简单的题目)
- android-屏幕适配(一)
- Linux进程间通信---有名管道 代码实现
- java并发编程第六章(3)使用阻塞式线程安全列表
- Codeforces Round #318(Div. 2) A
- CodeForces #318 (div1) C.Bear and Drawing