namespace PkmnLib.Plugin.Gen7.Scripts.Abilities; /// /// Illusion is an ability that disguises the Pokémon as the last non-fainted Pokémon in the party until it takes damage. /// /// Bulbapedia - Illusion /// [Script(ScriptCategory.Ability, "illusion")] public class Illusion : Script, IScriptOnIncomingHit, IScriptOnSwitchIn { private IBattlePokemon? _pokemon; /// public override void OnAddedToParent(IScriptSource source) { if (source is not IBattlePokemon pokemon) throw new InvalidOperationException("Illusion can only be added to a Pokemon script source."); _pokemon = pokemon; } /// public void OnSwitchIn(IBattlePokemon pokemon, byte position) { var lastNonFaintedPokemon = pokemon.Battle.Parties.FirstOrDefault(p => p.BattlePokemon.Contains(pokemon)) ?.BattlePokemon.WhereNotNull().FirstOrDefault(x => x.IsUsable); if (lastNonFaintedPokemon is null || lastNonFaintedPokemon == pokemon) return; pokemon.SetDisplaySpecies(lastNonFaintedPokemon.Species, lastNonFaintedPokemon.Form); } /// public override void OnRemove() { if (_pokemon is null) return; _pokemon.SetDisplaySpecies(null, null); _pokemon.Battle.EventHook.Invoke(new AbilityTriggerEvent(_pokemon)); } /// public void OnIncomingHit(IExecutingMove move, IBattlePokemon target, byte hit) { if (_pokemon?.Battle is null) return; // Remove the illusion when the Pokémon takes damage _pokemon.SetDisplaySpecies(null, null); _pokemon.Battle.EventHook.Invoke(new AbilityTriggerEvent(_pokemon)); } }