修改fsm

This commit is contained in:
2025-04-10 11:20:22 +08:00
parent 12f86c6eac
commit e3afd4acac
16 changed files with 229 additions and 403 deletions

View File

@@ -0,0 +1,53 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace Stary.Evo.FiniteStateMachine
{
public static class FSMInitialize
{
private static Dictionary<string, FSMController> _fsmControllers =
new Dictionary<string, FSMController>();
/// <summary>
/// 创建FSMController
/// </summary>
/// <returns>返回创建的FSMController的识别Int值</returns>
public static void CreateFSMController<T>(Transform parent) where T : FSMController
{
if (_fsmControllers.ContainsKey(typeof(T).Name))
{
Debug.LogError($"FSM控制器已存在_key:{typeof(T).Name}");
}
else
{
GameObject newController = new GameObject($"{typeof(T).Name}");
newController.transform.SetParent(parent);
var controller = newController.AddComponent<T>();
_fsmControllers.Add(typeof(T).Name, controller);
}
}
/// <summary>
/// 销毁指定FSMController
/// </summary>
/// <param name="controllerId"></param>
public static void DestroyFSMController<T>() where T : FSMController
{
if (_fsmControllers.TryGetValue(typeof(T).Name, out var controller))
{
GameObject.Destroy(controller.gameObject);
_fsmControllers.Remove(typeof(T).Name);
}
else
{
Debug.LogError("该Id的FSMController不存在或已被销毁");
}
}
}
}

View File

@@ -1,50 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace Stary.Evo.FiniteStateMachine
{
public static class FSMInitialize
{
/// <summary>
/// 状态机初始化
/// </summary>
/// <param name="targetScene">指定场景</param>
/// <returns></returns>
public static FSMManager Init(Scene targetScene)
{
GameObject targetObject = targetScene.GetRootGameObjects().FirstOrDefault(go => go.name == "FSMManger");
FSMManager fSMManager;
if (targetObject == null)
{
targetObject = new GameObject("FSMManger");
SceneManager.MoveGameObjectToScene(targetObject, targetScene);
fSMManager = new FSMManager();
fSMManager.FSMMangerObject = targetObject;
void OnSceneUnloaded(Scene scene)
{
// 当场景卸载时释放缓存
if (scene.name == targetScene.name)
{
GameObject.Destroy(targetObject);
fSMManager = null;
}
}
SceneManager.sceneUnloaded += OnSceneUnloaded;
return fSMManager;
}
else
{
Debug.LogError("场景: " + targetScene.name + "已经初始化过FSM状态机");
return null;
}
}
}
}

View File

@@ -1,150 +0,0 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Stary.Evo.FiniteStateMachine
{
public class FSMManager
{
public GameObject FSMMangerObject;
private Dictionary<int, FSMController> controllers = new Dictionary<int, FSMController>();
private Dictionary<int, SingleState> states = new Dictionary<int, SingleState>();
private int nextControllerId = 0;
private int nextStateId = 0;
/// <summary>
/// 创建FSMController
/// </summary>
/// <returns>返回创建的FSMController的识别Int值</returns>
public int CreateFSMController()
{
if (FSMMangerObject == null)
{
Debug.LogError("FSM状态机还未初始化");
return -1;
}
else
{
int id = nextControllerId++;
GameObject newController = new GameObject("FSMController" + id);
newController.transform.SetParent(FSMMangerObject.transform);
var controller = newController.AddComponent<FSMController>();
controllers[id] = controller;
return id;
}
}
/// <summary>
/// 销毁指定FSMController
/// </summary>
/// <param name="controllerId"></param>
public void DestroyFSMController(int controllerId)
{
if (controllers.TryGetValue(controllerId, out var controller))
{
GameObject.Destroy(controller.gameObject);
controllers.Remove(controllerId);
}
else
{
Debug.LogError("该Id的FSMController不存在或已被销毁");
}
}
/// <summary>
/// 退出指定Controller中的所有状态
/// </summary>
/// <param name="controllerId"></param>
public void ExitAllStateInController(int controllerId)
{
if (controllers.TryGetValue(controllerId, out var controller))
{
controller.ExitAll();
}
else
{
Debug.LogError("该Id的FSMController不存在或已被销毁");
}
}
/// <summary>
/// 创建状态
/// </summary>
/// <param name="controllerId">状态挂载的ControllerId</param>
/// <param name="stateDateAction">状态行为数据</param>
/// <returns></returns>
public int CreateState(int controllerId, StateDateAction stateDateAction)
{
if (controllers.TryGetValue(controllerId, out var controller))
{
// 创建state并将其添加到指定controller生命周期中
SingleState state = new SingleState();
state.stateDate = stateDateAction;
controller.AddState(state);
int id = nextStateId++;
states[id] = state;
return id;
}
else
{
Debug.LogError("该Id的FSMController不存在或已被销毁状态初始化失败");
return -1;
}
}
/// <summary>
/// 进入指定状态
/// </summary>
/// <param name="stateId"></param>
public void EnterState(int stateId)
{
if (states.TryGetValue(stateId, out var state))
{
state.Machine.Enter(state.GetType());
}
else
{
Debug.LogError("该Id的状态不存在");
}
}
/// <summary>
/// 离开指定状态
/// </summary>
/// <param name="stateId"></param>
public void ExitState(int stateId)
{
if (states.TryGetValue(stateId, out var state))
{
state.Machine.Exit(state.GetType());
}
else
{
Debug.LogError("该Id的状态不存在");
}
}
/// <summary>
/// 销毁指定状态
/// </summary>
/// <param name="stateId"></param>
public void DestroyState(int stateId)
{
if (states.TryGetValue(stateId, out var state))
{
states.Remove(stateId);
}
}
// 将状态添加到FSMController
public void AddStateToController(int controllerId, int stateId)
{
if (controllers.TryGetValue(controllerId, out var controller) && states.TryGetValue(stateId, out var state))
{
controller.AddState(state);
}
}
}
}

View File

@@ -0,0 +1,70 @@
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;
}
}
}