59 lines
1.7 KiB
C#
59 lines
1.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using PkmnLib.Static.Utils;
|
|
|
|
namespace PkmnLib.Plugin.Gen7.Scripts.Status;
|
|
|
|
[Script(ScriptCategory.Status, "sleep")]
|
|
public class Sleep : Script
|
|
{
|
|
public int Turns { get; set; }
|
|
private IPokemon? _owner;
|
|
|
|
/// <inheritdoc />
|
|
public override void OnAddedToParent(IScriptSource source)
|
|
{
|
|
// Rare case where the script is added again. Can happen for example when baton pass is used.
|
|
if (Turns != 0)
|
|
return;
|
|
if (source is not IPokemon pokemon)
|
|
throw new InvalidOperationException("Sleep script can only be added to a Pokemon.");
|
|
_owner = pokemon;
|
|
var battleData = pokemon.BattleData;
|
|
if (battleData != null)
|
|
{
|
|
// 1-3 turns of sleep
|
|
Turns = battleData.Battle.Random.GetInt(1, 4);
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override void PreventMove(IExecutingMove move, ref bool prevent)
|
|
{
|
|
Turns--;
|
|
if (Turns <= 0)
|
|
{
|
|
RemoveSelf();
|
|
move.User.BattleData?.Battle.EventHook.Invoke(new DialogEvent("pokemon_woke_up",
|
|
new Dictionary<string, object>
|
|
{
|
|
{ "pokemon", move.User },
|
|
}));
|
|
return;
|
|
}
|
|
|
|
if (move.UseMove.HasFlag("usable_while_asleep"))
|
|
return;
|
|
|
|
var bypass = false;
|
|
var pars = new Dictionary<StringKey, object?>
|
|
{
|
|
{ "bypass_sleep", bypass },
|
|
};
|
|
move.RunScriptHook(x => x.CustomTrigger(CustomTriggers.BypassSleep, pars));
|
|
bypass = pars.GetValueOrDefault("bypass_sleep", false) as bool? ?? false;
|
|
if (bypass)
|
|
return;
|
|
prevent = true;
|
|
}
|
|
} |