This commit is contained in:
36
PkmnLib.Dynamic/ScriptHandling/ProxyScript.cs
Normal file
36
PkmnLib.Dynamic/ScriptHandling/ProxyScript.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using PkmnLib.Dynamic.Models;
|
||||
using PkmnLib.Static;
|
||||
|
||||
namespace PkmnLib.Dynamic.ScriptHandling;
|
||||
|
||||
public class ProxyScript : Script
|
||||
{
|
||||
public delegate void ChangeOffensiveStatValueEventHandler(IExecutingMove move, IPokemon target, byte hit,
|
||||
uint defensiveStat, ImmutableStatisticSet<uint> targetStats, ref uint value);
|
||||
|
||||
private readonly List<(Script, ChangeOffensiveStatValueEventHandler)> _changeOffensiveStatValueEvents = new();
|
||||
|
||||
public void AddChangeOffensiveStatValueEvent(Script script, ChangeOffensiveStatValueEventHandler handler)
|
||||
{
|
||||
_changeOffensiveStatValueEvents.Add((script, handler));
|
||||
script.OnRemoveEvent += OnRemoveScriptEvent;
|
||||
}
|
||||
|
||||
private void OnRemoveScriptEvent(Script script)
|
||||
{
|
||||
_changeOffensiveStatValueEvents.RemoveAll(x => x.Item1 == script);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void ChangeOffensiveStatValue(IExecutingMove move, IPokemon target, byte hit, uint defensiveStat,
|
||||
ImmutableStatisticSet<uint> targetStats, ref uint value)
|
||||
{
|
||||
foreach (var (script, handler) in _changeOffensiveStatValueEvents)
|
||||
{
|
||||
if (!script.IsSuppressed)
|
||||
{
|
||||
handler(move, target, hit, defensiveStat, targetStats, ref value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -728,7 +728,8 @@ public abstract class Script : IDeepCloneable
|
||||
/// <summary>
|
||||
/// This function allows a script to prevent a Pokemon from being affected by a status condition.
|
||||
/// </summary>
|
||||
public virtual void PreventStatusChange(IPokemon pokemonImpl, StringKey status, ref bool preventStatus)
|
||||
public virtual void PreventStatusChange(IPokemon pokemon, StringKey status, bool selfInflicted,
|
||||
ref bool preventStatus)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -65,10 +65,7 @@ public class ScriptContainer : IReadOnlyScriptContainer
|
||||
/// </summary>
|
||||
public Script? Clear()
|
||||
{
|
||||
if (Script is not null)
|
||||
{
|
||||
Script.OnRemove();
|
||||
}
|
||||
Script?.OnRemove();
|
||||
var script = Script;
|
||||
Script = null;
|
||||
return script;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Collections;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using PkmnLib.Dynamic.ScriptHandling.Registry;
|
||||
using PkmnLib.Static.Utils;
|
||||
|
||||
@@ -31,6 +32,11 @@ public interface IScriptSet : IEnumerable<ScriptContainer>
|
||||
/// </summary>
|
||||
ScriptContainer? Get(StringKey scriptKey);
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get a script from the set using its type.
|
||||
/// </summary>
|
||||
bool TryGet<T>([NotNullWhen(true)] out T? script) where T : Script;
|
||||
|
||||
/// <summary>
|
||||
/// Gets a script from the set using its type.
|
||||
/// </summary>
|
||||
@@ -144,6 +150,20 @@ public class ScriptSet : IScriptSet
|
||||
/// <inheritdoc />
|
||||
public ScriptContainer? Get(StringKey scriptKey) => _scripts.FirstOrDefault(s => s.Script?.Name == scriptKey);
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool TryGet<T>([NotNullWhen(true)] out T? script) where T : Script
|
||||
{
|
||||
var scriptName = ScriptUtils.ResolveName<T>();
|
||||
var container = _scripts.FirstOrDefault(sc => sc.Script?.Name == scriptName);
|
||||
if (container?.Script is not T s)
|
||||
{
|
||||
script = null;
|
||||
return false;
|
||||
}
|
||||
script = s;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public T? Get<T>() where T : Script => Get(ScriptUtils.ResolveName<T>())?.Script as T;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user