Files
PkmnLib.NET/Plugins/PkmnLib.Plugin.Gen7/Scripts/Abilities/ZenMode.cs

34 lines
1.3 KiB
C#

namespace PkmnLib.Plugin.Gen7.Scripts.Abilities;
/// <summary>
/// Zen Mode is an ability that changes the Pokémon's form when its HP falls below half.
///
/// <see href="https://bulbapedia.bulbagarden.net/wiki/Zen_Mode_(Ability)">Bulbapedia - Zen Mode</see>
/// </summary>
[Script(ScriptCategory.Ability, "zen_mode")]
public class ZenMode : Script, IScriptOnEndTurn, IScriptOnSwitchIn
{
/// <inheritdoc />
public void OnSwitchIn(IBattlePokemon pokemon, byte position) => ChangeFormIfNeeded(pokemon);
/// <inheritdoc />
public void OnEndTurn(IScriptSource owner, IBattle battle) => ChangeFormIfNeeded(owner as IBattlePokemon);
private static void ChangeFormIfNeeded(IBattlePokemon? pokemon)
{
if (pokemon is null)
return;
if (pokemon.Species.Name != SpeciesNames.Darmanitan || pokemon.Battle == null)
return;
if (pokemon.CurrentHealth < pokemon.MaxHealth / 2 && pokemon.Form.Name != FormNames.Zen &&
pokemon.Species.TryGetForm(FormNames.Zen, out var zenForm))
{
pokemon.ChangeForm(zenForm);
}
else if (pokemon.CurrentHealth >= pokemon.MaxHealth / 2 && pokemon.Form.Name == FormNames.Zen)
{
pokemon.ChangeForm(pokemon.Species.GetDefaultForm());
}
}
}