You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
2.1 KiB
94 lines
2.1 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Sog
|
|
{
|
|
public class StateMachine<Entity>
|
|
{
|
|
private Entity m_Owner;
|
|
|
|
private State<Entity> m_oCurrentState;
|
|
private State<Entity> m_oPreviousState;
|
|
private State<Entity> m_oGlobalState;
|
|
|
|
public StateMachine(Entity Obj)
|
|
{
|
|
m_Owner = Obj;
|
|
m_oCurrentState = null;
|
|
m_oPreviousState = null;
|
|
m_oGlobalState = null;
|
|
}
|
|
|
|
|
|
public void SetCurrentState(State<Entity> s)
|
|
{
|
|
m_oCurrentState = s;
|
|
}
|
|
|
|
public void SetPreviousState(State<Entity> s)
|
|
{
|
|
m_oPreviousState = s;
|
|
}
|
|
|
|
public void SetGlobalState(State<Entity> s)
|
|
{
|
|
m_oGlobalState = s;
|
|
}
|
|
|
|
public void Update()
|
|
{
|
|
if (m_oGlobalState != null)
|
|
{
|
|
m_oGlobalState.Excute(m_Owner);
|
|
}
|
|
|
|
if (m_oCurrentState != null)
|
|
{
|
|
m_oCurrentState.Excute(m_Owner);
|
|
}
|
|
}
|
|
|
|
public void ChangeState(State<Entity> NewState)
|
|
{
|
|
if (NewState == null)
|
|
{
|
|
return;
|
|
}
|
|
if(m_oCurrentState != null)
|
|
{
|
|
m_oCurrentState.Exit(m_Owner);
|
|
m_oPreviousState = m_oCurrentState;
|
|
}
|
|
|
|
m_oCurrentState = NewState;
|
|
m_oCurrentState.Enter(m_Owner);
|
|
}
|
|
|
|
public void RevertToPreviousState() // 如果用来翻转状态, reload时m_oPreviousState也需要重新set
|
|
{
|
|
ChangeState(m_oPreviousState);
|
|
}
|
|
|
|
public bool IsInState(State<Entity> s)
|
|
{
|
|
return m_oCurrentState.Equals(s);
|
|
}
|
|
|
|
public State<Entity> CurrentState()
|
|
{
|
|
return m_oCurrentState;
|
|
}
|
|
|
|
public State<Entity> PreviousState()
|
|
{
|
|
return m_oPreviousState;
|
|
}
|
|
|
|
public State<Entity> GlobalState()
|
|
{
|
|
return m_oGlobalState;
|
|
}
|
|
}
|
|
}
|
|
|