Move all battle state from IPokemon to an ephemeral IBattlePokemon wrapper

This commit is contained in:
2026-08-28 15:20:26 +02:00
parent 942be8eaeb
commit 8a2733a0a9
859 changed files with 4408 additions and 5331 deletions

View File

@@ -8,24 +8,21 @@ namespace PkmnLib.Plugin.Gen7.Scripts.Abilities;
[Script(ScriptCategory.Ability, "illusion")]
public class Illusion : Script, IScriptOnIncomingHit, IScriptOnSwitchIn
{
private IPokemon? _pokemon;
private IBattlePokemon? _pokemon;
/// <inheritdoc />
public override void OnAddedToParent(IScriptSource source)
{
if (source is not IPokemon pokemon)
if (source is not IBattlePokemon pokemon)
throw new InvalidOperationException("Illusion can only be added to a Pokemon script source.");
_pokemon = pokemon;
}
/// <inheritdoc />
public void OnSwitchIn(IPokemon pokemon, byte position)
public void OnSwitchIn(IBattlePokemon pokemon, byte position)
{
var battleData = pokemon.BattleData;
if (battleData is null)
return;
var lastNonFaintedPokemon = battleData.Battle.Parties.FirstOrDefault(p => p.Party.Any(pkmn => pkmn == pokemon))
?.Party.WhereNotNull().FirstOrDefault(x => x.IsUsable);
var lastNonFaintedPokemon = pokemon.Battle.Parties.FirstOrDefault(p => p.BattlePokemon.Contains(pokemon))
?.BattlePokemon.WhereNotNull().FirstOrDefault(x => x.IsUsable);
if (lastNonFaintedPokemon is null || lastNonFaintedPokemon == pokemon)
return;
@@ -39,17 +36,17 @@ public class Illusion : Script, IScriptOnIncomingHit, IScriptOnSwitchIn
return;
_pokemon.SetDisplaySpecies(null, null);
_pokemon.BattleData?.Battle.EventHook.Invoke(new AbilityTriggerEvent(_pokemon));
_pokemon.Battle.EventHook.Invoke(new AbilityTriggerEvent(_pokemon));
}
/// <inheritdoc />
public void OnIncomingHit(IExecutingMove move, IPokemon target, byte hit)
public void OnIncomingHit(IExecutingMove move, IBattlePokemon target, byte hit)
{
if (_pokemon?.BattleData?.Battle is null)
if (_pokemon?.Battle is null)
return;
// Remove the illusion when the Pokémon takes damage
_pokemon.SetDisplaySpecies(null, null);
_pokemon.BattleData.Battle.EventHook.Invoke(new AbilityTriggerEvent(_pokemon));
_pokemon.Battle.EventHook.Invoke(new AbilityTriggerEvent(_pokemon));
}
}