Adds several convenience features

This commit is contained in:
Deukhoofd 2024-09-30 14:20:45 +02:00
parent 257c04c98b
commit a39c77745d
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
9 changed files with 72 additions and 13 deletions

View File

@ -107,6 +107,11 @@ public interface IPokemon : IScriptSource
/// The stats of the Pokemon including the stat boosts
/// </summary>
StatisticSet<uint> BoostedStats { get; }
/// <summary>
/// The maximum health of the Pokemon.
/// </summary>
uint MaxHealth { get; }
/// <summary>
/// The individual values of the Pokemon.
@ -268,6 +273,11 @@ public interface IPokemon : IScriptSource
/// heal if the Pokemon has 0 health. If the amount healed is 0, this will return false.
/// </summary>
bool Heal(uint heal, bool allowRevive);
/// <summary>
/// Restores all PP of the Pokemon.
/// </summary>
void RestoreAllPP();
/// <summary>
/// Learn a move by name.
@ -489,6 +499,9 @@ public class PokemonImpl : ScriptSource, IPokemon
/// <inheritdoc />
public StatisticSet<uint> BoostedStats { get; } = new();
/// <inheritdoc />
public uint MaxHealth => BoostedStats.Hp;
/// <inheritdoc />
public IndividualValueStatisticSet IndividualValues { get; } = new();
@ -780,6 +793,15 @@ public class PokemonImpl : ScriptSource, IPokemon
return true;
}
/// <inheritdoc />
public void RestoreAllPP()
{
foreach (var move in Moves)
{
move?.RestoreAllUses();
}
}
/// <inheritdoc />
/// <remarks>
/// If the index is 255, it will try to find the first empty move slot.

View File

@ -40,7 +40,7 @@ public class PokemonParty : IPokemonParty
/// <summary>
/// Sets the Pokemon at an index to a Pokemon, returning the old Pokemon.
/// </summary>
public IPokemon? SwapInto(IPokemon pokemon, int index)
public IPokemon? SwapInto(IPokemon? pokemon, int index)
{
var old = _pokemon[index];
_pokemon[index] = pokemon;
@ -68,4 +68,21 @@ public class PokemonParty : IPokemonParty
/// <inheritdoc />
public IPokemon? this[int index] => _pokemon[index];
public void Pack()
{
// Pack the party so that all Pokémon are at the front.
for (var i = 0; i < _pokemon.Length; i++)
{
if (_pokemon[i] != null)
continue;
for (var j = i + 1; j < _pokemon.Length; j++)
{
if (_pokemon[j] == null)
continue;
Swap(i, j);
break;
}
}
}
}

View File

@ -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.

View File

@ -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();
}

View File

@ -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.

View File

@ -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.

View File

@ -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.

View File

@ -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.

View File

@ -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;
}
}