diff --git a/GFramework.Core.Abstractions/state/IStateMachine.cs b/GFramework.Core.Abstractions/state/IStateMachine.cs index 7922b63..1e36c85 100644 --- a/GFramework.Core.Abstractions/state/IStateMachine.cs +++ b/GFramework.Core.Abstractions/state/IStateMachine.cs @@ -17,13 +17,13 @@ public interface IStateMachine /// 注册一个状态到状态机中 /// /// 要注册的状态实例 - void Register(IState state); + IStateMachine Register(IState state); /// /// 从状态机中注销指定类型的状态 /// /// 要注销的状态类型,必须实现IState接口 - void Unregister() where T : IState; + IStateMachine Unregister() where T : IState; /// /// 检查是否可以切换到指定类型的状态 diff --git a/GFramework.Core/state/StateMachine.cs b/GFramework.Core/state/StateMachine.cs index a2f1a0a..7a270ed 100644 --- a/GFramework.Core/state/StateMachine.cs +++ b/GFramework.Core/state/StateMachine.cs @@ -24,24 +24,26 @@ public class StateMachine(int maxHistorySize = 10) : IStateMachine /// 注册一个状态到状态机中 /// /// 要注册的状态实例 - public void Register(IState state) + public IStateMachine Register(IState state) { lock (_lock) { States[state.GetType()] = state; } + + return this; } /// /// 从状态机中注销指定类型的状态 /// /// 要注销的状态类型 - public void Unregister() where T : IState + public IStateMachine Unregister() where T : IState { lock (_lock) { var type = typeof(T); - if (!States.TryGetValue(type, out var state)) return; + if (!States.TryGetValue(type, out var state)) return this; // 如果当前状态是要注销的状态,则先执行退出逻辑 if (Current == state) @@ -60,6 +62,8 @@ public class StateMachine(int maxHistorySize = 10) : IStateMachine States.Remove(type); } + + return this; } ///