using System; using System.Collections.Generic; using Continentis.MainGame.Character; using Cysharp.Threading.Tasks; namespace SLSFramework.General { /// /// 命令静态工厂。 /// Mod 制作者通过此类创建常用命令,无需直接实例化具体命令类。 /// public static class Cmd { // ── 基础工厂 ───────────────────────────────────────────────────── /// 创建一条等待指定秒数后完成的命令。 public static CommandBase Wait(float seconds) { return new WaitCommand(seconds); } /// 创建一条执行同步 Action 的命令。 public static CommandBase Do(Action action) { return new ActionCommand(action); } /// 创建一条执行异步 UniTask 的命令。 public static CommandBase Do(Func asyncAction) { return new AsyncActionCommand(asyncAction); } /// 创建一条延迟指定秒数后执行同步 Action 的命令。 public static CommandBase After(float seconds, Action action) { return new ActionCommand(action).SetDelay(seconds); } /// /// 创建一条"等待外部信号"命令。 /// 调用返回的 Action 时,命令立即完成,队列继续执行。 /// 典型用法:等待玩家点击按钮确认。 /// /// 外部调用此委托以释放命令。 public static CommandBase WaitForSignal(out Action signal) { var tcs = new UniTaskCompletionSource(); signal = () => tcs.TrySetResult(); return new WaitForSignalCommand(tcs); } // ── 组合工厂 ───────────────────────────────────────────────────── /// 创建一个顺序执行的命令组。 public static CommandGroup Sequential(params CommandBase[] cmds) { return new CommandGroup(ExecutionMode.Sequential, cmds); } /// 创建一个并行执行的命令组。 public static CommandGroup Parallel(params CommandBase[] cmds) { return new CommandGroup(ExecutionMode.Parallel, cmds); } // ── Target 遍历工厂 ─────────────────────────────────────────────── /// /// 对 列表中的每个目标调用 , /// 将结果按顺序组合为一个 Sequential 。 /// public static CommandGroup ForEachTarget( List targets, Func factory) { var group = new CommandGroup(ExecutionMode.Sequential); foreach (CharacterBase target in targets) { CharacterBase captured = target; group.AddCommand(factory(captured)); } return group; } /// /// 对 列表中的每个目标调用 , /// 将结果按顺序组合为一个 Sequential 。 /// public static CommandGroup ForEachTarget( List targets, Func factory) { var group = new CommandGroup(ExecutionMode.Sequential); foreach (CharacterBase target in targets) { CharacterBase captured = target; group.AddCommand(factory(captured)); } return group; } // ── 内部实现类 ──────────────────────────────────────────────────── private sealed class WaitCommand : CommandBase { private readonly float seconds; public WaitCommand(float seconds) { this.seconds = seconds; } protected override async UniTask ExecuteAsync(CommandContext outerContext) { await UniTask.Delay(TimeSpan.FromSeconds(seconds)); } } private sealed class ActionCommand : CommandBase { private readonly Action action; public ActionCommand(Action action) { this.action = action; } protected override UniTask ExecuteAsync(CommandContext outerContext) { action?.Invoke(); return UniTask.CompletedTask; } } private sealed class AsyncActionCommand : CommandBase { private readonly Func asyncAction; public AsyncActionCommand(Func asyncAction) { this.asyncAction = asyncAction; } protected override async UniTask ExecuteAsync(CommandContext outerContext) { if (asyncAction != null) await asyncAction(); } } private sealed class WaitForSignalCommand : CommandBase { private readonly UniTaskCompletionSource tcs; public WaitForSignalCommand(UniTaskCompletionSource tcs) { this.tcs = tcs; } protected override async UniTask ExecuteAsync(CommandContext outerContext) { await tcs.Task; } } } }