55 lines
1.9 KiB
C#
55 lines
1.9 KiB
C#
namespace PkmnLib.Plugin.Gen7.Scripts.Abilities;
|
|
|
|
/// <summary>
|
|
/// Illusion is an ability that disguises the Pokémon as the last non-fainted Pokémon in the party until it takes damage.
|
|
///
|
|
/// <see href="https://bulbapedia.bulbagarden.net/wiki/Illusion_(Ability)">Bulbapedia - Illusion</see>
|
|
/// </summary>
|
|
[Script(ScriptCategory.Ability, "illusion")]
|
|
public class Illusion : Script
|
|
{
|
|
private IPokemon? _pokemon;
|
|
|
|
/// <inheritdoc />
|
|
public override void OnAddedToParent(IScriptSource source)
|
|
{
|
|
if (source is not IPokemon pokemon)
|
|
throw new InvalidOperationException("Illusion can only be added to a Pokemon script source.");
|
|
_pokemon = pokemon;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override void OnSwitchIn(IPokemon 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);
|
|
if (lastNonFaintedPokemon is null || lastNonFaintedPokemon == pokemon)
|
|
return;
|
|
|
|
pokemon.SetDisplaySpecies(lastNonFaintedPokemon.Species, lastNonFaintedPokemon.Form);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override void OnRemove()
|
|
{
|
|
if (_pokemon is null)
|
|
return;
|
|
|
|
_pokemon.SetDisplaySpecies(null, null);
|
|
_pokemon.BattleData?.Battle.EventHook.Invoke(new AbilityTriggerEvent(_pokemon));
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override void OnIncomingHit(IExecutingMove move, IPokemon target, byte hit)
|
|
{
|
|
if (_pokemon?.BattleData?.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));
|
|
}
|
|
} |