Initial work on implementing Pokemon
This commit is contained in:
@@ -20,7 +20,7 @@ public abstract class Script
|
||||
/// The name of a script is its unique identifier. This should generally be set on load, and be
|
||||
/// the same as the key that was used to load it.
|
||||
/// </summary>
|
||||
public abstract string Name { get; }
|
||||
public abstract StringKey Name { get; }
|
||||
|
||||
public bool MarkForDeletion() => _markedForDeletion = true;
|
||||
public bool IsMarkedForDeletion() => _markedForDeletion;
|
||||
|
||||
@@ -9,6 +9,17 @@ namespace PkmnLib.Dynamic.ScriptHandling;
|
||||
/// </summary>
|
||||
public class ScriptContainer : IEnumerable<ScriptContainer>
|
||||
{
|
||||
/// <inheritdoc cref="ScriptContainer"/>
|
||||
public ScriptContainer()
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ScriptContainer"/>
|
||||
public ScriptContainer(Script script)
|
||||
{
|
||||
Script = script;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether this container is empty.
|
||||
/// </summary>
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.Collections;
|
||||
using PkmnLib.Static.Utils;
|
||||
|
||||
namespace PkmnLib.Dynamic.ScriptHandling;
|
||||
@@ -19,7 +20,7 @@ public interface IScriptSet : IEnumerable<ScriptContainer>
|
||||
/// Adds a script with a name to the set. If the script with that name already exists in this
|
||||
/// set, this makes that script stack instead. The return value here is that script.
|
||||
/// </summary>
|
||||
ScriptContainer? Add(StringKey scriptKey);
|
||||
ScriptContainer? StackOrAdd(StringKey scriptKey, Func<Script> instantiation);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a script from the set using its unique name.
|
||||
@@ -39,7 +40,7 @@ public interface IScriptSet : IEnumerable<ScriptContainer>
|
||||
/// <summary>
|
||||
/// Checks if the set has a script with the given name.
|
||||
/// </summary>
|
||||
void Contains(StringKey scriptKey);
|
||||
bool Contains(StringKey scriptKey);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a script from the set at a specific index.
|
||||
@@ -55,4 +56,80 @@ public interface IScriptSet : IEnumerable<ScriptContainer>
|
||||
/// Gets the names of all scripts in the set.
|
||||
/// </summary>
|
||||
IEnumerable<StringKey> GetScriptNames();
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IScriptSet"/>
|
||||
public class ScriptSet : IScriptSet
|
||||
{
|
||||
private readonly List<ScriptContainer> _scripts = [];
|
||||
|
||||
/// <inheritdoc />
|
||||
public IEnumerator<ScriptContainer> GetEnumerator() => _scripts.GetEnumerator();
|
||||
|
||||
/// <inheritdoc />
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
|
||||
/// <inheritdoc />
|
||||
public ScriptContainer Add(Script script)
|
||||
{
|
||||
var existing = _scripts.FirstOrDefault(s => s.Script?.Name == script.Name);
|
||||
if (existing != null)
|
||||
{
|
||||
existing.Script!.Stack();
|
||||
return existing;
|
||||
}
|
||||
|
||||
var container = new ScriptContainer(script);
|
||||
_scripts.Add(container);
|
||||
return container;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public ScriptContainer? StackOrAdd(StringKey scriptKey, Func<Script?> instantiation)
|
||||
{
|
||||
var existing = _scripts.FirstOrDefault(s => s.Script?.Name == scriptKey);
|
||||
if (existing != null)
|
||||
{
|
||||
existing.Script!.Stack();
|
||||
return existing;
|
||||
}
|
||||
|
||||
var script = instantiation();
|
||||
if (script is null)
|
||||
return null;
|
||||
var container = new ScriptContainer(script);
|
||||
_scripts.Add(container);
|
||||
return container;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public ScriptContainer? Get(StringKey scriptKey) => _scripts.FirstOrDefault(s => s.Script?.Name == scriptKey);
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Remove(StringKey scriptKey)
|
||||
{
|
||||
var script = _scripts.FirstOrDefault(s => s.Script?.Name == scriptKey);
|
||||
if (script is null)
|
||||
return;
|
||||
_scripts.Remove(script);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Clear() => _scripts.Clear();
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool Contains(StringKey scriptKey) => _scripts.Any(s => s.Script?.Name == scriptKey);
|
||||
|
||||
/// <inheritdoc />
|
||||
public ScriptContainer At(int index) => _scripts[index];
|
||||
|
||||
/// <inheritdoc />
|
||||
public int Count => _scripts.Count;
|
||||
|
||||
/// <inheritdoc />
|
||||
public IEnumerable<StringKey> GetScriptNames() =>
|
||||
_scripts
|
||||
.Select(x => x.Script)
|
||||
.WhereNotNull()
|
||||
.Select(s => s.Name);
|
||||
}
|
||||
Reference in New Issue
Block a user