Deukhoofd 1feb27e826
All checks were successful
Build / Build (push) Successful in 50s
More work on refactor to interfaces
2025-06-29 12:03:51 +02:00

39 lines
1.4 KiB
C#

namespace PkmnLib.Plugin.Gen7.Scripts.Abilities;
/// <summary>
/// Bad Dreams is an ability that causes sleeping opposing Pokémon to lose 1/8 of their maximum HP at the end of each turn.
/// This effect only applies to sleeping Pokémon on the opposing side of the field.
///
/// <see href="https://bulbapedia.bulbagarden.net/wiki/Bad_Dreams_(Ability)">Bulbapedia - Bad Dreams</see>
/// </summary>
[Script(ScriptCategory.Ability, "bad_dreams")]
public class BadDreams : Script, IScriptOnEndTurn
{
private IPokemon? _owner;
/// <inheritdoc />
public override void OnAddedToParent(IScriptSource source)
{
if (source is not IPokemon pokemon)
throw new InvalidOperationException("Bad Dreams ability can only be added to a Pokemon.");
_owner = pokemon;
}
/// <inheritdoc />
public void OnEndTurn(IScriptSource owner, IBattle battle)
{
if (_owner is null)
return;
var opponents = battle.Sides.Where(x => x != _owner?.BattleData?.BattleSide).SelectMany(x => x.Pokemon)
.WhereNotNull();
foreach (var opponent in opponents)
{
if (!opponent.HasStatus(ScriptUtils.ResolveName<Status.Sleep>()))
continue;
EventBatchId batchId = new();
battle.EventHook.Invoke(new AbilityTriggerEvent(_owner));
opponent.Damage(opponent.MaxHealth / 8, DamageSource.Misc, batchId);
}
}
}