Last active
February 10, 2020 17:14
-
-
Save green224/ccb27c9427b3dceee3e5860320b8acac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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