2024-07-20 14:12:39 +00:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
using PkmnLib.Static.Species;
|
|
|
|
using PkmnLib.Static.Utils;
|
|
|
|
|
|
|
|
namespace PkmnLib.Static.Libraries;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The library for all species in the game.
|
|
|
|
/// </summary>
|
2024-09-30 12:20:45 +00:00
|
|
|
public interface IReadOnlySpeciesLibrary : IEnumerable<ISpecies>
|
2024-07-20 14:12:39 +00:00
|
|
|
{
|
|
|
|
/// <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);
|
2024-09-30 12:59:39 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Tried to get a species from the library by its national dex number. Returns false if the species is not found.
|
|
|
|
/// </summary>
|
2024-09-30 12:20:45 +00:00
|
|
|
bool TryGetById(int id, [MaybeNullWhen(false)] out ISpecies value);
|
2024-07-20 14:12:39 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a random species from the library.
|
|
|
|
/// </summary>
|
|
|
|
ISpecies GetRandom(IRandom random);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The amount of species in the library.
|
|
|
|
/// </summary>
|
|
|
|
int Count { get; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Whether the library is empty.
|
|
|
|
/// </summary>
|
|
|
|
bool IsEmpty { get; }
|
2024-09-30 12:59:39 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Finds the Pokémon that evolves into the given species. If the species does not evolve, returns null.
|
|
|
|
/// If multiple species evolve into the given species, returns the first one found.
|
|
|
|
/// </summary>
|
|
|
|
ISpecies? FindPreEvolution(ISpecies species);
|
2024-07-20 14:12:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc cref="IReadOnlySpeciesLibrary"/>
|
2024-09-30 12:20:45 +00:00
|
|
|
public class SpeciesLibrary : DataLibrary<ISpecies>, IReadOnlySpeciesLibrary
|
|
|
|
{
|
2024-09-30 12:59:39 +00:00
|
|
|
private Dictionary<ISpecies, ISpecies> _preEvolutionCache = new();
|
|
|
|
|
2024-09-30 12:20:45 +00:00
|
|
|
/// <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;
|
|
|
|
}
|
2024-09-30 12:59:39 +00:00
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public ISpecies? FindPreEvolution(ISpecies species)
|
|
|
|
{
|
|
|
|
if (_preEvolutionCache.TryGetValue(species, out var preEvolution))
|
|
|
|
return preEvolution;
|
|
|
|
foreach (var s in this)
|
|
|
|
{
|
|
|
|
if (s.EvolutionData.All(e => e.ToSpecies != species.Name))
|
|
|
|
continue;
|
|
|
|
_preEvolutionCache[species] = s;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2024-09-30 12:20:45 +00:00
|
|
|
}
|