修改fsm
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
fileFormatVersion: 2
|
fileFormatVersion: 2
|
||||||
<<<<<<<< HEAD:Assets/05.TableTextConversion/RunTime/Abstract.meta
|
<<<<<<<< HEAD:Assets/05.TableTextConversion/RunTime/Abstract.meta
|
||||||
guid: 3f4958b2707b1874fb59e045842881b7
|
guid: d5e1ba5da7a7031428c195b18d385b42
|
||||||
========
|
========
|
||||||
guid: a4c1b2e6dc7695646be9fd2a98863529
|
guid: a4c1b2e6dc7695646be9fd2a98863529
|
||||||
>>>>>>>> 03.FiniteStateMachine:Assets/03.FiniteStateMachine/RunTime/Abstract.meta
|
>>>>>>>> 03.FiniteStateMachine:Assets/03.FiniteStateMachine/RunTime/Abstract.meta
|
||||||
|
|||||||
@@ -4,84 +4,28 @@ using UnityEngine;
|
|||||||
|
|
||||||
namespace Stary.Evo.FiniteStateMachine
|
namespace Stary.Evo.FiniteStateMachine
|
||||||
{
|
{
|
||||||
public interface IState
|
public interface IFSMIState
|
||||||
{
|
{
|
||||||
public FSMController Machine { get; set; }
|
IFSMManager FsmManager { get; }
|
||||||
|
string Name { get; }
|
||||||
public void OnEnter();
|
public void OnEnter();
|
||||||
public void OnExit();
|
public void OnExit();
|
||||||
public void Update();
|
public void Update();
|
||||||
public void FixedUpdate();
|
public void FixedUpdate();
|
||||||
public void OnInit();
|
|
||||||
public void OnDestory();
|
public void OnDestory();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract class AbstractState : IState
|
public abstract class AbstractState : IFSMIState
|
||||||
{
|
{
|
||||||
public FSMController Machine { get; set; }
|
public IFSMManager FsmManager { get; }
|
||||||
|
public string Name { get; }
|
||||||
public abstract void OnEnter();
|
public abstract void OnEnter();
|
||||||
public abstract void OnExit();
|
public abstract void OnExit();
|
||||||
public abstract void Update();
|
public abstract void Update();
|
||||||
public abstract void FixedUpdate();
|
public abstract void FixedUpdate();
|
||||||
public abstract void OnInit();
|
|
||||||
public abstract void OnDestory();
|
public abstract void OnDestory();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 状态中具体行为:
|
|
||||||
/// (OnInitAction:初始化)
|
|
||||||
/// (OnEnterAction:进入)
|
|
||||||
/// (UpdateAction:循环)
|
|
||||||
/// (FixedUpdateAction:后循环)
|
|
||||||
/// (OnExitAction:离开)
|
|
||||||
/// (OnDestoryAction:销毁)
|
|
||||||
/// </summary>
|
|
||||||
public struct StateDateAction
|
|
||||||
{
|
|
||||||
public System.Action OnEnterAction;
|
|
||||||
public System.Action OnExitAction;
|
|
||||||
public System.Action UpdateAction;
|
|
||||||
public System.Action FixedUpdateAction;
|
|
||||||
public System.Action OnInitAction;
|
|
||||||
public System.Action OnDestoryAction;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 单个状态
|
|
||||||
/// </summary>
|
|
||||||
public class SingleState : AbstractState
|
|
||||||
{
|
|
||||||
public StateDateAction stateDate;
|
|
||||||
public bool IsRunning;
|
|
||||||
|
|
||||||
public override void FixedUpdate()
|
|
||||||
{
|
|
||||||
stateDate.FixedUpdateAction?.Invoke();
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void OnDestory()
|
|
||||||
{
|
|
||||||
stateDate.OnDestoryAction?.Invoke();
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void OnEnter()
|
|
||||||
{
|
|
||||||
stateDate.OnEnterAction?.Invoke();
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void OnExit()
|
|
||||||
{
|
|
||||||
stateDate.OnExitAction?.Invoke();
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void OnInit()
|
|
||||||
{
|
|
||||||
stateDate.OnInitAction?.Invoke();
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void Update()
|
|
||||||
{
|
|
||||||
stateDate.UpdateAction?.Invoke();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
fileFormatVersion: 2
|
fileFormatVersion: 2
|
||||||
<<<<<<<< HEAD:Assets/05.TableTextConversion/RunTime/Base.meta
|
<<<<<<<< HEAD:Assets/05.TableTextConversion/RunTime/Base.meta
|
||||||
guid: f458924b47972e344ab95e73c17cec83
|
guid: 0cf7b52f32e49cc4ba7a0727dbab7f3f
|
||||||
========
|
========
|
||||||
guid: 482775b04d27d6644bc8d880e135e6ac
|
guid: 482775b04d27d6644bc8d880e135e6ac
|
||||||
>>>>>>>> 03.FiniteStateMachine:Assets/03.FiniteStateMachine/RunTime/Base.meta
|
>>>>>>>> 03.FiniteStateMachine:Assets/03.FiniteStateMachine/RunTime/Base.meta
|
||||||
|
|||||||
@@ -1,135 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using UnityEngine;
|
|
||||||
|
|
||||||
namespace Stary.Evo.FiniteStateMachine
|
|
||||||
{
|
|
||||||
public class FSMController : MonoBehaviour
|
|
||||||
{
|
|
||||||
private Dictionary<Type, SingleState> states = new Dictionary<Type, SingleState>();
|
|
||||||
|
|
||||||
public void AddState(SingleState state)
|
|
||||||
{
|
|
||||||
var type = state.GetType();
|
|
||||||
if (!states.ContainsKey(type))
|
|
||||||
{
|
|
||||||
// 添加时初始化
|
|
||||||
state.Machine = this;
|
|
||||||
state.OnInit();
|
|
||||||
states.Add(type, state);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Debug.LogError("该FSMController中已经添加过该状态!!!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* //public bool IsRunning<T>() where T : SingleState => IsRunning(typeof(T));
|
|
||||||
|
|
||||||
//public void Enter<T>() where T : SingleState => Enter(typeof(T));
|
|
||||||
|
|
||||||
//public void Exit<T>() where T : SingleState => Exit(typeof(T));
|
|
||||||
|
|
||||||
//public void ForceEnter<T>() where T : SingleState => ForceEnter(typeof(T));*/
|
|
||||||
|
|
||||||
public bool IsRunning(Type type)
|
|
||||||
{
|
|
||||||
if (states.ContainsKey(type))
|
|
||||||
{
|
|
||||||
if (states[type].IsRunning) return true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Debug.LogError("状态已不存在!!!");
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Enter(Type type)
|
|
||||||
{
|
|
||||||
if (states.ContainsKey(type))
|
|
||||||
{
|
|
||||||
if (IsRunning(type))
|
|
||||||
{
|
|
||||||
Debug.LogError("该状态正在进行,无法再次进入!!!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ForceEnter(type);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Debug.LogError("状态已不存在!!!");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ForceEnter(Type type)
|
|
||||||
{
|
|
||||||
if (states.ContainsKey(type))
|
|
||||||
{
|
|
||||||
SingleState state = states[type];
|
|
||||||
state.OnEnter();
|
|
||||||
state.IsRunning = true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Debug.LogError("状态已不存在!!!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Exit(Type type)
|
|
||||||
{
|
|
||||||
if (states.TryGetValue(type, out SingleState state))
|
|
||||||
{
|
|
||||||
if (!IsRunning(type)) return;
|
|
||||||
state.IsRunning = false;
|
|
||||||
state.OnExit();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Debug.LogError("状态已不存在!!!");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ExitAll()
|
|
||||||
{
|
|
||||||
foreach (var state in states)
|
|
||||||
{
|
|
||||||
if (state.Value.IsRunning) Exit(state.Key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void Update()
|
|
||||||
{
|
|
||||||
foreach (var state in states)
|
|
||||||
{
|
|
||||||
if (state.Value.IsRunning) state.Value.Update();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void FixedUpdate()
|
|
||||||
{
|
|
||||||
foreach (var state in states)
|
|
||||||
{
|
|
||||||
if (state.Value.IsRunning) state.Value.FixedUpdate();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnDestroy()
|
|
||||||
{
|
|
||||||
ExitAll();
|
|
||||||
// 遍历字典中的每个状态
|
|
||||||
foreach (var key in states.Keys.ToList())
|
|
||||||
{
|
|
||||||
states[key].OnDestory();
|
|
||||||
// 将每个值设置为 null,释放引用
|
|
||||||
states[key] = null;
|
|
||||||
}
|
|
||||||
// 清空字典
|
|
||||||
states.Clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
93
Assets/03.FiniteStateMachine/RunTime/Base/IFSMController.cs
Normal file
93
Assets/03.FiniteStateMachine/RunTime/Base/IFSMController.cs
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace Stary.Evo.FiniteStateMachine
|
||||||
|
{
|
||||||
|
public interface IFSMController
|
||||||
|
{
|
||||||
|
void Initialize();
|
||||||
|
void AddState(IFSMIState state);
|
||||||
|
void RemoveState(IFSMIState state);
|
||||||
|
}
|
||||||
|
public abstract class FSMController : MonoBehaviour,IFSMController
|
||||||
|
{
|
||||||
|
private IFSMManager manager;
|
||||||
|
|
||||||
|
|
||||||
|
public void Start()
|
||||||
|
{
|
||||||
|
manager = new FSMManager();
|
||||||
|
Initialize();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddState(IFSMIState state)
|
||||||
|
{
|
||||||
|
if (manager != null)
|
||||||
|
{
|
||||||
|
manager.AddState(state);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Debug.LogError("FSMManager未初始化!!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void RemoveState(IFSMIState state)
|
||||||
|
{
|
||||||
|
if (manager != null)
|
||||||
|
{
|
||||||
|
manager.RemoveState(state);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Debug.LogError("FSMManager未初始化!!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// FSM在此类中初始化,进行状态的添加等操作
|
||||||
|
/// </summary>
|
||||||
|
public abstract void Initialize();
|
||||||
|
private void Update()
|
||||||
|
{
|
||||||
|
if (manager != null)
|
||||||
|
{
|
||||||
|
manager.CurState.Update();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Debug.LogError("FSMManager未初始化!!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void FixedUpdate()
|
||||||
|
{
|
||||||
|
if (manager != null)
|
||||||
|
{
|
||||||
|
manager.CurState.FixedUpdate();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Debug.LogError("FSMManager未初始化!!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnDestroy()
|
||||||
|
{
|
||||||
|
if (manager != null)
|
||||||
|
{
|
||||||
|
foreach (var state in manager. States)
|
||||||
|
{
|
||||||
|
state.Value.OnDestory();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Debug.LogError("FSMManager未初始化!!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
{
|
{
|
||||||
"name": "FiniteStateMachine.RunTime"
|
"name": "FiniteStateMachine.RunTime",
|
||||||
|
"references":[ "GUID:d1a793c2b6959e04ea45b972eaa369c8" ]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
fileFormatVersion: 2
|
fileFormatVersion: 2
|
||||||
<<<<<<<< HEAD:Assets/05.TableTextConversion/Editor/Excel/I18N.meta
|
<<<<<<<< HEAD:Assets/05.TableTextConversion/Editor/Excel/I18N.meta
|
||||||
guid: a0ddbcdd27271534b8277ca2bddffca7
|
guid: 24734379547c0af4bab49f6e45d74d94
|
||||||
folderAsset: yes
|
folderAsset: yes
|
||||||
timeCreated: 1531104110
|
timeCreated: 1531104110
|
||||||
licenseType: Pro
|
licenseType: Pro
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
fileFormatVersion: 2
|
fileFormatVersion: 2
|
||||||
<<<<<<<< HEAD:Assets/05.TableTextConversion/RunTime/Use.meta
|
<<<<<<<< HEAD:Assets/05.TableTextConversion/RunTime/Use.meta
|
||||||
guid: 102768b9489625641a18c187eac264be
|
guid: d7a613213ec10e649a2923aa72919d23
|
||||||
========
|
========
|
||||||
guid: bacf497398db3124994cae839e813842
|
guid: bacf497398db3124994cae839e813842
|
||||||
>>>>>>>> 03.FiniteStateMachine:Assets/03.FiniteStateMachine/RunTime/Use.meta
|
>>>>>>>> 03.FiniteStateMachine:Assets/03.FiniteStateMachine/RunTime/Use.meta
|
||||||
|
|||||||
53
Assets/03.FiniteStateMachine/RunTime/Use/FSMExtension.cs
Normal file
53
Assets/03.FiniteStateMachine/RunTime/Use/FSMExtension.cs
Normal 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不存在或已被销毁!!!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
70
Assets/03.FiniteStateMachine/RunTime/Use/IFSMManager.cs
Normal file
70
Assets/03.FiniteStateMachine/RunTime/Use/IFSMManager.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "com.staryevo.finitestatemachine",
|
"name": "com.staryevo.finitestatemachine",
|
||||||
"version": "1.0.1",
|
"version": "1.0.2",
|
||||||
"displayName": "03.FiniteStateMachine",
|
"displayName": "03.FiniteStateMachine",
|
||||||
"description": "状态机工具",
|
"description": "状态机工具",
|
||||||
"unity": "2021.3",
|
"unity": "2021.3",
|
||||||
|
|||||||
Reference in New Issue
Block a user