Adds several convenience features
This commit is contained in:
@@ -7,7 +7,7 @@ namespace PkmnLib.Static.Libraries;
|
||||
/// <summary>
|
||||
/// The library for all abilities in the game.
|
||||
/// </summary>
|
||||
public interface IReadOnlyAbilityLibrary
|
||||
public interface IReadOnlyAbilityLibrary : IEnumerable<IAbility>
|
||||
{
|
||||
/// <summary>
|
||||
/// Tries to get an ability from the library. Returns false if the ability is not found.
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.Collections;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using PkmnLib.Static.Utils;
|
||||
|
||||
@@ -6,10 +7,13 @@ namespace PkmnLib.Static.Libraries;
|
||||
/// <summary>
|
||||
/// A basic library for data types. Stores data both by name and by index.
|
||||
/// </summary>
|
||||
public abstract class DataLibrary<T>
|
||||
public abstract class DataLibrary<T> : IEnumerable<T>
|
||||
where T : INamedValue
|
||||
{
|
||||
private readonly Dictionary<StringKey, T> _data = new();
|
||||
/// <summary>
|
||||
/// The underlying data storage.
|
||||
/// </summary>
|
||||
protected readonly Dictionary<StringKey, T> Data = new();
|
||||
private readonly List<T> _values = [];
|
||||
|
||||
/// <summary>
|
||||
@@ -17,7 +21,7 @@ public abstract class DataLibrary<T>
|
||||
/// </summary>
|
||||
public void Add(T value)
|
||||
{
|
||||
_data.Add(value.Name, value);
|
||||
Data.Add(value.Name, value);
|
||||
_values.Add(value);
|
||||
}
|
||||
|
||||
@@ -26,14 +30,14 @@ public abstract class DataLibrary<T>
|
||||
/// </summary>
|
||||
public void Remove(T value)
|
||||
{
|
||||
_data.Remove(value.Name);
|
||||
Data.Remove(value.Name);
|
||||
_values.Remove(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Try to get a value from the library. Returns false if the value is not found.
|
||||
/// </summary>
|
||||
public bool TryGet(StringKey key, [MaybeNullWhen(false)] out T value) => _data.TryGetValue(key, out value);
|
||||
public bool TryGet(StringKey key, [MaybeNullWhen(false)] out T value) => Data.TryGetValue(key, out value);
|
||||
|
||||
/// <summary>
|
||||
/// Get a random value from the library.
|
||||
@@ -49,4 +53,10 @@ public abstract class DataLibrary<T>
|
||||
/// Whether the library is empty.
|
||||
/// </summary>
|
||||
public bool IsEmpty => _values.Count == 0;
|
||||
|
||||
/// <inheritdoc />
|
||||
public IEnumerator<T> GetEnumerator() => _values.GetEnumerator();
|
||||
|
||||
/// <inheritdoc />
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
}
|
||||
@@ -6,7 +6,7 @@ namespace PkmnLib.Static.Libraries;
|
||||
/// <summary>
|
||||
/// The library for all growth rates in the game.
|
||||
/// </summary>
|
||||
public interface IReadOnlyGrowthRateLibrary
|
||||
public interface IReadOnlyGrowthRateLibrary : IEnumerable<IGrowthRate>
|
||||
{
|
||||
/// <summary>
|
||||
/// Tries to get a growth rate from the library. Returns false if the growth rate is not found.
|
||||
|
||||
@@ -6,7 +6,7 @@ namespace PkmnLib.Static.Libraries;
|
||||
/// <summary>
|
||||
/// The library for all items in the game.
|
||||
/// </summary>
|
||||
public interface IReadOnlyItemLibrary
|
||||
public interface IReadOnlyItemLibrary : IEnumerable<IItem>
|
||||
{
|
||||
/// <summary>
|
||||
/// Tries to get an item from the library. Returns false if the item is not found.
|
||||
|
||||
@@ -7,7 +7,7 @@ namespace PkmnLib.Static.Libraries;
|
||||
/// <summary>
|
||||
/// The library for all moves in the game.
|
||||
/// </summary>
|
||||
public interface IReadOnlyMoveLibrary
|
||||
public interface IReadOnlyMoveLibrary : IEnumerable<IMoveData>
|
||||
{
|
||||
/// <summary>
|
||||
/// Tries to get a move from the library. Returns false if the move is not found.
|
||||
|
||||
@@ -6,7 +6,7 @@ namespace PkmnLib.Static.Libraries;
|
||||
/// <summary>
|
||||
/// The library for all natures in the game.
|
||||
/// </summary>
|
||||
public interface IReadOnlyNatureLibrary
|
||||
public interface IReadOnlyNatureLibrary : IEnumerable<INature>
|
||||
{
|
||||
/// <summary>
|
||||
/// Tries to get a nature from the library. Returns false if the nature is not found.
|
||||
|
||||
@@ -7,12 +7,13 @@ namespace PkmnLib.Static.Libraries;
|
||||
/// <summary>
|
||||
/// The library for all species in the game.
|
||||
/// </summary>
|
||||
public interface IReadOnlySpeciesLibrary
|
||||
public interface IReadOnlySpeciesLibrary : IEnumerable<ISpecies>
|
||||
{
|
||||
/// <summary>
|
||||
/// Tries to get a species from the library. Returns false if the species is not found.
|
||||
/// </summary>
|
||||
bool TryGet(StringKey key, [MaybeNullWhen(false)] out ISpecies value);
|
||||
bool TryGetById(int id, [MaybeNullWhen(false)] out ISpecies value);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a random species from the library.
|
||||
@@ -31,4 +32,13 @@ public interface IReadOnlySpeciesLibrary
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IReadOnlySpeciesLibrary"/>
|
||||
public class SpeciesLibrary : DataLibrary<ISpecies>, IReadOnlySpeciesLibrary;
|
||||
public class SpeciesLibrary : DataLibrary<ISpecies>, IReadOnlySpeciesLibrary
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public bool TryGetById(int id, [MaybeNullWhen(false)] out ISpecies value)
|
||||
{
|
||||
return this.FirstOrDefault(s => s.Id == id) is { } species
|
||||
? (value = species) != null
|
||||
: (value = default) != null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user