2025-10-03 00:02:43 -04:00
|
|
|
|
using System.Collections.Generic;
|
2025-10-24 09:11:22 -04:00
|
|
|
|
using UnityEngine;
|
2025-10-03 00:02:43 -04:00
|
|
|
|
|
2025-10-23 00:49:44 -04:00
|
|
|
|
namespace SLSFramework.General
|
2025-10-03 00:02:43 -04:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2025-10-23 00:49:44 -04:00
|
|
|
|
/// 指令内容 (Command Context)
|
2025-10-03 00:02:43 -04:00
|
|
|
|
/// 包含了指令执行时可能需要的所有游戏状态信息。
|
2025-10-23 00:49:44 -04:00
|
|
|
|
/// 指令组开始执行时创建的CommandContext,会被传递给每一个子指令。
|
|
|
|
|
|
/// 在ICommand内置的CommandContext中,则包含了该指令执行时特有的信息。
|
2025-10-03 00:02:43 -04:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class CommandContext
|
|
|
|
|
|
{
|
2025-10-23 00:49:44 -04:00
|
|
|
|
public readonly Dictionary<string, object> context;
|
|
|
|
|
|
|
|
|
|
|
|
public CommandContext()
|
|
|
|
|
|
{
|
|
|
|
|
|
context = new Dictionary<string, object>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-24 09:11:22 -04:00
|
|
|
|
public CommandContext((string, object)[] pairs)
|
2025-10-23 00:49:44 -04:00
|
|
|
|
{
|
2025-10-24 09:11:22 -04:00
|
|
|
|
context = new Dictionary<string, object>();
|
|
|
|
|
|
foreach ((string, object) pair in pairs)
|
2025-10-23 00:49:44 -04:00
|
|
|
|
{
|
2025-10-24 09:11:22 -04:00
|
|
|
|
context[pair.Item1] = pair.Item2;
|
|
|
|
|
|
}
|
2025-10-23 00:49:44 -04:00
|
|
|
|
}
|
2025-10-24 09:11:22 -04:00
|
|
|
|
|
|
|
|
|
|
public CommandContext(Dictionary<string, object> initialInfo)
|
2025-10-23 00:49:44 -04:00
|
|
|
|
{
|
|
|
|
|
|
context = new Dictionary<string, object>();
|
|
|
|
|
|
foreach (var pair in initialInfo)
|
|
|
|
|
|
{
|
|
|
|
|
|
context[pair.Key] = pair.Value;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-10-24 09:11:22 -04:00
|
|
|
|
|
2025-10-23 00:49:44 -04:00
|
|
|
|
public CommandContext Clone()
|
|
|
|
|
|
{
|
|
|
|
|
|
var newContext = new CommandContext();
|
|
|
|
|
|
foreach (var pair in context)
|
|
|
|
|
|
{
|
|
|
|
|
|
newContext.context[pair.Key] = pair.Value;
|
|
|
|
|
|
}
|
|
|
|
|
|
return newContext;
|
|
|
|
|
|
}
|
2025-10-24 09:11:22 -04:00
|
|
|
|
|
|
|
|
|
|
public CommandContext Merge(CommandContext other)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var pair in other.context)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.context[pair.Key] = pair.Value;
|
|
|
|
|
|
}
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
2025-10-23 00:49:44 -04:00
|
|
|
|
|
|
|
|
|
|
public T GetInfo<T>(string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (context.TryGetValue(key, out object value) && value is T typedValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
return typedValue;
|
|
|
|
|
|
}
|
2025-10-24 09:11:22 -04:00
|
|
|
|
|
|
|
|
|
|
Debug.LogWarning($"CommandContext 中不存在键 '{key}',或其类型不匹配。返回默认值。");
|
2025-10-23 00:49:44 -04:00
|
|
|
|
return default;
|
|
|
|
|
|
}
|
2025-10-03 00:02:43 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|