Fixes and reworking of item use
All checks were successful
Build / Build (push) Successful in 57s

This commit is contained in:
2025-11-08 11:43:07 +01:00
parent fa05cdd773
commit 21ec4b28c7
11 changed files with 150 additions and 34 deletions

View File

@@ -36,7 +36,7 @@ public abstract class ItemScript : IDeepCloneable
/// <summary>
/// Returns whether the item requires a target to be used.
/// </summary>
public virtual bool RequiresTarget => false;
public virtual ItemTargetType TargetType => ItemTargetType.None;
/// <summary>
/// Returns whether the item is usable on the given target.

View File

@@ -29,7 +29,7 @@ public abstract class PokeballScript : ItemScript
public override bool IsItemUsable => true;
/// <inheritdoc />
public override bool RequiresTarget => true;
public override ItemTargetType TargetType => ItemTargetType.FoePokemon;
/// <inheritdoc />
public override bool IsTargetValid(IPokemon target) =>

View File

@@ -100,8 +100,8 @@ public static class ScriptExecution
/// <summary>
/// Executes a script on an item.
/// </summary>
public static void RunItemScript(this IItem item, ScriptResolver scriptResolver, IPokemon? target,
EventHook eventHook)
public static void RunItemScript(this IItem item, ScriptResolver scriptResolver, IPokemon? target, IPokemon user,
IBattle battle, EventHook eventHook)
{
if (!scriptResolver.TryResolveBattleItemScript(item, out var itemScript))
{
@@ -111,16 +111,20 @@ public static class ScriptExecution
return;
itemScript.OnInitialize(item.BattleEffect!.Parameters);
var requiresTarget = itemScript.RequiresTarget;
if (requiresTarget)
{
if (target == null)
throw new ArgumentNullException(nameof(target), "Item script requires a target.");
itemScript.OnUseWithTarget(target, eventHook);
}
else
var targetType = itemScript.TargetType;
if (targetType == ItemTargetType.None)
{
itemScript.OnUse(eventHook);
}
else
{
if (target == null)
throw new ArgumentNullException(nameof(target), "Item script requires a target.");
if (!targetType.IsValidTarget(battle, user, target))
throw new InvalidOperationException("Item script target is not valid for the required target type.");
if (!itemScript.IsTargetValid(target))
throw new InvalidOperationException("Item script target is not valid.");
itemScript.OnUseWithTarget(target, eventHook);
}
}
}