70 lines
1.9 KiB
C#
70 lines
1.9 KiB
C#
|
|
using System.Collections.Generic;
|
|||
|
|
using UnityEngine;
|
|||
|
|
|
|||
|
|
namespace Stary.Evo.FiniteStateMachine
|
|||
|
|
{
|
|||
|
|
public interface IFSMManager
|
|||
|
|
{
|
|||
|
|
IFSMIState CurState { get; set; }
|
|||
|
|
void AddState(IFSMIState state);
|
|||
|
|
void RemoveState(IFSMIState state);
|
|||
|
|
void SetCurState(string name);
|
|||
|
|
IFSMIState GetStateWithName(string name);
|
|||
|
|
Dictionary<string, IFSMIState> States { get; set; }
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public class FSMManager : IFSMManager
|
|||
|
|
{
|
|||
|
|
public IFSMIState CurState { get; set; }
|
|||
|
|
public Dictionary<string, IFSMIState> States { get; set; }
|
|||
|
|
|
|||
|
|
public FSMManager()
|
|||
|
|
{
|
|||
|
|
States = new Dictionary<string, IFSMIState>();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void AddState(IFSMIState state)
|
|||
|
|
{
|
|||
|
|
Debug.Log(state.Name);
|
|||
|
|
if (!States.ContainsKey(state.Name))
|
|||
|
|
{
|
|||
|
|
States.Add(state.Name, state);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
Debug.LogErrorFormat("States状态机容器里已存在名字为--【{0}】--的状态", state.Name.ToString());
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void RemoveState(IFSMIState state)
|
|||
|
|
{
|
|||
|
|
if (States.ContainsKey(state.Name))
|
|||
|
|
{
|
|||
|
|
States.Remove(state.Name);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
Debug.LogErrorFormat("States状态机容器里不存在名字为--【{0}】--的状态", state.Name.ToString());
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void SetCurState(string name)
|
|||
|
|
{
|
|||
|
|
if (CurState != null)
|
|||
|
|
CurState.OnExit();
|
|||
|
|
IFSMIState state = GetStateWithName(name);
|
|||
|
|
CurState = state;
|
|||
|
|
CurState.OnEnter();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public IFSMIState GetStateWithName(string name)
|
|||
|
|
{
|
|||
|
|
if (States.ContainsKey(name))
|
|||
|
|
{
|
|||
|
|
return States[name];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|