A lightweight implementation for a stacked state machine in C#. It's designed to control any actions of an NPC in a game. Part of the project is a sample console application simulating the actions of a carrier with verbose output.
Create a new instance:
var ssm = new StackedStateMachine(<inital State>);
Configure transitions:
ssm.addTransition(<typeof active State>, <typeof Event>, <delegate that returns the new state>);
Execute active state's action and control the state machine with external events:
var e = ssm.State.onGameTick();
if (e != null) ssm.raiseEvent(e);
The active state raises it, if it runs into problems. Raise it externally to abort the active action and to initiate rollback of the state stack.
The active state raises it, if the action is done. Hands over control to the previous state on the state stack.