mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
feat(state): 为状态基类添加销毁功能支持
- 实现 IDisposable 接口以支持资源清理 - 添加 Destroy 方法用于状态销毁和资源释放 - 在状态机切换时调用 Destroy 替代 Dispose - 允许子类重写 Destroy 方法执行特定清理操作 - 确保状态切换时正确释放相关资源
This commit is contained in:
parent
00053e67e5
commit
103792f178
@ -1,6 +1,7 @@
|
||||
using GFramework.Core.Abstractions.architecture;
|
||||
using GFramework.Core.Abstractions.rule;
|
||||
using GFramework.Core.Abstractions.state;
|
||||
using IDisposable = GFramework.Core.Abstractions.lifecycle.IDisposable;
|
||||
|
||||
namespace GFramework.Core.state;
|
||||
|
||||
@ -9,7 +10,7 @@ namespace GFramework.Core.state;
|
||||
/// 提供基础的状态管理功能和架构上下文访问能力
|
||||
/// 实现了IState和IContextAware接口
|
||||
/// </summary>
|
||||
public class ContextAwareStateBase : IState, IContextAware
|
||||
public class ContextAwareStateBase : IState, IContextAware, IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// 架构上下文引用,用于访问架构相关的服务和数据
|
||||
@ -34,12 +35,20 @@ public class ContextAwareStateBase : IState, IContextAware
|
||||
return _context!;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 销毁当前状态,释放相关资源
|
||||
/// 子类可重写此方法以执行特定的清理操作
|
||||
/// </summary>
|
||||
public virtual void Destroy()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 进入状态时调用的方法
|
||||
/// 子类可重写此方法以实现特定的状态进入逻辑
|
||||
/// </summary>
|
||||
/// <param name="fromState">从哪个状态转换而来,可能为null表示初始状态</param>
|
||||
public virtual void OnEnter(IState? fromState)
|
||||
/// <param name="from">从哪个状态转换而来,可能为null表示初始状态</param>
|
||||
public virtual void OnEnter(IState? from)
|
||||
{
|
||||
}
|
||||
|
||||
@ -47,8 +56,8 @@ public class ContextAwareStateBase : IState, IContextAware
|
||||
/// 退出状态时调用的方法
|
||||
/// 子类可重写此方法以实现特定的状态退出逻辑
|
||||
/// </summary>
|
||||
/// <param name="toState">将要转换到的目标状态,可能为null表示结束状态</param>
|
||||
public virtual void OnExit(IState? toState)
|
||||
/// <param name="to">将要转换到的目标状态,可能为null表示结束状态</param>
|
||||
public virtual void OnExit(IState? to)
|
||||
{
|
||||
}
|
||||
|
||||
@ -56,9 +65,9 @@ public class ContextAwareStateBase : IState, IContextAware
|
||||
/// 判断当前状态是否可以转换到目标状态
|
||||
/// 子类可重写此方法以实现自定义的状态转换规则
|
||||
/// </summary>
|
||||
/// <param name="targetState">希望转换到的目标状态对象</param>
|
||||
/// <param name="target">希望转换到的目标状态对象</param>
|
||||
/// <returns>如果允许转换则返回true,否则返回false</returns>
|
||||
public virtual bool CanTransitionTo(IState targetState)
|
||||
public virtual bool CanTransitionTo(IState target)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -4,6 +4,7 @@ using GFramework.Core.Abstractions.rule;
|
||||
using GFramework.Core.Abstractions.state;
|
||||
using GFramework.Core.Abstractions.system;
|
||||
using GFramework.Core.extensions;
|
||||
using IDisposable = GFramework.Core.Abstractions.lifecycle.IDisposable;
|
||||
|
||||
namespace GFramework.Core.state;
|
||||
|
||||
@ -71,7 +72,7 @@ public class ContextAwareStateMachine : StateMachine, ISystem
|
||||
// 清理所有状态
|
||||
foreach (var state in States.Values.OfType<IDisposable>())
|
||||
{
|
||||
state.Dispose();
|
||||
state.Destroy();
|
||||
}
|
||||
|
||||
States.Clear();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user