Initial set up for item use
This commit is contained in:
@@ -314,10 +314,9 @@ public class BattleImpl : ScriptSource, IBattle
|
||||
{
|
||||
if (weatherName.HasValue)
|
||||
{
|
||||
if (!Library.ScriptResolver.TryResolve(ScriptCategory.Weather, weatherName.Value, out var script))
|
||||
if (!Library.ScriptResolver.TryResolve(ScriptCategory.Weather, weatherName.Value, null, out var script))
|
||||
throw new InvalidOperationException($"Weather script {weatherName} not found.");
|
||||
_weatherScript.Set(script);
|
||||
script.OnInitialize(Library, null);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -25,10 +25,10 @@ internal static class MoveTurnExecutor
|
||||
var secondaryEffect = moveData.SecondaryEffect;
|
||||
if (secondaryEffect != null)
|
||||
{
|
||||
if (moveChoice.User.Library.ScriptResolver.TryResolve(ScriptCategory.Move, secondaryEffect.Name, out var script))
|
||||
if (moveChoice.User.Library.ScriptResolver.TryResolve(ScriptCategory.Move, secondaryEffect.Name,
|
||||
secondaryEffect.Parameters, out var script))
|
||||
{
|
||||
moveChoice.Script.Set(script);
|
||||
script.OnInitialize(moveChoice.User.Library, secondaryEffect.Parameters);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -90,7 +90,9 @@ public static class TurnRunner
|
||||
case IFleeChoice fleeChoice:
|
||||
ExecuteFleeChoice(battle, fleeChoice);
|
||||
break;
|
||||
// TODO: Implement item choice types
|
||||
case IItemChoice itemChoice:
|
||||
ExecuteItemChoice(battle, itemChoice);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,4 +157,36 @@ public static class TurnRunner
|
||||
battle.ValidateBattleState();
|
||||
}
|
||||
|
||||
private static void ExecuteItemChoice(IBattle battle, IItemChoice itemChoice)
|
||||
{
|
||||
var user = itemChoice.User;
|
||||
var battleData = user.BattleData;
|
||||
if (battleData == null)
|
||||
return;
|
||||
if (!battle.Library.ScriptResolver.TryResolveBattleItemScript(itemChoice.Item, out var itemScript))
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (!itemScript.IsItemUsable)
|
||||
return;
|
||||
itemScript.OnInitialize(itemChoice.Item.BattleEffect!.Parameters);
|
||||
IPokemon? target = null;
|
||||
if (itemChoice is { TargetSide: not null, TargetPosition: not null })
|
||||
{
|
||||
var side = battle.Sides[itemChoice.TargetSide.Value];
|
||||
target = side.Pokemon[itemChoice.TargetPosition.Value];
|
||||
}
|
||||
|
||||
var requiresTarget = itemScript.RequiresTarget;
|
||||
if (requiresTarget)
|
||||
{
|
||||
target ??= user;
|
||||
itemScript.OnUseWithTarget(target);
|
||||
}
|
||||
else
|
||||
{
|
||||
itemScript.OnUse();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,19 +8,40 @@ namespace PkmnLib.Dynamic.Models.Choices;
|
||||
/// </summary>
|
||||
public interface IItemChoice : ITurnChoice
|
||||
{
|
||||
/// <summary>
|
||||
/// The item that is used.
|
||||
/// </summary>
|
||||
public IItem Item { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The side the move is targeted at.
|
||||
/// </summary>
|
||||
byte? TargetSide { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The position the move is targeted at.
|
||||
/// </summary>
|
||||
byte? TargetPosition { get; }
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IItemChoice"/>
|
||||
public class ItemChoice : TurnChoice, IItemChoice
|
||||
{
|
||||
public ItemChoice(IPokemon user, IItem item) : base(user)
|
||||
public ItemChoice(IPokemon user, IItem item, byte? targetSide, byte? targetPosition) : base(user)
|
||||
{
|
||||
Item = item;
|
||||
TargetSide = targetSide;
|
||||
TargetPosition = targetPosition;
|
||||
}
|
||||
|
||||
public IItem Item { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte? TargetSide { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte? TargetPosition { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override int ScriptCount => User.ScriptCount;
|
||||
|
||||
|
||||
@@ -46,10 +46,10 @@ public class MoveChoice : TurnChoice, IMoveChoice
|
||||
var secondaryEffect = usedMove.MoveData.SecondaryEffect;
|
||||
if (secondaryEffect != null)
|
||||
{
|
||||
if (user.Library.ScriptResolver.TryResolve(ScriptCategory.Move, secondaryEffect.Name, out var script))
|
||||
if (user.Library.ScriptResolver.TryResolve(ScriptCategory.Move, secondaryEffect.Name,
|
||||
secondaryEffect.Parameters, out var script))
|
||||
{
|
||||
Script.Set(script);
|
||||
script.OnInitialize(user.Library, secondaryEffect.Parameters);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -465,7 +465,7 @@ public class PokemonImpl : ScriptSource, IPokemon
|
||||
|
||||
if (serializedPokemon.Status != null)
|
||||
{
|
||||
if (!library.ScriptResolver.TryResolve(ScriptCategory.Status, serializedPokemon.Status,
|
||||
if (!library.ScriptResolver.TryResolve(ScriptCategory.Status, serializedPokemon.Status, null,
|
||||
out var statusScript))
|
||||
throw new KeyNotFoundException($"Status script {serializedPokemon.Status} not found");
|
||||
StatusScript.Set(statusScript);
|
||||
@@ -668,7 +668,7 @@ public class PokemonImpl : ScriptSource, IPokemon
|
||||
{
|
||||
if (HeldItem is null)
|
||||
return false;
|
||||
if (!Library.ScriptResolver.TryResolveItemScript(HeldItem, out _))
|
||||
if (!Library.ScriptResolver.TryResolveBattleItemScript(HeldItem, out _))
|
||||
return false;
|
||||
// TODO: actually consume the item
|
||||
throw new NotImplementedException();
|
||||
@@ -777,10 +777,10 @@ public class PokemonImpl : ScriptSource, IPokemon
|
||||
AbilityScript.Clear();
|
||||
if (!Library.StaticLibrary.Abilities.TryGet(newAbility, out var ability))
|
||||
throw new KeyNotFoundException($"Ability {newAbility} not found.");
|
||||
if (Library.ScriptResolver.TryResolve(ScriptCategory.Ability, newAbility, out var abilityScript))
|
||||
if (Library.ScriptResolver.TryResolve(ScriptCategory.Ability, newAbility, ability.Parameters,
|
||||
out var abilityScript))
|
||||
{
|
||||
AbilityScript.Set(abilityScript);
|
||||
abilityScript.OnInitialize(Library, ability.Parameters);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user