using System.Collections; using System.Diagnostics.CodeAnalysis; using PkmnLib.Static.Utils; namespace PkmnLib.Dynamic.ScriptHandling; /// /// A holder class for a script. This is used so we can cache a list of these, and iterate over them, even when /// the underlying script changes. /// public interface IReadOnlyScriptContainer : IEnumerable, IDeepCloneable { /// /// Whether this container is empty. /// public bool IsEmpty { get; } /// /// The script in this container. /// public Script? Script { get; } } /// public class ScriptContainer : IReadOnlyScriptContainer { /// public ScriptContainer() { } /// public ScriptContainer(Script script) { Script = script; } /// [MemberNotNullWhen(false, nameof(ScriptHandling.Script))] public bool IsEmpty => Script is null; /// public Script? Script { get; private set; } /// public IEnumerator GetEnumerator() { yield return this; } IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); /// /// Assigns a new script to this container. If there was a script already, it is removed. /// /// public void Set(Script script) { Script?.OnRemove(); Script = script; } /// /// Removes the script from this container. /// public Script? Clear() { Script?.OnRemove(); var script = Script; Script = null; return script; } /// /// Removes the script from this container, but does not call . /// Be very careful with this, as it can lead to unexpected behavior. An example of a valid use is Baton-Pass, /// where scripts are being removed to be added to another Pokemon, so we want them to remain active. /// public void ClearWithoutRemoving() { Script = null; } }