Initial set up for item use
This commit is contained in:
@@ -9,33 +9,57 @@ namespace PkmnLib.Dynamic.ScriptHandling;
|
||||
/// </summary>
|
||||
public class ScriptResolver
|
||||
{
|
||||
private Dictionary<(ScriptCategory, StringKey), Func<Script>> _scriptCtors;
|
||||
private IReadOnlyDictionary<(ScriptCategory, StringKey), Func<Script>> _scriptCtors;
|
||||
private IReadOnlyDictionary<StringKey, Func<ItemScript>> _itemScriptCtors;
|
||||
|
||||
/// <inheritdoc cref="ScriptResolver"/>
|
||||
public ScriptResolver(Dictionary<(ScriptCategory, StringKey), Func<Script>> scriptCtors)
|
||||
public ScriptResolver(IReadOnlyDictionary<(ScriptCategory, StringKey), Func<Script>> scriptCtors,
|
||||
IReadOnlyDictionary<StringKey, Func<ItemScript>> itemScriptCtors)
|
||||
{
|
||||
_scriptCtors = scriptCtors;
|
||||
_itemScriptCtors = itemScriptCtors;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Try and create a new script for the given category and key. If the script does not exist, return false.
|
||||
/// </summary>
|
||||
public bool TryResolve(ScriptCategory category, StringKey key, [MaybeNullWhen(false)] out Script script)
|
||||
public bool TryResolve(ScriptCategory category, StringKey key, IReadOnlyDictionary<StringKey, object?>? parameters,
|
||||
[MaybeNullWhen(false)] out Script script)
|
||||
{
|
||||
if (!_scriptCtors.TryGetValue((category, key), out var scriptCtor))
|
||||
{
|
||||
script = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
script = scriptCtor();
|
||||
if (parameters != null)
|
||||
{
|
||||
script.OnInitialize(parameters);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Try and resolve an item script for the given item. If the item does not have a script, return false.
|
||||
/// </summary>
|
||||
public bool TryResolveItemScript(IItem item, [MaybeNullWhen(false)] out Script script)
|
||||
public bool TryResolveBattleItemScript(IItem item, [MaybeNullWhen(false)] out ItemScript script)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var effect = item.BattleEffect;
|
||||
if (effect == null)
|
||||
{
|
||||
script = null;
|
||||
return false;
|
||||
}
|
||||
if (!_itemScriptCtors.TryGetValue(effect.Name, out var scriptCtor))
|
||||
{
|
||||
script = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
script = scriptCtor();
|
||||
script.OnInitialize(effect.Parameters);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user