using System;
using Cysharp.Threading.Tasks;
namespace SLSFramework.General
{
///
/// 命令基类。子类重写 实现具体逻辑。
/// 框架通过 调用,先处理可选延迟再执行核心逻辑。
///
public abstract class CommandBase
{
/// 命令自身的私有上下文,可携带局部参数。
public CommandContext selfContext;
private float delayBeforeExecute;
public CommandBase(CommandContext selfContext = null)
{
this.selfContext = selfContext ?? new CommandContext();
}
/// 设置命令执行前的延迟秒数,返回自身支持链式调用。
public CommandBase SetDelay(float seconds)
{
delayBeforeExecute = seconds;
return this;
}
///
/// 框架调用入口:先执行可选延迟,再调用 。
/// 子类不应重写此方法。
///
public async UniTask RunAsync(CommandContext outerContext)
{
if (delayBeforeExecute > 0f)
await UniTask.Delay(TimeSpan.FromSeconds(delayBeforeExecute));
await ExecuteAsync(outerContext);
}
/// 子类重写此方法以实现命令的核心逻辑。
protected virtual async UniTask ExecuteAsync(CommandContext outerContext)
{
await UniTask.CompletedTask;
}
/// 浅拷贝当前命令,selfContext 也会被拷贝一份新实例。
public virtual CommandBase Clone()
{
CommandBase clone = (CommandBase)MemberwiseClone();
clone.selfContext = selfContext.Clone();
return clone;
}
}
}