[FONT="]public class [/FONT][COLOR=#2B91AF][FONT=Consolas]SensorDevice [/FONT][/COLOR][FONT="]: MonoBehaviour
{
[/FONT
][FONT=Consolas
] [COLOR=
blue]protected[/COLOR
] [COLOR=
blue]enum[/COLOR
] [COLOR=
#2B91AF]States[/COLOR][/FONT]
[FONT=Consolas] {[/FONT]
[FONT=Consolas] Uninitialised,[/FONT]
[FONT=Consolas] Initialising,[/FONT]
[FONT=Consolas] Reinitialise,[/FONT]
[FONT=Consolas] Reading,[/FONT]
[FONT=Consolas] ShuttingDown,[/FONT]
[FONT=Consolas] }[/FONT]
[FONT=Consolas
] [COLOR=
blue]protected[/COLOR
] [COLOR=
blue]enum[/COLOR
] [COLOR=
#2B91AF]Triggers[/COLOR][/FONT]
[FONT=Consolas] {[/FONT]
[FONT=Consolas] Initialise,[/FONT]
[FONT=Consolas] ReadData,[/FONT]
[FONT=Consolas] Shutdown,[/FONT]
[FONT=Consolas] Unplugged,[/FONT]
[FONT=Consolas] StartOver,[/FONT]
[FONT=Consolas] FailInitialisation[/FONT]
[FONT=Consolas] }[/FONT]
[FONT=Consolas
] [COLOR=
blue]protected[/COLOR
] [COLOR=
#2B91AF]StateMachine[/COLOR]<[COLOR=#2B91AF]States[/COLOR], [COLOR=#2B91AF]Triggers[/COLOR]> m_stateMachine;[/FONT]
[FONT=Consolas] // set up our state machine[/FONT]
[/FONT][FONT="]{[/FONT]
[FONT="] // no such problem with making a generic class now[/FONT]
[FONT=Consolas
] m_stateMachine =
[COLOR=
blue]new[/COLOR
] [COLOR=
#2B91AF]StateMachine[/COLOR]<[COLOR=#2B91AF]States[/COLOR], [COLOR=#2B91AF]Triggers[/COLOR]>([COLOR=#2B91AF]States[/COLOR].Uninitialised);[/FONT]
[FONT=Consolas] m_stateMachine.Configure([COLOR=#2B91AF]States[/COLOR].Uninitialised)[/FONT]
[FONT=Consolas] .PermitReentry([COLOR=#2B91AF]Triggers[/COLOR].Shutdown)[/FONT]
[FONT=Consolas] .Permit([COLOR=#2B91AF]Triggers[/COLOR].Initialise, [COLOR=#2B91AF]States[/COLOR].Initialising);[/FONT]
[FONT=Consolas] m_stateMachine.Configure([COLOR=#2B91AF]States[/COLOR].Initialising)[/FONT]
[FONT=Consolas] .OnEntry(() => StateEntry_Initialising())[/FONT]
[FONT=Consolas] .Permit([COLOR=#2B91AF]Triggers[/COLOR].FailInitialisation, [COLOR=#2B91AF]States[/COLOR].Reinitialise)[/FONT]
[FONT=Consolas] .Permit([COLOR=#2B91AF]Triggers[/COLOR].Shutdown, [COLOR=#2B91AF]States[/COLOR].ShuttingDown)[/FONT]
[FONT=Consolas] .Permit([COLOR=#2B91AF]Triggers[/COLOR].ReadData, [COLOR=#2B91AF]States[/COLOR].Reading);[/FONT]
[FONT=Consolas] m_stateMachine.Configure([COLOR=#2B91AF]States[/COLOR].Reinitialise)[/FONT]
[FONT=Consolas] .OnEntry(() => StateEntry_Reinitialise())[/FONT]
[FONT=Consolas] .Permit([COLOR=#2B91AF]Triggers[/COLOR].Shutdown, [COLOR=#2B91AF]States[/COLOR].ShuttingDown)[/FONT]
[FONT=Consolas] .Permit([COLOR=#2B91AF]Triggers[/COLOR].Initialise, [COLOR=#2B91AF]States[/COLOR].Initialising);[/FONT]
[FONT=Consolas] m_stateMachine.Configure([COLOR=#2B91AF]States[/COLOR].Reading)[/FONT]
[FONT=Consolas] .Permit([COLOR=#2B91AF]Triggers[/COLOR].Shutdown, [COLOR=#2B91AF]States[/COLOR].ShuttingDown)[/FONT]
[FONT=Consolas] .Permit([COLOR=#2B91AF]Triggers[/COLOR].Unplugged, [COLOR=#2B91AF]States[/COLOR].Reinitialise);[/FONT]
[FONT=Consolas] m_stateMachine.Configure([COLOR=#2B91AF]States[/COLOR].ShuttingDown)[/FONT]
[FONT=Consolas] .OnEntry(() => StateEntry_ShuttingDown())[/FONT]
[FONT=Consolas] .Permit([COLOR=#2B91AF]Triggers[/COLOR].StartOver, [COLOR=#2B91AF]States[/COLOR].Uninitialised);[/FONT]
[FONT="]}[/FONT]
[FONT="] protected void StateEntry_Initialising()[/FONT]
[FONT="] {[/FONT]
[FONT="] // initialiise the sensor device upon state entry[/FONT]
[FONT="] }[/FONT]
[FONT="] void StateEntry_ReInitialise()[/FONT]
[FONT="] {[/FONT]
[FONT="] // reinitialise the sensor device upon state entry[/FONT]
[FONT="] }[/FONT]
[FONT="] void StateEntry_ShuttingDown()[/FONT]
[FONT="] {[/FONT]
[FONT="] // shut down the sensor device[/FONT]
[FONT="] }[/FONT]
[FONT="] // let's make some functions to expose just those bits of our state machine we want to show to the rest of the world[/FONT]
[FONT="] public void InitialiseSensor()[/FONT]
[FONT="] {[/FONT]
[FONT=Consolas] m_stateMachine.Fire([COLOR=#2B91AF]Triggers[/COLOR].Initialise);[/FONT]
[FONT=Consolas] }[/FONT]
[FONT="] public void ShutdownSensor()[/FONT]
[FONT="] {[/FONT]
[FONT=Consolas] m_stateMachine.Fire([COLOR=#2B91AF]Triggers[/COLOR].Shutdown);[/FONT]
[FONT=Consolas] }[/FONT]
[FONT="]// and some basic code to interact with the state machine[/FONT]
[FONT="]{[/FONT]
[FONT="
] void OnGUI()[/FONT
]
[FONT="] {[/FONT]
[FONT="
] if (GUI.
Button(new Rect(25,
25,
200,
50),
"Initialise") ==
true)[/FONT
]
[FONT="] {[/FONT]
[FONT="] SensorDevice device = GetComponent<SensorDevice>();[/FONT]
[FONT="] device.InitialiseSensor();[/FONT]
[FONT="] }[/FONT]
[FONT="
] if (GUI.
Button(new Rect(25,
100,
200,
50),
"Shutdown") ==
true)[/FONT
]
[FONT="] {[/FONT]
[FONT="] SensorDevice device = GetComponent<SensorDevice>();[/FONT]
[FONT="] device.ShutdownSensor();[/FONT]
[FONT="] }[/FONT]
[FONT="] }[/FONT]
[FONT="]}[/FONT]