using System.Collections.Generic;
namespace SLSFramework.General
{
///
/// 指令内容 (Command Context)
/// 包含了指令执行时可能需要的所有游戏状态信息。
/// 指令组开始执行时创建的CommandContext,会被传递给每一个子指令。
/// 在ICommand内置的CommandContext中,则包含了该指令执行时特有的信息。
///
public class CommandContext
{
public readonly Dictionary context;
public CommandContext()
{
context = new Dictionary();
}
public CommandContext(string key, object value)
{
context = new Dictionary
{
{ key, value }
};
}
public CommandContext(List> initialInfo)
{
context = new Dictionary();
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 T GetInfo(string key)
{
if (context.TryGetValue(key, out object value) && value is T typedValue)
{
return typedValue;
}
return default;
}
}
}