Files
Continentis/Assets/Scripts/MainGame/Commands/Cmd_Functions.cs

108 lines
3.6 KiB
C#
Raw Normal View History

2025-10-23 00:49:44 -04:00
using System;
2026-03-20 11:56:50 -04:00
using Cysharp.Threading.Tasks;
2025-10-23 00:49:44 -04:00
using SLSFramework.General;
using UnityEngine;
using UnityEngine.Events;
namespace Continentis.MainGame.Commands
{
2026-03-20 11:56:50 -04:00
/// <summary>
/// 同步/延迟函数命令。新代码请改用 <see cref="Cmd.Do"/> / <see cref="Cmd.Wait"/> / <see cref="Cmd.After"/>。
/// </summary>
[Obsolete("请改用 Cmd.Do() / Cmd.Wait() / Cmd.After()。")]
2025-10-23 00:49:44 -04:00
public class Cmd_Function : CommandBase
{
private readonly float functionDuration;
private readonly UnityAction function;
private readonly bool executeAtStart;
2026-03-20 11:56:50 -04:00
public Cmd_Function(float functionDuration, UnityAction function, bool executeAtStart = true)
2025-10-23 00:49:44 -04:00
{
this.functionDuration = functionDuration;
this.function = function;
this.executeAtStart = executeAtStart;
}
2026-03-20 11:56:50 -04:00
public Cmd_Function(UnityAction function)
2025-10-23 00:49:44 -04:00
{
2026-03-20 11:56:50 -04:00
this.functionDuration = 0f;
2025-10-23 00:49:44 -04:00
this.function = function;
this.executeAtStart = true;
}
2026-03-20 11:56:50 -04:00
protected override async UniTask ExecuteAsync(CommandContext outerContext)
2025-10-23 00:49:44 -04:00
{
2026-03-20 11:56:50 -04:00
if (functionDuration > 0f)
2025-10-23 00:49:44 -04:00
{
2026-03-20 11:56:50 -04:00
if (executeAtStart)
2025-10-23 00:49:44 -04:00
{
function?.Invoke();
2026-03-20 11:56:50 -04:00
await UniTask.Delay(TimeSpan.FromSeconds(functionDuration));
}
else
{
await UniTask.Delay(TimeSpan.FromSeconds(functionDuration));
function?.Invoke();
2025-10-23 00:49:44 -04:00
}
}
2026-03-20 11:56:50 -04:00
else
{
function?.Invoke();
}
2025-10-23 00:49:44 -04:00
}
}
2026-03-20 11:56:50 -04:00
/// <summary>
/// 带参数的函数命令,参数由 selfContext 中的 "Target" 注入。
/// 新代码请改用闭包捕获参数,通过 <see cref="Cmd.Do"/> 执行。
/// </summary>
[Obsolete("请改用闭包捕获参数并通过 Cmd.Do() 执行。")]
2025-10-23 00:49:44 -04:00
public class Cmd_ParamFunction<T> : CommandBase where T : class
{
private readonly float functionDuration;
private readonly UnityAction<T> function;
private readonly bool executeAtStart;
2026-03-20 11:56:50 -04:00
public Cmd_ParamFunction(UnityAction<T> function, CommandContext selfContext = null, bool executeAtStart = true)
: base(selfContext)
2025-10-23 00:49:44 -04:00
{
2026-03-20 11:56:50 -04:00
this.functionDuration = 0f;
2025-10-23 00:49:44 -04:00
this.function = function;
this.executeAtStart = executeAtStart;
}
2026-03-20 11:56:50 -04:00
public Cmd_ParamFunction(float functionDuration, UnityAction<T> function, CommandContext selfContext = null, bool executeAtStart = true)
: base(selfContext)
2025-10-23 00:49:44 -04:00
{
this.functionDuration = functionDuration;
this.function = function;
this.executeAtStart = executeAtStart;
}
2026-03-20 11:56:50 -04:00
protected override async UniTask ExecuteAsync(CommandContext outerContext)
2025-10-23 00:49:44 -04:00
{
2026-03-20 11:56:50 -04:00
if (!selfContext.TryGet<T>(CommandContextKeys.Target, out T param))
2025-10-23 00:49:44 -04:00
{
2026-03-20 11:56:50 -04:00
Debug.LogWarning($"Cmd_ParamFunction<{typeof(T).Name}> 未能从 selfContext 中获取 Target 参数。");
2025-10-23 00:49:44 -04:00
}
2026-03-20 11:56:50 -04:00
if (functionDuration > 0f)
2025-10-23 00:49:44 -04:00
{
2026-03-20 11:56:50 -04:00
if (executeAtStart)
2025-10-23 00:49:44 -04:00
{
function?.Invoke(param);
2026-03-20 11:56:50 -04:00
await UniTask.Delay(TimeSpan.FromSeconds(functionDuration));
}
else
{
await UniTask.Delay(TimeSpan.FromSeconds(functionDuration));
function?.Invoke(param);
2025-10-23 00:49:44 -04:00
}
}
2026-03-20 11:56:50 -04:00
else
{
function?.Invoke(param);
}
2025-10-23 00:49:44 -04:00
}
}
}