2025-04-10 11:20:22 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Stary.Evo.FiniteStateMachine
|
|
|
|
|
|
{
|
2025-04-10 11:46:42 +08:00
|
|
|
|
public interface IFsmSystem
|
2025-04-10 11:20:22 +08:00
|
|
|
|
{
|
|
|
|
|
|
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; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-10 11:46:42 +08:00
|
|
|
|
public class FsmSystem : IFsmSystem
|
2025-04-10 11:20:22 +08:00
|
|
|
|
{
|
|
|
|
|
|
public IFSMIState CurState { get; set; }
|
2025-04-10 11:46:42 +08:00
|
|
|
|
public Dictionary<string, IFSMIState> States { get; set; } = new();
|
2025-04-10 11:20:22 +08:00
|
|
|
|
|
|
|
|
|
|
public void AddState(IFSMIState state)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log(state.Name);
|
2025-04-10 11:46:42 +08:00
|
|
|
|
if (!States.ContainsKey(state.Name))
|
2025-04-10 11:20:22 +08:00
|
|
|
|
{
|
|
|
|
|
|
States.Add(state.Name, state);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-04-10 11:57:13 +08:00
|
|
|
|
Debug.LogErrorFormat("UnityEvo:States状态机容器里已存在名字为--【{0}】--的状态", state.Name.ToString());
|
2025-04-10 11:20:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void RemoveState(IFSMIState state)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (States.ContainsKey(state.Name))
|
|
|
|
|
|
{
|
|
|
|
|
|
States.Remove(state.Name);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-04-10 11:57:13 +08:00
|
|
|
|
Debug.LogErrorFormat("UnityEvo:States状态机容器里不存在名字为--【{0}】--的状态", state.Name.ToString());
|
2025-04-10 11:20:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|