namespace PkmnLib.Plugin.Gen7.Scripts.Abilities;
///
/// Forecast is an ability that changes the Pokémon's form depending on the weather.
/// This ability is exclusive to Castform.
///
/// Bulbapedia - Forecast
///
[Script(ScriptCategory.Ability, "forecast")]
public class Forecast : Script
{
private IPokemon? _pokemon;
///
public override void OnAddedToParent(IScriptSource source)
{
if (source is not IPokemon pokemon)
throw new InvalidOperationException("Forecast can only be added to a Pokemon script source.");
_pokemon = pokemon;
}
///
public override void OnSwitchIn(IPokemon pokemon, byte position)
{
ChangeForm(pokemon, pokemon.BattleData?.Battle.WeatherName);
}
///
public override void OnWeatherChange(IBattle battle, StringKey? weatherName, StringKey? oldWeatherName)
{
if (_pokemon is null)
return;
ChangeForm(_pokemon, weatherName);
}
///
public override void OnRemove()
{
if (_pokemon is null)
return;
ChangeForm(_pokemon, null);
}
private static void ChangeForm(IPokemon pokemon, StringKey? weather)
{
if (pokemon.Species.Name != "castform")
return;
if (weather == ScriptUtils.ResolveName() &&
pokemon.Species.TryGetForm("sunny", out var sunnyForm) && pokemon.Form != sunnyForm)
{
pokemon.ChangeForm(sunnyForm);
}
else if (weather == ScriptUtils.ResolveName() &&
pokemon.Species.TryGetForm("rainy", out var rainyForm) && pokemon.Form != rainyForm)
{
pokemon.ChangeForm(rainyForm);
}
else if (weather == ScriptUtils.ResolveName() &&
pokemon.Species.TryGetForm("snowy", out var snowyForm) && pokemon.Form != snowyForm)
{
pokemon.ChangeForm(snowyForm);
}
else if (pokemon.Form != pokemon.Species.GetDefaultForm())
{
pokemon.ChangeForm(pokemon.Species.GetDefaultForm());
}
}
}