mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
FsmComponent
FsmComponent
This commit is contained in:
43
Assets/TEngine/Editor/Inspector/FsmComponentInspector.cs
Normal file
43
Assets/TEngine/Editor/Inspector/FsmComponentInspector.cs
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
using UnityEditor;
|
||||||
|
|
||||||
|
namespace TEngine.Editor.Inspector
|
||||||
|
{
|
||||||
|
[CustomEditor(typeof(FsmComponent))]
|
||||||
|
internal sealed class FsmComponentInspector : GameFrameworkInspector
|
||||||
|
{
|
||||||
|
public override void OnInspectorGUI()
|
||||||
|
{
|
||||||
|
base.OnInspectorGUI();
|
||||||
|
|
||||||
|
if (!EditorApplication.isPlaying)
|
||||||
|
{
|
||||||
|
EditorGUILayout.HelpBox("Available during runtime only.", MessageType.Info);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
FsmComponent t = (FsmComponent)target;
|
||||||
|
|
||||||
|
if (IsPrefabInHierarchy(t.gameObject))
|
||||||
|
{
|
||||||
|
EditorGUILayout.LabelField("FSM Count", t.Count.ToString());
|
||||||
|
|
||||||
|
FsmBase[] fsms = t.GetAllFsms();
|
||||||
|
foreach (FsmBase fsm in fsms)
|
||||||
|
{
|
||||||
|
DrawFsm(fsm);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Repaint();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnEnable()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
private void DrawFsm(FsmBase fsm)
|
||||||
|
{
|
||||||
|
EditorGUILayout.LabelField(fsm.FullName, fsm.IsRunning ? Utility.Text.Format("{0}, {1:F1} s", fsm.CurrentStateName, fsm.CurrentStateTime) : (fsm.IsDestroyed ? "Destroyed" : "Not Running"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 1c11a8085df74853a698b707f9d5d87f
|
||||||
|
timeCreated: 1680504546
|
3
Assets/TEngine/Runtime/GameFramework/Fsm.meta
Normal file
3
Assets/TEngine/Runtime/GameFramework/Fsm.meta
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 6e93f024b1a142e2bd1afe53ad9c2941
|
||||||
|
timeCreated: 1680503869
|
555
Assets/TEngine/Runtime/GameFramework/Fsm/Fsm.cs
Normal file
555
Assets/TEngine/Runtime/GameFramework/Fsm/Fsm.cs
Normal file
@@ -0,0 +1,555 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace TEngine
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
|
||||||
|
internal sealed class Fsm<T> : FsmBase, IMemory, IFsm<T> where T : class
|
||||||
|
{
|
||||||
|
private T m_Owner;
|
||||||
|
private readonly Dictionary<Type, FsmState<T>> m_States;
|
||||||
|
private Dictionary<string, object> m_Datas;
|
||||||
|
private FsmState<T> m_CurrentState;
|
||||||
|
private float m_CurrentStateTime;
|
||||||
|
private bool m_IsDestroyed;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 初始化有限状态机的新实例。
|
||||||
|
/// </summary>
|
||||||
|
public Fsm()
|
||||||
|
{
|
||||||
|
m_Owner = null;
|
||||||
|
m_States = new Dictionary<Type, FsmState<T>>();
|
||||||
|
m_Datas = null;
|
||||||
|
m_CurrentState = null;
|
||||||
|
m_CurrentStateTime = 0f;
|
||||||
|
m_IsDestroyed = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机持有者。
|
||||||
|
/// </summary>
|
||||||
|
public T Owner
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return m_Owner;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机持有者类型。
|
||||||
|
/// </summary>
|
||||||
|
public override Type OwnerType
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return typeof(T);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机中状态的数量。
|
||||||
|
/// </summary>
|
||||||
|
public override int FsmStateCount
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return m_States.Count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机是否正在运行。
|
||||||
|
/// </summary>
|
||||||
|
public override bool IsRunning
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return m_CurrentState != null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机是否被销毁。
|
||||||
|
/// </summary>
|
||||||
|
public override bool IsDestroyed
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return m_IsDestroyed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取当前有限状态机状态。
|
||||||
|
/// </summary>
|
||||||
|
public FsmState<T> CurrentState
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return m_CurrentState;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取当前有限状态机状态名称。
|
||||||
|
/// </summary>
|
||||||
|
public override string CurrentStateName
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return m_CurrentState != null ? m_CurrentState.GetType().FullName : null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取当前有限状态机状态持续时间。
|
||||||
|
/// </summary>
|
||||||
|
public override float CurrentStateTime
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return m_CurrentStateTime;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 创建有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="name">有限状态机名称。</param>
|
||||||
|
/// <param name="owner">有限状态机持有者。</param>
|
||||||
|
/// <param name="states">有限状态机状态集合。</param>
|
||||||
|
/// <returns>创建的有限状态机。</returns>
|
||||||
|
public static Fsm<T> Create(string name, T owner, params FsmState<T>[] states)
|
||||||
|
{
|
||||||
|
if (owner == null)
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException("FSM owner is invalid.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (states == null || states.Length < 1)
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException("FSM states is invalid.");
|
||||||
|
}
|
||||||
|
|
||||||
|
Fsm<T> fsm = MemoryPool.Acquire<Fsm<T>>();
|
||||||
|
fsm.Name = name;
|
||||||
|
fsm.m_Owner = owner;
|
||||||
|
fsm.m_IsDestroyed = false;
|
||||||
|
foreach (FsmState<T> state in states)
|
||||||
|
{
|
||||||
|
if (state == null)
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException("FSM states is invalid.");
|
||||||
|
}
|
||||||
|
|
||||||
|
Type stateType = state.GetType();
|
||||||
|
if (fsm.m_States.ContainsKey(stateType))
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException(Utility.Text.Format("FSM '{0}' state '{1}' is already exist.", new TypeNamePair(typeof(T), name), stateType.FullName));
|
||||||
|
}
|
||||||
|
|
||||||
|
fsm.m_States.Add(stateType, state);
|
||||||
|
state.OnInit(fsm);
|
||||||
|
}
|
||||||
|
|
||||||
|
return fsm;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 创建有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="name">有限状态机名称。</param>
|
||||||
|
/// <param name="owner">有限状态机持有者。</param>
|
||||||
|
/// <param name="states">有限状态机状态集合。</param>
|
||||||
|
/// <returns>创建的有限状态机。</returns>
|
||||||
|
public static Fsm<T> Create(string name, T owner, List<FsmState<T>> states)
|
||||||
|
{
|
||||||
|
if (owner == null)
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException("FSM owner is invalid.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (states == null || states.Count < 1)
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException("FSM states is invalid.");
|
||||||
|
}
|
||||||
|
|
||||||
|
Fsm<T> fsm = MemoryPool.Acquire<Fsm<T>>();
|
||||||
|
fsm.Name = name;
|
||||||
|
fsm.m_Owner = owner;
|
||||||
|
fsm.m_IsDestroyed = false;
|
||||||
|
foreach (FsmState<T> state in states)
|
||||||
|
{
|
||||||
|
if (state == null)
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException("FSM states is invalid.");
|
||||||
|
}
|
||||||
|
|
||||||
|
Type stateType = state.GetType();
|
||||||
|
if (fsm.m_States.ContainsKey(stateType))
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException(Utility.Text.Format("FSM '{0}' state '{1}' is already exist.", new TypeNamePair(typeof(T), name), stateType.FullName));
|
||||||
|
}
|
||||||
|
|
||||||
|
fsm.m_States.Add(stateType, state);
|
||||||
|
state.OnInit(fsm);
|
||||||
|
}
|
||||||
|
|
||||||
|
return fsm;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 清理有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
public void Clear()
|
||||||
|
{
|
||||||
|
if (m_CurrentState != null)
|
||||||
|
{
|
||||||
|
m_CurrentState.OnLeave(this, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (KeyValuePair<Type, FsmState<T>> state in m_States)
|
||||||
|
{
|
||||||
|
state.Value.OnDestroy(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
Name = null;
|
||||||
|
m_Owner = null;
|
||||||
|
m_States.Clear();
|
||||||
|
|
||||||
|
m_Datas.Clear();
|
||||||
|
|
||||||
|
m_CurrentState = null;
|
||||||
|
m_CurrentStateTime = 0f;
|
||||||
|
m_IsDestroyed = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 开始有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TState">要开始的有限状态机状态类型。</typeparam>
|
||||||
|
public void Start<TState>() where TState : FsmState<T>
|
||||||
|
{
|
||||||
|
if (IsRunning)
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException("FSM is running, can not start again.");
|
||||||
|
}
|
||||||
|
|
||||||
|
FsmState<T> state = GetState<TState>();
|
||||||
|
if (state == null)
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException(Utility.Text.Format("FSM '{0}' can not start state '{1}' which is not exist.", new TypeNamePair(typeof(T), Name), typeof(TState).FullName));
|
||||||
|
}
|
||||||
|
|
||||||
|
m_CurrentStateTime = 0f;
|
||||||
|
m_CurrentState = state;
|
||||||
|
m_CurrentState.OnEnter(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 开始有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="stateType">要开始的有限状态机状态类型。</param>
|
||||||
|
public void Start(Type stateType)
|
||||||
|
{
|
||||||
|
if (IsRunning)
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException("FSM is running, can not start again.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stateType == null)
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException("State type is invalid.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!typeof(FsmState<T>).IsAssignableFrom(stateType))
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException(Utility.Text.Format("State type '{0}' is invalid.", stateType.FullName));
|
||||||
|
}
|
||||||
|
|
||||||
|
FsmState<T> state = GetState(stateType);
|
||||||
|
if (state == null)
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException(Utility.Text.Format("FSM '{0}' can not start state '{1}' which is not exist.", new TypeNamePair(typeof(T), Name), stateType.FullName));
|
||||||
|
}
|
||||||
|
|
||||||
|
m_CurrentStateTime = 0f;
|
||||||
|
m_CurrentState = state;
|
||||||
|
m_CurrentState.OnEnter(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否存在有限状态机状态。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TState">要检查的有限状态机状态类型。</typeparam>
|
||||||
|
/// <returns>是否存在有限状态机状态。</returns>
|
||||||
|
public bool HasState<TState>() where TState : FsmState<T>
|
||||||
|
{
|
||||||
|
return m_States.ContainsKey(typeof(TState));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否存在有限状态机状态。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="stateType">要检查的有限状态机状态类型。</param>
|
||||||
|
/// <returns>是否存在有限状态机状态。</returns>
|
||||||
|
public bool HasState(Type stateType)
|
||||||
|
{
|
||||||
|
if (stateType == null)
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException("State type is invalid.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!typeof(FsmState<T>).IsAssignableFrom(stateType))
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException(Utility.Text.Format("State type '{0}' is invalid.", stateType.FullName));
|
||||||
|
}
|
||||||
|
|
||||||
|
return m_States.ContainsKey(stateType);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机状态。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TState">要获取的有限状态机状态类型。</typeparam>
|
||||||
|
/// <returns>要获取的有限状态机状态。</returns>
|
||||||
|
public TState GetState<TState>() where TState : FsmState<T>
|
||||||
|
{
|
||||||
|
FsmState<T> state = null;
|
||||||
|
if (m_States.TryGetValue(typeof(TState), out state))
|
||||||
|
{
|
||||||
|
return (TState)state;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机状态。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="stateType">要获取的有限状态机状态类型。</param>
|
||||||
|
/// <returns>要获取的有限状态机状态。</returns>
|
||||||
|
public FsmState<T> GetState(Type stateType)
|
||||||
|
{
|
||||||
|
if (stateType == null)
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException("State type is invalid.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!typeof(FsmState<T>).IsAssignableFrom(stateType))
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException(Utility.Text.Format("State type '{0}' is invalid.", stateType.FullName));
|
||||||
|
}
|
||||||
|
|
||||||
|
FsmState<T> state = null;
|
||||||
|
if (m_States.TryGetValue(stateType, out state))
|
||||||
|
{
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机的所有状态。
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>有限状态机的所有状态。</returns>
|
||||||
|
public FsmState<T>[] GetAllStates()
|
||||||
|
{
|
||||||
|
int index = 0;
|
||||||
|
FsmState<T>[] results = new FsmState<T>[m_States.Count];
|
||||||
|
foreach (KeyValuePair<Type, FsmState<T>> state in m_States)
|
||||||
|
{
|
||||||
|
results[index++] = state.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机的所有状态。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="results">有限状态机的所有状态。</param>
|
||||||
|
public void GetAllStates(List<FsmState<T>> results)
|
||||||
|
{
|
||||||
|
if (results == null)
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException("Results is invalid.");
|
||||||
|
}
|
||||||
|
|
||||||
|
results.Clear();
|
||||||
|
foreach (KeyValuePair<Type, FsmState<T>> state in m_States)
|
||||||
|
{
|
||||||
|
results.Add(state.Value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否存在有限状态机数据。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="name">有限状态机数据名称。</param>
|
||||||
|
/// <returns>有限状态机数据是否存在。</returns>
|
||||||
|
public bool HasData(string name)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(name))
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException("Data name is invalid.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_Datas == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return m_Datas.ContainsKey(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机数据。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TData">要获取的有限状态机数据的类型。</typeparam>
|
||||||
|
/// <param name="name">有限状态机数据名称。</param>
|
||||||
|
/// <returns>要获取的有限状态机数据。</returns>
|
||||||
|
public TData GetData<TData>(string name)
|
||||||
|
{
|
||||||
|
return (TData)GetData(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机数据。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="name">有限状态机数据名称。</param>
|
||||||
|
/// <returns>要获取的有限状态机数据。</returns>
|
||||||
|
public object GetData(string name)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(name))
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException("Data name is invalid.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_Datas == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_Datas.TryGetValue(name, out var data))
|
||||||
|
{
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 设置有限状态机数据。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TData">要设置的有限状态机数据的类型。</typeparam>
|
||||||
|
/// <param name="name">有限状态机数据名称。</param>
|
||||||
|
/// <param name="data">要设置的有限状态机数据。</param>
|
||||||
|
public void SetData<TData>(string name, TData data)
|
||||||
|
{
|
||||||
|
SetData(name, (object)data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 设置有限状态机数据。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="name">有限状态机数据名称。</param>
|
||||||
|
/// <param name="data">要设置的有限状态机数据。</param>
|
||||||
|
public void SetData(string name, object data)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(name))
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException("Data name is invalid.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_Datas == null)
|
||||||
|
{
|
||||||
|
m_Datas = new Dictionary<string, object>(StringComparer.Ordinal);
|
||||||
|
}
|
||||||
|
|
||||||
|
m_Datas[name] = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 移除有限状态机数据。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="name">有限状态机数据名称。</param>
|
||||||
|
/// <returns>是否移除有限状态机数据成功。</returns>
|
||||||
|
public bool RemoveData(string name)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(name))
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException("Data name is invalid.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_Datas == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return m_Datas.Remove(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 有限状态机轮询。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="elapseSeconds">逻辑流逝时间,以秒为单位。</param>
|
||||||
|
/// <param name="realElapseSeconds">真实流逝时间,以秒为单位。</param>
|
||||||
|
internal override void Update(float elapseSeconds, float realElapseSeconds)
|
||||||
|
{
|
||||||
|
if (m_CurrentState == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_CurrentStateTime += elapseSeconds;
|
||||||
|
m_CurrentState.OnUpdate(this, elapseSeconds, realElapseSeconds);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 关闭并清理有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
internal override void Shutdown()
|
||||||
|
{
|
||||||
|
MemoryPool.Release(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 切换当前有限状态机状态。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TState">要切换到的有限状态机状态类型。</typeparam>
|
||||||
|
internal void ChangeState<TState>() where TState : FsmState<T>
|
||||||
|
{
|
||||||
|
ChangeState(typeof(TState));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 切换当前有限状态机状态。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="stateType">要切换到的有限状态机状态类型。</param>
|
||||||
|
internal void ChangeState(Type stateType)
|
||||||
|
{
|
||||||
|
if (m_CurrentState == null)
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException("Current state is invalid.");
|
||||||
|
}
|
||||||
|
|
||||||
|
FsmState<T> state = GetState(stateType);
|
||||||
|
if (state == null)
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException(Utility.Text.Format("FSM '{0}' can not change state to '{1}' which is not exist.", new TypeNamePair(typeof(T), Name), stateType.FullName));
|
||||||
|
}
|
||||||
|
|
||||||
|
m_CurrentState.OnLeave(this, false);
|
||||||
|
m_CurrentStateTime = 0f;
|
||||||
|
m_CurrentState = state;
|
||||||
|
m_CurrentState.OnEnter(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
Assets/TEngine/Runtime/GameFramework/Fsm/Fsm.cs.meta
Normal file
3
Assets/TEngine/Runtime/GameFramework/Fsm/Fsm.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 3706fab38c144cd5a30f5b14946e22e5
|
||||||
|
timeCreated: 1680503869
|
106
Assets/TEngine/Runtime/GameFramework/Fsm/FsmBase.cs
Normal file
106
Assets/TEngine/Runtime/GameFramework/Fsm/FsmBase.cs
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace TEngine
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 有限状态机基类。
|
||||||
|
/// </summary>
|
||||||
|
public abstract class FsmBase
|
||||||
|
{
|
||||||
|
private string m_Name;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 初始化有限状态机基类的新实例。
|
||||||
|
/// </summary>
|
||||||
|
public FsmBase()
|
||||||
|
{
|
||||||
|
m_Name = string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机名称。
|
||||||
|
/// </summary>
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return m_Name;
|
||||||
|
}
|
||||||
|
protected set
|
||||||
|
{
|
||||||
|
m_Name = value ?? string.Empty;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机完整名称。
|
||||||
|
/// </summary>
|
||||||
|
public string FullName
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return new TypeNamePair(OwnerType, m_Name).ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机持有者类型。
|
||||||
|
/// </summary>
|
||||||
|
public abstract Type OwnerType
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机中状态的数量。
|
||||||
|
/// </summary>
|
||||||
|
public abstract int FsmStateCount
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机是否正在运行。
|
||||||
|
/// </summary>
|
||||||
|
public abstract bool IsRunning
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机是否被销毁。
|
||||||
|
/// </summary>
|
||||||
|
public abstract bool IsDestroyed
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取当前有限状态机状态名称。
|
||||||
|
/// </summary>
|
||||||
|
public abstract string CurrentStateName
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取当前有限状态机状态持续时间。
|
||||||
|
/// </summary>
|
||||||
|
public abstract float CurrentStateTime
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 有限状态机轮询。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="elapseSeconds">逻辑流逝时间,以秒为单位。</param>
|
||||||
|
/// <param name="realElapseSeconds">当前已流逝时间,以秒为单位。</param>
|
||||||
|
internal abstract void Update(float elapseSeconds, float realElapseSeconds);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 关闭并清理有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
internal abstract void Shutdown();
|
||||||
|
}
|
||||||
|
}
|
3
Assets/TEngine/Runtime/GameFramework/Fsm/FsmBase.cs.meta
Normal file
3
Assets/TEngine/Runtime/GameFramework/Fsm/FsmBase.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: c9096cb538b149d08ca95c78f651c111
|
||||||
|
timeCreated: 1680503870
|
259
Assets/TEngine/Runtime/GameFramework/Fsm/FsmComponent.cs
Normal file
259
Assets/TEngine/Runtime/GameFramework/Fsm/FsmComponent.cs
Normal file
@@ -0,0 +1,259 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace TEngine
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 有限状态机组件。
|
||||||
|
/// </summary>
|
||||||
|
[DisallowMultipleComponent]
|
||||||
|
public sealed class FsmComponent : GameFrameworkComponent
|
||||||
|
{
|
||||||
|
private IFsmManager m_FsmManager = null;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机数量。
|
||||||
|
/// </summary>
|
||||||
|
public int Count
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return m_FsmManager.Count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 游戏框架组件初始化。
|
||||||
|
/// </summary>
|
||||||
|
protected override void Awake()
|
||||||
|
{
|
||||||
|
base.Awake();
|
||||||
|
|
||||||
|
m_FsmManager = GameFrameworkEntry.GetModule<IFsmManager>();
|
||||||
|
if (m_FsmManager == null)
|
||||||
|
{
|
||||||
|
Log.Fatal("FSM manager is invalid.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Start()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 检查是否存在有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
|
||||||
|
/// <returns>是否存在有限状态机。</returns>
|
||||||
|
public bool HasFsm<T>() where T : class
|
||||||
|
{
|
||||||
|
return m_FsmManager.HasFsm<T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 检查是否存在有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ownerType">有限状态机持有者类型。</param>
|
||||||
|
/// <returns>是否存在有限状态机。</returns>
|
||||||
|
public bool HasFsm(Type ownerType)
|
||||||
|
{
|
||||||
|
return m_FsmManager.HasFsm(ownerType);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 检查是否存在有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
|
||||||
|
/// <param name="name">有限状态机名称。</param>
|
||||||
|
/// <returns>是否存在有限状态机。</returns>
|
||||||
|
public bool HasFsm<T>(string name) where T : class
|
||||||
|
{
|
||||||
|
return m_FsmManager.HasFsm<T>(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 检查是否存在有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ownerType">有限状态机持有者类型。</param>
|
||||||
|
/// <param name="name">有限状态机名称。</param>
|
||||||
|
/// <returns>是否存在有限状态机。</returns>
|
||||||
|
public bool HasFsm(Type ownerType, string name)
|
||||||
|
{
|
||||||
|
return m_FsmManager.HasFsm(ownerType, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
|
||||||
|
/// <returns>要获取的有限状态机。</returns>
|
||||||
|
public IFsm<T> GetFsm<T>() where T : class
|
||||||
|
{
|
||||||
|
return m_FsmManager.GetFsm<T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ownerType">有限状态机持有者类型。</param>
|
||||||
|
/// <returns>要获取的有限状态机。</returns>
|
||||||
|
public FsmBase GetFsm(Type ownerType)
|
||||||
|
{
|
||||||
|
return m_FsmManager.GetFsm(ownerType);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
|
||||||
|
/// <param name="name">有限状态机名称。</param>
|
||||||
|
/// <returns>要获取的有限状态机。</returns>
|
||||||
|
public IFsm<T> GetFsm<T>(string name) where T : class
|
||||||
|
{
|
||||||
|
return m_FsmManager.GetFsm<T>(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ownerType">有限状态机持有者类型。</param>
|
||||||
|
/// <param name="name">有限状态机名称。</param>
|
||||||
|
/// <returns>要获取的有限状态机。</returns>
|
||||||
|
public FsmBase GetFsm(Type ownerType, string name)
|
||||||
|
{
|
||||||
|
return m_FsmManager.GetFsm(ownerType, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取所有有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
public FsmBase[] GetAllFsms()
|
||||||
|
{
|
||||||
|
return m_FsmManager.GetAllFsms();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取所有有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="results">所有有限状态机。</param>
|
||||||
|
public void GetAllFsms(List<FsmBase> results)
|
||||||
|
{
|
||||||
|
m_FsmManager.GetAllFsms(results);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 创建有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
|
||||||
|
/// <param name="owner">有限状态机持有者。</param>
|
||||||
|
/// <param name="states">有限状态机状态集合。</param>
|
||||||
|
/// <returns>要创建的有限状态机。</returns>
|
||||||
|
public IFsm<T> CreateFsm<T>(T owner, params FsmState<T>[] states) where T : class
|
||||||
|
{
|
||||||
|
return m_FsmManager.CreateFsm(owner, states);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 创建有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
|
||||||
|
/// <param name="name">有限状态机名称。</param>
|
||||||
|
/// <param name="owner">有限状态机持有者。</param>
|
||||||
|
/// <param name="states">有限状态机状态集合。</param>
|
||||||
|
/// <returns>要创建的有限状态机。</returns>
|
||||||
|
public IFsm<T> CreateFsm<T>(string name, T owner, params FsmState<T>[] states) where T : class
|
||||||
|
{
|
||||||
|
return m_FsmManager.CreateFsm(name, owner, states);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 创建有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
|
||||||
|
/// <param name="owner">有限状态机持有者。</param>
|
||||||
|
/// <param name="states">有限状态机状态集合。</param>
|
||||||
|
/// <returns>要创建的有限状态机。</returns>
|
||||||
|
public IFsm<T> CreateFsm<T>(T owner, List<FsmState<T>> states) where T : class
|
||||||
|
{
|
||||||
|
return m_FsmManager.CreateFsm(owner, states);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 创建有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
|
||||||
|
/// <param name="name">有限状态机名称。</param>
|
||||||
|
/// <param name="owner">有限状态机持有者。</param>
|
||||||
|
/// <param name="states">有限状态机状态集合。</param>
|
||||||
|
/// <returns>要创建的有限状态机。</returns>
|
||||||
|
public IFsm<T> CreateFsm<T>(string name, T owner, List<FsmState<T>> states) where T : class
|
||||||
|
{
|
||||||
|
return m_FsmManager.CreateFsm(name, owner, states);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 销毁有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
|
||||||
|
/// <returns>是否销毁有限状态机成功。</returns>
|
||||||
|
public bool DestroyFsm<T>() where T : class
|
||||||
|
{
|
||||||
|
return m_FsmManager.DestroyFsm<T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 销毁有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ownerType">有限状态机持有者类型。</param>
|
||||||
|
/// <returns>是否销毁有限状态机成功。</returns>
|
||||||
|
public bool DestroyFsm(Type ownerType)
|
||||||
|
{
|
||||||
|
return m_FsmManager.DestroyFsm(ownerType);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 销毁有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
|
||||||
|
/// <param name="name">要销毁的有限状态机名称。</param>
|
||||||
|
/// <returns>是否销毁有限状态机成功。</returns>
|
||||||
|
public bool DestroyFsm<T>(string name) where T : class
|
||||||
|
{
|
||||||
|
return m_FsmManager.DestroyFsm<T>(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 销毁有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ownerType">有限状态机持有者类型。</param>
|
||||||
|
/// <param name="name">要销毁的有限状态机名称。</param>
|
||||||
|
/// <returns>是否销毁有限状态机成功。</returns>
|
||||||
|
public bool DestroyFsm(Type ownerType, string name)
|
||||||
|
{
|
||||||
|
return m_FsmManager.DestroyFsm(ownerType, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 销毁有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
|
||||||
|
/// <param name="fsm">要销毁的有限状态机。</param>
|
||||||
|
/// <returns>是否销毁有限状态机成功。</returns>
|
||||||
|
public bool DestroyFsm<T>(IFsm<T> fsm) where T : class
|
||||||
|
{
|
||||||
|
return m_FsmManager.DestroyFsm(fsm);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 销毁有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="fsm">要销毁的有限状态机。</param>
|
||||||
|
/// <returns>是否销毁有限状态机成功。</returns>
|
||||||
|
public bool DestroyFsm(FsmBase fsm)
|
||||||
|
{
|
||||||
|
return m_FsmManager.DestroyFsm(fsm);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 351f97b231854e659f0b7af89d8478c6
|
||||||
|
timeCreated: 1680504514
|
404
Assets/TEngine/Runtime/GameFramework/Fsm/FsmManager.cs
Normal file
404
Assets/TEngine/Runtime/GameFramework/Fsm/FsmManager.cs
Normal file
@@ -0,0 +1,404 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace TEngine
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 有限状态机管理器。
|
||||||
|
/// </summary>
|
||||||
|
internal sealed class FsmManager : GameFrameworkModule, IFsmManager
|
||||||
|
{
|
||||||
|
private readonly Dictionary<TypeNamePair, FsmBase> m_Fsms;
|
||||||
|
private readonly List<FsmBase> m_TempFsms;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 初始化有限状态机管理器的新实例。
|
||||||
|
/// </summary>
|
||||||
|
public FsmManager()
|
||||||
|
{
|
||||||
|
m_Fsms = new Dictionary<TypeNamePair, FsmBase>();
|
||||||
|
m_TempFsms = new List<FsmBase>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取游戏框架模块优先级。
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>优先级较高的模块会优先轮询,并且关闭操作会后进行。</remarks>
|
||||||
|
internal override int Priority
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机数量。
|
||||||
|
/// </summary>
|
||||||
|
public int Count
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return m_Fsms.Count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 有限状态机管理器轮询。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="elapseSeconds">逻辑流逝时间,以秒为单位。</param>
|
||||||
|
/// <param name="realElapseSeconds">真实流逝时间,以秒为单位。</param>
|
||||||
|
internal override void Update(float elapseSeconds, float realElapseSeconds)
|
||||||
|
{
|
||||||
|
m_TempFsms.Clear();
|
||||||
|
if (m_Fsms.Count <= 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (KeyValuePair<TypeNamePair, FsmBase> fsm in m_Fsms)
|
||||||
|
{
|
||||||
|
m_TempFsms.Add(fsm.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (FsmBase fsm in m_TempFsms)
|
||||||
|
{
|
||||||
|
if (fsm.IsDestroyed)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
fsm.Update(elapseSeconds, realElapseSeconds);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 关闭并清理有限状态机管理器。
|
||||||
|
/// </summary>
|
||||||
|
internal override void Shutdown()
|
||||||
|
{
|
||||||
|
foreach (KeyValuePair<TypeNamePair, FsmBase> fsm in m_Fsms)
|
||||||
|
{
|
||||||
|
fsm.Value.Shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
m_Fsms.Clear();
|
||||||
|
m_TempFsms.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 检查是否存在有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
|
||||||
|
/// <returns>是否存在有限状态机。</returns>
|
||||||
|
public bool HasFsm<T>() where T : class
|
||||||
|
{
|
||||||
|
return InternalHasFsm(new TypeNamePair(typeof(T)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 检查是否存在有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ownerType">有限状态机持有者类型。</param>
|
||||||
|
/// <returns>是否存在有限状态机。</returns>
|
||||||
|
public bool HasFsm(Type ownerType)
|
||||||
|
{
|
||||||
|
if (ownerType == null)
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException("Owner type is invalid.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return InternalHasFsm(new TypeNamePair(ownerType));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 检查是否存在有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
|
||||||
|
/// <param name="name">有限状态机名称。</param>
|
||||||
|
/// <returns>是否存在有限状态机。</returns>
|
||||||
|
public bool HasFsm<T>(string name) where T : class
|
||||||
|
{
|
||||||
|
return InternalHasFsm(new TypeNamePair(typeof(T), name));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 检查是否存在有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ownerType">有限状态机持有者类型。</param>
|
||||||
|
/// <param name="name">有限状态机名称。</param>
|
||||||
|
/// <returns>是否存在有限状态机。</returns>
|
||||||
|
public bool HasFsm(Type ownerType, string name)
|
||||||
|
{
|
||||||
|
if (ownerType == null)
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException("Owner type is invalid.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return InternalHasFsm(new TypeNamePair(ownerType, name));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
|
||||||
|
/// <returns>要获取的有限状态机。</returns>
|
||||||
|
public IFsm<T> GetFsm<T>() where T : class
|
||||||
|
{
|
||||||
|
return (IFsm<T>)InternalGetFsm(new TypeNamePair(typeof(T)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ownerType">有限状态机持有者类型。</param>
|
||||||
|
/// <returns>要获取的有限状态机。</returns>
|
||||||
|
public FsmBase GetFsm(Type ownerType)
|
||||||
|
{
|
||||||
|
if (ownerType == null)
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException("Owner type is invalid.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return InternalGetFsm(new TypeNamePair(ownerType));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
|
||||||
|
/// <param name="name">有限状态机名称。</param>
|
||||||
|
/// <returns>要获取的有限状态机。</returns>
|
||||||
|
public IFsm<T> GetFsm<T>(string name) where T : class
|
||||||
|
{
|
||||||
|
return (IFsm<T>)InternalGetFsm(new TypeNamePair(typeof(T), name));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ownerType">有限状态机持有者类型。</param>
|
||||||
|
/// <param name="name">有限状态机名称。</param>
|
||||||
|
/// <returns>要获取的有限状态机。</returns>
|
||||||
|
public FsmBase GetFsm(Type ownerType, string name)
|
||||||
|
{
|
||||||
|
if (ownerType == null)
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException("Owner type is invalid.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return InternalGetFsm(new TypeNamePair(ownerType, name));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取所有有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>所有有限状态机。</returns>
|
||||||
|
public FsmBase[] GetAllFsms()
|
||||||
|
{
|
||||||
|
int index = 0;
|
||||||
|
FsmBase[] results = new FsmBase[m_Fsms.Count];
|
||||||
|
foreach (KeyValuePair<TypeNamePair, FsmBase> fsm in m_Fsms)
|
||||||
|
{
|
||||||
|
results[index++] = fsm.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取所有有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="results">所有有限状态机。</param>
|
||||||
|
public void GetAllFsms(List<FsmBase> results)
|
||||||
|
{
|
||||||
|
if (results == null)
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException("Results is invalid.");
|
||||||
|
}
|
||||||
|
|
||||||
|
results.Clear();
|
||||||
|
foreach (KeyValuePair<TypeNamePair, FsmBase> fsm in m_Fsms)
|
||||||
|
{
|
||||||
|
results.Add(fsm.Value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 创建有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
|
||||||
|
/// <param name="owner">有限状态机持有者。</param>
|
||||||
|
/// <param name="states">有限状态机状态集合。</param>
|
||||||
|
/// <returns>要创建的有限状态机。</returns>
|
||||||
|
public IFsm<T> CreateFsm<T>(T owner, params FsmState<T>[] states) where T : class
|
||||||
|
{
|
||||||
|
return CreateFsm(string.Empty, owner, states);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 创建有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
|
||||||
|
/// <param name="name">有限状态机名称。</param>
|
||||||
|
/// <param name="owner">有限状态机持有者。</param>
|
||||||
|
/// <param name="states">有限状态机状态集合。</param>
|
||||||
|
/// <returns>要创建的有限状态机。</returns>
|
||||||
|
public IFsm<T> CreateFsm<T>(string name, T owner, params FsmState<T>[] states) where T : class
|
||||||
|
{
|
||||||
|
TypeNamePair typeNamePair = new TypeNamePair(typeof(T), name);
|
||||||
|
if (HasFsm<T>(name))
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException(Utility.Text.Format("Already exist FSM '{0}'.", typeNamePair));
|
||||||
|
}
|
||||||
|
|
||||||
|
Fsm<T> fsm = Fsm<T>.Create(name, owner, states);
|
||||||
|
m_Fsms.Add(typeNamePair, fsm);
|
||||||
|
return fsm;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 创建有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
|
||||||
|
/// <param name="owner">有限状态机持有者。</param>
|
||||||
|
/// <param name="states">有限状态机状态集合。</param>
|
||||||
|
/// <returns>要创建的有限状态机。</returns>
|
||||||
|
public IFsm<T> CreateFsm<T>(T owner, List<FsmState<T>> states) where T : class
|
||||||
|
{
|
||||||
|
return CreateFsm(string.Empty, owner, states);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 创建有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
|
||||||
|
/// <param name="name">有限状态机名称。</param>
|
||||||
|
/// <param name="owner">有限状态机持有者。</param>
|
||||||
|
/// <param name="states">有限状态机状态集合。</param>
|
||||||
|
/// <returns>要创建的有限状态机。</returns>
|
||||||
|
public IFsm<T> CreateFsm<T>(string name, T owner, List<FsmState<T>> states) where T : class
|
||||||
|
{
|
||||||
|
TypeNamePair typeNamePair = new TypeNamePair(typeof(T), name);
|
||||||
|
if (HasFsm<T>(name))
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException(Utility.Text.Format("Already exist FSM '{0}'.", typeNamePair));
|
||||||
|
}
|
||||||
|
|
||||||
|
Fsm<T> fsm = Fsm<T>.Create(name, owner, states);
|
||||||
|
m_Fsms.Add(typeNamePair, fsm);
|
||||||
|
return fsm;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 销毁有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
|
||||||
|
/// <returns>是否销毁有限状态机成功。</returns>
|
||||||
|
public bool DestroyFsm<T>() where T : class
|
||||||
|
{
|
||||||
|
return InternalDestroyFsm(new TypeNamePair(typeof(T)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 销毁有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ownerType">有限状态机持有者类型。</param>
|
||||||
|
/// <returns>是否销毁有限状态机成功。</returns>
|
||||||
|
public bool DestroyFsm(Type ownerType)
|
||||||
|
{
|
||||||
|
if (ownerType == null)
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException("Owner type is invalid.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return InternalDestroyFsm(new TypeNamePair(ownerType));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 销毁有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
|
||||||
|
/// <param name="name">要销毁的有限状态机名称。</param>
|
||||||
|
/// <returns>是否销毁有限状态机成功。</returns>
|
||||||
|
public bool DestroyFsm<T>(string name) where T : class
|
||||||
|
{
|
||||||
|
return InternalDestroyFsm(new TypeNamePair(typeof(T), name));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 销毁有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ownerType">有限状态机持有者类型。</param>
|
||||||
|
/// <param name="name">要销毁的有限状态机名称。</param>
|
||||||
|
/// <returns>是否销毁有限状态机成功。</returns>
|
||||||
|
public bool DestroyFsm(Type ownerType, string name)
|
||||||
|
{
|
||||||
|
if (ownerType == null)
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException("Owner type is invalid.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return InternalDestroyFsm(new TypeNamePair(ownerType, name));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 销毁有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
|
||||||
|
/// <param name="fsm">要销毁的有限状态机。</param>
|
||||||
|
/// <returns>是否销毁有限状态机成功。</returns>
|
||||||
|
public bool DestroyFsm<T>(IFsm<T> fsm) where T : class
|
||||||
|
{
|
||||||
|
if (fsm == null)
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException("FSM is invalid.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return InternalDestroyFsm(new TypeNamePair(typeof(T), fsm.Name));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 销毁有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="fsm">要销毁的有限状态机。</param>
|
||||||
|
/// <returns>是否销毁有限状态机成功。</returns>
|
||||||
|
public bool DestroyFsm(FsmBase fsm)
|
||||||
|
{
|
||||||
|
if (fsm == null)
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException("FSM is invalid.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return InternalDestroyFsm(new TypeNamePair(fsm.OwnerType, fsm.Name));
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool InternalHasFsm(TypeNamePair typeNamePair)
|
||||||
|
{
|
||||||
|
return m_Fsms.ContainsKey(typeNamePair);
|
||||||
|
}
|
||||||
|
|
||||||
|
private FsmBase InternalGetFsm(TypeNamePair typeNamePair)
|
||||||
|
{
|
||||||
|
FsmBase fsm = null;
|
||||||
|
if (m_Fsms.TryGetValue(typeNamePair, out fsm))
|
||||||
|
{
|
||||||
|
return fsm;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool InternalDestroyFsm(TypeNamePair typeNamePair)
|
||||||
|
{
|
||||||
|
FsmBase fsm = null;
|
||||||
|
if (m_Fsms.TryGetValue(typeNamePair, out fsm))
|
||||||
|
{
|
||||||
|
fsm.Shutdown();
|
||||||
|
return m_Fsms.Remove(typeNamePair);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 839a80bc3c4f454f9718609ca8cded3e
|
||||||
|
timeCreated: 1680503870
|
103
Assets/TEngine/Runtime/GameFramework/Fsm/FsmState.cs
Normal file
103
Assets/TEngine/Runtime/GameFramework/Fsm/FsmState.cs
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace TEngine
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 有限状态机状态基类。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
|
||||||
|
public abstract class FsmState<T> where T : class
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 初始化有限状态机状态基类的新实例。
|
||||||
|
/// </summary>
|
||||||
|
public FsmState()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 有限状态机状态初始化时调用。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="fsm">有限状态机引用。</param>
|
||||||
|
protected internal virtual void OnInit(IFsm<T> fsm)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 有限状态机状态进入时调用。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="fsm">有限状态机引用。</param>
|
||||||
|
protected internal virtual void OnEnter(IFsm<T> fsm)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 有限状态机状态轮询时调用。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="fsm">有限状态机引用。</param>
|
||||||
|
/// <param name="elapseSeconds">逻辑流逝时间,以秒为单位。</param>
|
||||||
|
/// <param name="realElapseSeconds">真实流逝时间,以秒为单位。</param>
|
||||||
|
protected internal virtual void OnUpdate(IFsm<T> fsm, float elapseSeconds, float realElapseSeconds)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 有限状态机状态离开时调用。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="fsm">有限状态机引用。</param>
|
||||||
|
/// <param name="isShutdown">是否是关闭有限状态机时触发。</param>
|
||||||
|
protected internal virtual void OnLeave(IFsm<T> fsm, bool isShutdown)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 有限状态机状态销毁时调用。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="fsm">有限状态机引用。</param>
|
||||||
|
protected internal virtual void OnDestroy(IFsm<T> fsm)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 切换当前有限状态机状态。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TState">要切换到的有限状态机状态类型。</typeparam>
|
||||||
|
/// <param name="fsm">有限状态机引用。</param>
|
||||||
|
protected void ChangeState<TState>(IFsm<T> fsm) where TState : FsmState<T>
|
||||||
|
{
|
||||||
|
Fsm<T> fsmImplement = (Fsm<T>)fsm;
|
||||||
|
if (fsmImplement == null)
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException("FSM is invalid.");
|
||||||
|
}
|
||||||
|
|
||||||
|
fsmImplement.ChangeState<TState>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 切换当前有限状态机状态。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="fsm">有限状态机引用。</param>
|
||||||
|
/// <param name="stateType">要切换到的有限状态机状态类型。</param>
|
||||||
|
protected void ChangeState(IFsm<T> fsm, Type stateType)
|
||||||
|
{
|
||||||
|
Fsm<T> fsmImplement = (Fsm<T>)fsm;
|
||||||
|
if (fsmImplement == null)
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException("FSM is invalid.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stateType == null)
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException("State type is invalid.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!typeof(FsmState<T>).IsAssignableFrom(stateType))
|
||||||
|
{
|
||||||
|
throw new GameFrameworkException(Utility.Text.Format("State type '{0}' is invalid.", stateType.FullName));
|
||||||
|
}
|
||||||
|
|
||||||
|
fsmImplement.ChangeState(stateType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 45ba0a93666a491296ffd4abfaec75e8
|
||||||
|
timeCreated: 1680503870
|
158
Assets/TEngine/Runtime/GameFramework/Fsm/IFsm.cs
Normal file
158
Assets/TEngine/Runtime/GameFramework/Fsm/IFsm.cs
Normal file
@@ -0,0 +1,158 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace TEngine
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 有限状态机接口。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
|
||||||
|
public interface IFsm<T> where T : class
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机名称。
|
||||||
|
/// </summary>
|
||||||
|
string Name
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机完整名称。
|
||||||
|
/// </summary>
|
||||||
|
string FullName
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机持有者。
|
||||||
|
/// </summary>
|
||||||
|
T Owner
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机中状态的数量。
|
||||||
|
/// </summary>
|
||||||
|
int FsmStateCount
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机是否正在运行。
|
||||||
|
/// </summary>
|
||||||
|
bool IsRunning
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机是否被销毁。
|
||||||
|
/// </summary>
|
||||||
|
bool IsDestroyed
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取当前有限状态机状态。
|
||||||
|
/// </summary>
|
||||||
|
FsmState<T> CurrentState
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取当前有限状态机状态持续时间。
|
||||||
|
/// </summary>
|
||||||
|
float CurrentStateTime
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 开始有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TState">要开始的有限状态机状态类型。</typeparam>
|
||||||
|
void Start<TState>() where TState : FsmState<T>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 开始有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="stateType">要开始的有限状态机状态类型。</param>
|
||||||
|
void Start(Type stateType);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否存在有限状态机状态。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TState">要检查的有限状态机状态类型。</typeparam>
|
||||||
|
/// <returns>是否存在有限状态机状态。</returns>
|
||||||
|
bool HasState<TState>() where TState : FsmState<T>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否存在有限状态机状态。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="stateType">要检查的有限状态机状态类型。</param>
|
||||||
|
/// <returns>是否存在有限状态机状态。</returns>
|
||||||
|
bool HasState(Type stateType);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机状态。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TState">要获取的有限状态机状态类型。</typeparam>
|
||||||
|
/// <returns>要获取的有限状态机状态。</returns>
|
||||||
|
TState GetState<TState>() where TState : FsmState<T>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机状态。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="stateType">要获取的有限状态机状态类型。</param>
|
||||||
|
/// <returns>要获取的有限状态机状态。</returns>
|
||||||
|
FsmState<T> GetState(Type stateType);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机的所有状态。
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>有限状态机的所有状态。</returns>
|
||||||
|
FsmState<T>[] GetAllStates();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机的所有状态。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="results">有限状态机的所有状态。</param>
|
||||||
|
void GetAllStates(List<FsmState<T>> results);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否存在有限状态机数据。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="name">有限状态机数据名称。</param>
|
||||||
|
/// <returns>有限状态机数据是否存在。</returns>
|
||||||
|
bool HasData(string name);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机数据。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TData">要获取的有限状态机数据的类型。</typeparam>
|
||||||
|
/// <param name="name">有限状态机数据名称。</param>
|
||||||
|
/// <returns>要获取的有限状态机数据。</returns>
|
||||||
|
TData GetData<TData>(string name);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 设置有限状态机数据。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TData">要设置的有限状态机数据的类型。</typeparam>
|
||||||
|
/// <param name="name">有限状态机数据名称。</param>
|
||||||
|
/// <param name="data">要设置的有限状态机数据。</param>
|
||||||
|
void SetData<TData>(string name, TData data);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 移除有限状态机数据。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="name">有限状态机数据名称。</param>
|
||||||
|
/// <returns>是否移除有限状态机数据成功。</returns>
|
||||||
|
bool RemoveData(string name);
|
||||||
|
}
|
||||||
|
}
|
3
Assets/TEngine/Runtime/GameFramework/Fsm/IFsm.cs.meta
Normal file
3
Assets/TEngine/Runtime/GameFramework/Fsm/IFsm.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 675620bfcf2643e4a58641c33ce1537e
|
||||||
|
timeCreated: 1680503870
|
174
Assets/TEngine/Runtime/GameFramework/Fsm/IFsmManager.cs
Normal file
174
Assets/TEngine/Runtime/GameFramework/Fsm/IFsmManager.cs
Normal file
@@ -0,0 +1,174 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace TEngine
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 有限状态机管理器。
|
||||||
|
/// </summary>
|
||||||
|
public interface IFsmManager
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机数量。
|
||||||
|
/// </summary>
|
||||||
|
int Count
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 检查是否存在有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
|
||||||
|
/// <returns>是否存在有限状态机。</returns>
|
||||||
|
bool HasFsm<T>() where T : class;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 检查是否存在有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ownerType">有限状态机持有者类型。</param>
|
||||||
|
/// <returns>是否存在有限状态机。</returns>
|
||||||
|
bool HasFsm(Type ownerType);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 检查是否存在有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
|
||||||
|
/// <param name="name">有限状态机名称。</param>
|
||||||
|
/// <returns>是否存在有限状态机。</returns>
|
||||||
|
bool HasFsm<T>(string name) where T : class;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 检查是否存在有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ownerType">有限状态机持有者类型。</param>
|
||||||
|
/// <param name="name">有限状态机名称。</param>
|
||||||
|
/// <returns>是否存在有限状态机。</returns>
|
||||||
|
bool HasFsm(Type ownerType, string name);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
|
||||||
|
/// <returns>要获取的有限状态机。</returns>
|
||||||
|
IFsm<T> GetFsm<T>() where T : class;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ownerType">有限状态机持有者类型。</param>
|
||||||
|
/// <returns>要获取的有限状态机。</returns>
|
||||||
|
FsmBase GetFsm(Type ownerType);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
|
||||||
|
/// <param name="name">有限状态机名称。</param>
|
||||||
|
/// <returns>要获取的有限状态机。</returns>
|
||||||
|
IFsm<T> GetFsm<T>(string name) where T : class;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ownerType">有限状态机持有者类型。</param>
|
||||||
|
/// <param name="name">有限状态机名称。</param>
|
||||||
|
/// <returns>要获取的有限状态机。</returns>
|
||||||
|
FsmBase GetFsm(Type ownerType, string name);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取所有有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>所有有限状态机。</returns>
|
||||||
|
FsmBase[] GetAllFsms();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取所有有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="results">所有有限状态机。</param>
|
||||||
|
void GetAllFsms(List<FsmBase> results);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 创建有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
|
||||||
|
/// <param name="owner">有限状态机持有者。</param>
|
||||||
|
/// <param name="states">有限状态机状态集合。</param>
|
||||||
|
/// <returns>要创建的有限状态机。</returns>
|
||||||
|
IFsm<T> CreateFsm<T>(T owner, params FsmState<T>[] states) where T : class;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 创建有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
|
||||||
|
/// <param name="name">有限状态机名称。</param>
|
||||||
|
/// <param name="owner">有限状态机持有者。</param>
|
||||||
|
/// <param name="states">有限状态机状态集合。</param>
|
||||||
|
/// <returns>要创建的有限状态机。</returns>
|
||||||
|
IFsm<T> CreateFsm<T>(string name, T owner, params FsmState<T>[] states) where T : class;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 创建有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
|
||||||
|
/// <param name="owner">有限状态机持有者。</param>
|
||||||
|
/// <param name="states">有限状态机状态集合。</param>
|
||||||
|
/// <returns>要创建的有限状态机。</returns>
|
||||||
|
IFsm<T> CreateFsm<T>(T owner, List<FsmState<T>> states) where T : class;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 创建有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
|
||||||
|
/// <param name="name">有限状态机名称。</param>
|
||||||
|
/// <param name="owner">有限状态机持有者。</param>
|
||||||
|
/// <param name="states">有限状态机状态集合。</param>
|
||||||
|
/// <returns>要创建的有限状态机。</returns>
|
||||||
|
IFsm<T> CreateFsm<T>(string name, T owner, List<FsmState<T>> states) where T : class;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 销毁有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
|
||||||
|
/// <returns>是否销毁有限状态机成功。</returns>
|
||||||
|
bool DestroyFsm<T>() where T : class;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 销毁有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ownerType">有限状态机持有者类型。</param>
|
||||||
|
/// <returns>是否销毁有限状态机成功。</returns>
|
||||||
|
bool DestroyFsm(Type ownerType);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 销毁有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
|
||||||
|
/// <param name="name">要销毁的有限状态机名称。</param>
|
||||||
|
/// <returns>是否销毁有限状态机成功。</returns>
|
||||||
|
bool DestroyFsm<T>(string name) where T : class;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 销毁有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ownerType">有限状态机持有者类型。</param>
|
||||||
|
/// <param name="name">要销毁的有限状态机名称。</param>
|
||||||
|
/// <returns>是否销毁有限状态机成功。</returns>
|
||||||
|
bool DestroyFsm(Type ownerType, string name);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 销毁有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
|
||||||
|
/// <param name="fsm">要销毁的有限状态机。</param>
|
||||||
|
/// <returns>是否销毁有限状态机成功。</returns>
|
||||||
|
bool DestroyFsm<T>(IFsm<T> fsm) where T : class;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 销毁有限状态机。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="fsm">要销毁的有限状态机。</param>
|
||||||
|
/// <returns>是否销毁有限状态机成功。</returns>
|
||||||
|
bool DestroyFsm(FsmBase fsm);
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 2c9f1536f7484d609c882586db2dc855
|
||||||
|
timeCreated: 1680503870
|
Reference in New Issue
Block a user