Skip to content

Instantly share code, notes, and snippets.

@green224
Last active February 10, 2020 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save green224/ccb27c9427b3dceee3e5860320b8acac to your computer and use it in GitHub Desktop.
Save green224/ccb27c9427b3dceee3e5860320b8acac to your computer and use it in GitHub Desktop.
using System;
namespace P8.StateMachine {
public static class FSM< ImplMachine, ImplState >
where ImplMachine : FSM< ImplMachine, ImplState >.Machine
where ImplState : FSM< ImplMachine, ImplState >.State
{
public abstract class Machine {
public ImplState current = null;
protected Machine() {
}
virtual public void setup( ImplState first ) {
trans( first );
}
virtual public void update( float dt ) {
current?.update( dt );
}
virtual public void trans( ImplState next ) {
current?.exit( next );
var prev = current;
current = next;
current?.enter( prev );
}
}
public abstract class State {
public float tCnt => _tCnt; //!< 経過時刻
protected State( ImplMachine parent ) {
_parent = parent;
}
virtual public void enter( ImplState prev ) {
_tCnt = 0;
}
virtual public void update( float dt ) {
_tCnt += dt;
}
virtual public void exit( ImplState next ) {
}
protected ImplMachine _parent;
protected float _tCnt;
protected void trans( ImplState next ) => _parent.trans( next );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment