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