57 lines
1.8 KiB
C#
57 lines
1.8 KiB
C#
namespace PkmnLib.Plugin.Gen7.Scripts.Status;
|
|
|
|
[Script(ScriptCategory.Status, "sleep")]
|
|
public class Sleep : Script, IScriptPreventMove
|
|
{
|
|
private IPokemon? _pokemon;
|
|
public int Turns { get; set; }
|
|
|
|
/// <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.");
|
|
_pokemon = pokemon;
|
|
var battleData = pokemon.BattleData;
|
|
if (battleData != null)
|
|
{
|
|
// 1-3 turns of sleep
|
|
Turns = battleData.Battle.Random.GetInt(1, 4);
|
|
var args = new CustomTriggers.ModifySleepTurnsArgs(pokemon, Turns);
|
|
source.RunScriptHook(x => x.CustomTrigger(CustomTriggers.ModifySleepTurns, args));
|
|
Turns = Math.Max(1, args.Turns);
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void PreventMove(IExecutingMove move, ref bool prevent)
|
|
{
|
|
Turns--;
|
|
if (Turns <= 0)
|
|
{
|
|
move.User.ClearStatus();
|
|
return;
|
|
}
|
|
|
|
if (move.UseMove.HasFlag("usable_while_asleep"))
|
|
return;
|
|
|
|
var args = new CustomTriggers.BypassSleepArgs(move, false);
|
|
move.RunScriptHook(x => x.CustomTrigger(CustomTriggers.BypassSleep, args));
|
|
if (args.Bypass)
|
|
return;
|
|
prevent = true;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override void OnRemove()
|
|
{
|
|
_pokemon?.BattleData?.Battle.EventHook.Invoke(new DialogEvent("pokemon_woke_up", new Dictionary<string, object>
|
|
{
|
|
{ "pokemon", _pokemon },
|
|
}));
|
|
}
|
|
} |