Files
Continentis/Assets/Scripts/ScriptExtensions/CommandQueue/CommandContext.cs
SoulliesOfficial 76157e3cb1 继续
2025-10-24 09:11:22 -04:00

69 lines
2.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Collections.Generic;
using UnityEngine;
namespace SLSFramework.General
{
/// <summary>
/// 指令内容 (Command Context)
/// 包含了指令执行时可能需要的所有游戏状态信息。
/// 指令组开始执行时创建的CommandContext会被传递给每一个子指令。
/// 在ICommand内置的CommandContext中则包含了该指令执行时特有的信息。
/// </summary>
public class CommandContext
{
public readonly Dictionary<string, object> context;
public CommandContext()
{
context = new Dictionary<string, object>();
}
public CommandContext((string, object)[] pairs)
{
context = new Dictionary<string, object>();
foreach ((string, object) pair in pairs)
{
context[pair.Item1] = pair.Item2;
}
}
public CommandContext(Dictionary<string, object> initialInfo)
{
context = new Dictionary<string, object>();
foreach (var pair in initialInfo)
{
context[pair.Key] = pair.Value;
}
}
public CommandContext Clone()
{
var newContext = new CommandContext();
foreach (var pair in context)
{
newContext.context[pair.Key] = pair.Value;
}
return newContext;
}
public CommandContext Merge(CommandContext other)
{
foreach (var pair in other.context)
{
this.context[pair.Key] = pair.Value;
}
return this;
}
public T GetInfo<T>(string key)
{
if (context.TryGetValue(key, out object value) && value is T typedValue)
{
return typedValue;
}
Debug.LogWarning($"CommandContext 中不存在键 '{key}',或其类型不匹配。返回默认值。");
return default;
}
}
}