3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【C#】シンプルな有限ステートマシンの実装。

Last updated at Posted at 2020-09-19

久しぶりの投稿になります.

#実装

有限ステートマシンの実装になります.

StateMachine.cs
/// <summary>
/// 有限ステートマシン(FSM), where T : class
/// </summary>
public class StateMachine<T> 
    where T : class
{
    private T m_Owner;
    private State<T> m_CurrentState;
    public State<T> currentState { get {return m_CurrentState;} set{ m_CurrentState = value;} }
    private State<T> m_PreviousState;
    public State<T> previousState { get {return m_PreviousState;} set{ m_PreviousState = value;} }
    private State<T> m_GlobalState;
    public State<T> globalState { get {return m_GlobalState;} set{ m_GlobalState = value;} }
    /// <summary>
    /// コンストラクタ
    /// </summary>
    public StateMachine(T owner){
        m_Owner = owner;
        m_CurrentState = new NullState<T>();
        m_PreviousState = new NullState<T>();
        m_GlobalState = new NullState<T>();
    }
    /// <summary>
    /// 現在の状態を実行する
    /// </summary>
    public void Update(){
        m_GlobalState.Execute(m_Owner);
        m_CurrentState.Execute(m_Owner);
    }
    /// <summary>
    /// 現在のStateを変更する
    /// </summary>
    public void ChangeState(State<T> newState){
        // Assert(newState != null);
        m_PreviousState = m_CurrentState;
        m_CurrentState.Exit(m_Owner);
        m_CurrentState = newState;
        m_CurrentState.Enter(m_Owner);
    }

    /// <summary>
    /// 前のStateに変更する
    /// </summary>
    public void RevertToPreviousState(){
        ChangeState(m_PreviousState);
    }
}
    

ステートの実装になります.

State.cs
public interface State<T>
    where T : class
{
    /// <summary>
    /// このStateになった時に呼ばれる
    /// </summary>
    void Enter(T t);
    /// <summary>
    /// このState中はずっと呼ばれる
    /// </summary>
    void Execute(T t);
    /// <summary>
    /// このStateから変わる時に呼ばれる
    /// </summary>
    void Exit(T t);
}

ヌルステートの実装になります.

NullState.cs
/// <summary>
/// null避けのためのクラス
/// </summary>
public class NullState<T>: State<T> 
    where T : class
{
    public void Enter(T t){}
    public void Execute(T t){}
    public void Exit(T t){}
}

#使用方法

Main.cs
void main(){
    // 遷移を管理するクラス
    MyClass myClass = new MyClass();
    // ステートマシンの宣言
    StateMachine<MyClass> stateMachine = new StateMachine<MyClass>(myClass);
    // stateインターフェースを実装したクラス
    State<MyClass> myState = new MyState();
    // 現在の状態を変更
    stateMachine.ChangeState(myState);
    // 現在のステートを実行
    stateMachine.Update();
}
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?