mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-07 16:45:10 +00:00
Fsm
Fsm
This commit is contained in:
8
Assets/TEngine/Runtime/Fsm.meta
Normal file
8
Assets/TEngine/Runtime/Fsm.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ff94fb3406b8dc744adcde21595f769c
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
575
Assets/TEngine/Runtime/Fsm/Fsm.cs
Normal file
575
Assets/TEngine/Runtime/Fsm/Fsm.cs
Normal file
@@ -0,0 +1,575 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace TEngine.Fsm
|
||||
{
|
||||
/// <summary>
|
||||
/// 有限状态机。
|
||||
/// </summary>
|
||||
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
|
||||
internal sealed class Fsm<T> : FsmBase,IMemPoolObject, 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 Exception("FSM owner is invalid.");
|
||||
}
|
||||
|
||||
if (states == null || states.Length < 1)
|
||||
{
|
||||
throw new Exception("FSM states is invalid.");
|
||||
}
|
||||
|
||||
Fsm<T> fsm = GameMemPool<Fsm<T>>.Alloc();
|
||||
fsm.Name = name;
|
||||
fsm.m_Owner = owner;
|
||||
fsm.m_IsDestroyed = false;
|
||||
foreach (FsmState<T> state in states)
|
||||
{
|
||||
if (state == null)
|
||||
{
|
||||
throw new Exception("FSM states is invalid.");
|
||||
}
|
||||
|
||||
Type stateType = state.GetType();
|
||||
if (fsm.m_States.ContainsKey(stateType))
|
||||
{
|
||||
throw new Exception(string.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 Exception("FSM owner is invalid.");
|
||||
}
|
||||
|
||||
if (states == null || states.Count < 1)
|
||||
{
|
||||
throw new Exception("FSM states is invalid.");
|
||||
}
|
||||
|
||||
Fsm<T> fsm = GameMemPool<Fsm<T>>.Alloc();
|
||||
fsm.Name = name;
|
||||
fsm.m_Owner = owner;
|
||||
fsm.m_IsDestroyed = false;
|
||||
foreach (FsmState<T> state in states)
|
||||
{
|
||||
if (state == null)
|
||||
{
|
||||
throw new Exception("FSM states is invalid.");
|
||||
}
|
||||
|
||||
Type stateType = state.GetType();
|
||||
if (fsm.m_States.ContainsKey(stateType))
|
||||
{
|
||||
throw new Exception(string.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();
|
||||
|
||||
if (m_Datas != null)
|
||||
{
|
||||
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 Exception("FSM is running, can not start again.");
|
||||
}
|
||||
|
||||
FsmState<T> state = GetState<TState>();
|
||||
if (state == null)
|
||||
{
|
||||
throw new Exception(string.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 Exception("FSM is running, can not start again.");
|
||||
}
|
||||
|
||||
if (stateType == null)
|
||||
{
|
||||
throw new Exception("State type is invalid.");
|
||||
}
|
||||
|
||||
if (!typeof(FsmState<T>).IsAssignableFrom(stateType))
|
||||
{
|
||||
throw new Exception(string.Format("State type '{0}' is invalid.", stateType.FullName));
|
||||
}
|
||||
|
||||
FsmState<T> state = GetState(stateType);
|
||||
if (state == null)
|
||||
{
|
||||
throw new Exception(string.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 Exception("State type is invalid.");
|
||||
}
|
||||
|
||||
if (!typeof(FsmState<T>).IsAssignableFrom(stateType))
|
||||
{
|
||||
throw new Exception(string.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 Exception("State type is invalid.");
|
||||
}
|
||||
|
||||
if (!typeof(FsmState<T>).IsAssignableFrom(stateType))
|
||||
{
|
||||
throw new Exception(string.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 Exception("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 Exception("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 Exception("Data name is invalid.");
|
||||
}
|
||||
|
||||
if (m_Datas == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
object data = null;
|
||||
if (m_Datas.TryGetValue(name, out 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,data);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置有限状态机数据。
|
||||
/// </summary>
|
||||
/// <param name="name">有限状态机数据名称。</param>
|
||||
/// <param name="data">要设置的有限状态机数据。</param>
|
||||
public void SetData(string name, object data)
|
||||
{
|
||||
if (string.IsNullOrEmpty(name))
|
||||
{
|
||||
throw new Exception("Data name is invalid.");
|
||||
}
|
||||
|
||||
if (m_Datas == null)
|
||||
{
|
||||
m_Datas = new Dictionary<string, object>(StringComparer.Ordinal);
|
||||
}
|
||||
|
||||
object oldData = GetData(name);
|
||||
oldData = null;
|
||||
|
||||
m_Datas[name] = data;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除有限状态机数据。
|
||||
/// </summary>
|
||||
/// <param name="name">有限状态机数据名称。</param>
|
||||
/// <returns>是否移除有限状态机数据成功。</returns>
|
||||
public bool RemoveData(string name)
|
||||
{
|
||||
if (string.IsNullOrEmpty(name))
|
||||
{
|
||||
throw new Exception("Data name is invalid.");
|
||||
}
|
||||
|
||||
if (m_Datas == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
object oldData = GetData(name);
|
||||
oldData = null;
|
||||
|
||||
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()
|
||||
{
|
||||
GameMemPool<Fsm<T>>.Free(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 切换当前有限状态机状态。
|
||||
/// </summary>
|
||||
/// <typeparam name="TState">要切换到的有限状态机状态类型。</typeparam>
|
||||
public 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 Exception("Current state is invalid.");
|
||||
}
|
||||
|
||||
FsmState<T> state = GetState(stateType);
|
||||
if (state == null)
|
||||
{
|
||||
throw new Exception(string.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);
|
||||
}
|
||||
|
||||
public void Init()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void Destroy()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/TEngine/Runtime/Fsm/Fsm.cs.meta
Normal file
11
Assets/TEngine/Runtime/Fsm/Fsm.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 88364685b9103df45b9d246548c35246
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
106
Assets/TEngine/Runtime/Fsm/FsmBase.cs
Normal file
106
Assets/TEngine/Runtime/Fsm/FsmBase.cs
Normal file
@@ -0,0 +1,106 @@
|
||||
using System;
|
||||
|
||||
namespace TEngine.Fsm
|
||||
{
|
||||
/// <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();
|
||||
}
|
||||
}
|
11
Assets/TEngine/Runtime/Fsm/FsmBase.cs.meta
Normal file
11
Assets/TEngine/Runtime/Fsm/FsmBase.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3a23e58e7b2ef2e469d1e6e295516fc5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
404
Assets/TEngine/Runtime/Fsm/FsmManager.cs
Normal file
404
Assets/TEngine/Runtime/Fsm/FsmManager.cs
Normal file
@@ -0,0 +1,404 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace TEngine.Fsm
|
||||
{
|
||||
/// <summary>
|
||||
/// 有限状态机管理器。
|
||||
/// </summary>
|
||||
public class FsmManager : UnitySingleton<FsmManager>, 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 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>
|
||||
public void Update()
|
||||
{
|
||||
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(Time.deltaTime, Time.unscaledDeltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 关闭并清理有限状态机管理器。
|
||||
/// </summary>
|
||||
protected override void OnDestroy()
|
||||
{
|
||||
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 Exception("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 Exception("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 Exception("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 Exception("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 Exception("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 Exception(string.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 Exception(string.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 Exception("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 Exception("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 Exception("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 Exception("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;
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/TEngine/Runtime/Fsm/FsmManager.cs.meta
Normal file
11
Assets/TEngine/Runtime/Fsm/FsmManager.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 313492502b69d854e94ccd7ca4eb436d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
103
Assets/TEngine/Runtime/Fsm/FsmState.cs
Normal file
103
Assets/TEngine/Runtime/Fsm/FsmState.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
using System;
|
||||
|
||||
namespace TEngine.Fsm
|
||||
{
|
||||
/// <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 Exception("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 Exception("FSM is invalid.");
|
||||
}
|
||||
|
||||
if (stateType == null)
|
||||
{
|
||||
throw new Exception("State type is invalid.");
|
||||
}
|
||||
|
||||
if (!typeof(FsmState<T>).IsAssignableFrom(stateType))
|
||||
{
|
||||
throw new Exception(string.Format("State type '{0}' is invalid.", stateType.FullName));
|
||||
}
|
||||
|
||||
fsmImplement.ChangeState(stateType);
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/TEngine/Runtime/Fsm/FsmState.cs.meta
Normal file
11
Assets/TEngine/Runtime/Fsm/FsmState.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5f37f11a7e625a445a24eeb4c9286423
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
178
Assets/TEngine/Runtime/Fsm/IFsm.cs
Normal file
178
Assets/TEngine/Runtime/Fsm/IFsm.cs
Normal file
@@ -0,0 +1,178 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace TEngine.Fsm
|
||||
{
|
||||
/// <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>
|
||||
/// <param name="name">有限状态机数据名称。</param>
|
||||
/// <returns>要获取的有限状态机数据。</returns>
|
||||
object GetData(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>
|
||||
/// <param name="data">要设置的有限状态机数据。</param>
|
||||
void SetData(string name, object data);
|
||||
|
||||
/// <summary>
|
||||
/// 移除有限状态机数据。
|
||||
/// </summary>
|
||||
/// <param name="name">有限状态机数据名称。</param>
|
||||
/// <returns>是否移除有限状态机数据成功。</returns>
|
||||
bool RemoveData(string name);
|
||||
|
||||
/// <summary>
|
||||
/// 切换状态机
|
||||
/// </summary>
|
||||
/// <typeparam name="TState"></typeparam>
|
||||
void ChangeState<TState>() where TState : FsmState<T>;
|
||||
}
|
||||
}
|
11
Assets/TEngine/Runtime/Fsm/IFsm.cs.meta
Normal file
11
Assets/TEngine/Runtime/Fsm/IFsm.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 65550ee2887b2fc4097289b3d9103891
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
174
Assets/TEngine/Runtime/Fsm/IFsmManager.cs
Normal file
174
Assets/TEngine/Runtime/Fsm/IFsmManager.cs
Normal file
@@ -0,0 +1,174 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace TEngine.Fsm
|
||||
{
|
||||
/// <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);
|
||||
}
|
||||
}
|
11
Assets/TEngine/Runtime/Fsm/IFsmManager.cs.meta
Normal file
11
Assets/TEngine/Runtime/Fsm/IFsmManager.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d57006ef47615e34e915854cdbd234bf
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
128
Assets/TEngine/Runtime/Fsm/TypeNamePair.cs
Normal file
128
Assets/TEngine/Runtime/Fsm/TypeNamePair.cs
Normal file
@@ -0,0 +1,128 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace TEngine
|
||||
{
|
||||
/// <summary>
|
||||
/// <20><><EFBFBD>ͺ<EFBFBD><CDBA><EFBFBD><EFBFBD>Ƶ<EFBFBD><C6B5><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Auto)]
|
||||
internal struct TypeNamePair : IEquatable<TypeNamePair>
|
||||
{
|
||||
private readonly Type m_Type;
|
||||
private readonly string m_Name;
|
||||
|
||||
/// <summary>
|
||||
/// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD>ͺ<EFBFBD><CDBA><EFBFBD><EFBFBD>Ƶ<EFBFBD><C6B5><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD>
|
||||
/// </summary>
|
||||
/// <param name="type"><3E><><EFBFBD>͡<EFBFBD></param>
|
||||
public TypeNamePair(Type type)
|
||||
: this(type, string.Empty)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD>ͺ<EFBFBD><CDBA><EFBFBD><EFBFBD>Ƶ<EFBFBD><C6B5><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD>
|
||||
/// </summary>
|
||||
/// <param name="type"><3E><><EFBFBD>͡<EFBFBD></param>
|
||||
/// <param name="name"><3E><><EFBFBD>ơ<EFBFBD></param>
|
||||
public TypeNamePair(Type type, string name)
|
||||
{
|
||||
if (type == null)
|
||||
{
|
||||
throw new Exception("Type is invalid.");
|
||||
}
|
||||
|
||||
m_Type = type;
|
||||
m_Name = name ?? string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <20><>ȡ<EFBFBD><C8A1><EFBFBD>͡<EFBFBD>
|
||||
/// </summary>
|
||||
public Type Type
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_Type;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <20><>ȡ<EFBFBD><C8A1><EFBFBD>ơ<EFBFBD>
|
||||
/// </summary>
|
||||
public string Name
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_Name;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <20><>ȡ<EFBFBD><C8A1><EFBFBD>ͺ<EFBFBD><CDBA><EFBFBD><EFBFBD>Ƶ<EFBFBD><C6B5><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>
|
||||
/// </summary>
|
||||
/// <returns><3E><><EFBFBD>ͺ<EFBFBD><CDBA><EFBFBD><EFBFBD>Ƶ<EFBFBD><C6B5><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD></returns>
|
||||
public override string ToString()
|
||||
{
|
||||
if (m_Type == null)
|
||||
{
|
||||
throw new Exception("Type is invalid.");
|
||||
}
|
||||
|
||||
string typeName = m_Type.FullName;
|
||||
return string.IsNullOrEmpty(m_Name) ? typeName : string.Format("{0}.{1}", typeName, m_Name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD>Ĺ<EFBFBD>ϣֵ<CFA3><D6B5>
|
||||
/// </summary>
|
||||
/// <returns><3E><><EFBFBD><EFBFBD><EFBFBD>Ĺ<EFBFBD>ϣֵ<CFA3><D6B5></returns>
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return m_Type.GetHashCode() ^ m_Name.GetHashCode();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <20>Ƚ϶<C8BD><CFB6><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD>
|
||||
/// </summary>
|
||||
/// <param name="obj">Ҫ<>ȽϵĶ<CFB5><C4B6><EFBFBD><EFBFBD><EFBFBD></param>
|
||||
/// <returns><3E><><EFBFBD>ȽϵĶ<CFB5><C4B6><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD></returns>
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return obj is TypeNamePair && Equals((TypeNamePair)obj);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <20>Ƚ϶<C8BD><CFB6><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD>
|
||||
/// </summary>
|
||||
/// <param name="value">Ҫ<>ȽϵĶ<CFB5><C4B6><EFBFBD><EFBFBD><EFBFBD></param>
|
||||
/// <returns><3E><><EFBFBD>ȽϵĶ<CFB5><C4B6><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD></returns>
|
||||
public bool Equals(TypeNamePair value)
|
||||
{
|
||||
return m_Type == value.m_Type && m_Name == value.m_Name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <20>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ȡ<EFBFBD>
|
||||
/// </summary>
|
||||
/// <param name="a">ֵ a<><61></param>
|
||||
/// <param name="b">ֵ b<><62></param>
|
||||
/// <returns><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ȡ<EFBFBD></returns>
|
||||
public static bool operator ==(TypeNamePair a, TypeNamePair b)
|
||||
{
|
||||
return a.Equals(b);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <20>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD>
|
||||
/// </summary>
|
||||
/// <param name="a">ֵ a<><61></param>
|
||||
/// <param name="b">ֵ b<><62></param>
|
||||
/// <returns><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD></returns>
|
||||
public static bool operator !=(TypeNamePair a, TypeNamePair b)
|
||||
{
|
||||
return !(a == b);
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/TEngine/Runtime/Fsm/TypeNamePair.cs.meta
Normal file
11
Assets/TEngine/Runtime/Fsm/TypeNamePair.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3185e3f5b6cd2f04ebff57b9c5a80b07
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user