82 lines
1.7 KiB
C#
82 lines
1.7 KiB
C#
using System.Collections.Generic;
|
|
|
|
namespace Koptilnya.StateMachine
|
|
{
|
|
public class StateMachine<T>
|
|
{
|
|
protected Dictionary<T, State<T>> mStates;
|
|
|
|
protected State<T> mCurrentState;
|
|
|
|
public StateMachine()
|
|
{
|
|
mStates = new Dictionary<T, State<T>>();
|
|
}
|
|
|
|
public void Add(State<T> state)
|
|
{
|
|
mStates.Add(state.ID, state);
|
|
}
|
|
|
|
public void Add(T stateID, State<T> state)
|
|
{
|
|
mStates.Add(stateID, state);
|
|
}
|
|
|
|
public State<T> GetState(T stateID)
|
|
{
|
|
if(mStates.ContainsKey(stateID))
|
|
return mStates[stateID];
|
|
|
|
return null;
|
|
}
|
|
|
|
public void SetCurrentState(T stateID)
|
|
{
|
|
State<T> state = mStates[stateID];
|
|
|
|
SetCurrentState(state);
|
|
}
|
|
|
|
public State<T> GetCurrentState()
|
|
{
|
|
return mCurrentState;
|
|
}
|
|
|
|
public void SetCurrentState(State<T> state)
|
|
{
|
|
if (mCurrentState == state)
|
|
{
|
|
mCurrentState.ReEnter();
|
|
}
|
|
|
|
if (mCurrentState != null)
|
|
{
|
|
mCurrentState.Exit();
|
|
}
|
|
|
|
mCurrentState = state;
|
|
|
|
if (mCurrentState != null)
|
|
{
|
|
mCurrentState.Enter();
|
|
}
|
|
}
|
|
|
|
public void Update()
|
|
{
|
|
if (mCurrentState != null)
|
|
{
|
|
mCurrentState.Update();
|
|
}
|
|
}
|
|
|
|
public void FixedUpdate()
|
|
{
|
|
if (mCurrentState != null)
|
|
{
|
|
mCurrentState.FixedUpdate();
|
|
}
|
|
}
|
|
}
|
|
} |