Initial set up for item use

This commit is contained in:
2025-01-10 11:11:50 +01:00
parent 85ea31f7cd
commit 0518499a4c
23 changed files with 305 additions and 59 deletions

View File

@@ -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;
}
}