PkmnLib.NET/PkmnLib.Dynamic/Models/PokemonParty.cs

59 lines
1.5 KiB
C#
Raw Normal View History

2024-07-27 14:26:45 +00:00
using System.Collections;
namespace PkmnLib.Dynamic.Models;
public interface IPokemonParty : IReadOnlyList<IPokemon?>
{
/// <summary>
/// Sets the Pokemon at an index to a Pokemon, returning the old Pokemon.
/// </summary>
IPokemon? SwapInto(IPokemon pokemon, int index);
/// <summary>
/// Swaps two Pokemon in the party around.
/// </summary>
void Swap(int index1, int index2);
bool HasUsablePokemon();
}
public class PokemonParty : IPokemonParty
{
private readonly IPokemon?[] _pokemon;
public PokemonParty(int size)
{
_pokemon = new IPokemon[size];
}
/// <summary>
/// Sets the Pokemon at an index to a Pokemon, returning the old Pokemon.
/// </summary>
public IPokemon? SwapInto(IPokemon pokemon, int index)
{
var old = _pokemon[index];
_pokemon[index] = pokemon;
return old;
}
/// <summary>
/// Swaps two Pokemon in the party around.
/// </summary>
public void Swap(int index1, int index2) =>
(_pokemon[index1], _pokemon[index2]) = (_pokemon[index2], _pokemon[index1]);
public bool HasUsablePokemon() => _pokemon.Any(p => p != null && p.IsUsable);
/// <inheritdoc />
public IEnumerator<IPokemon?> GetEnumerator() => ((IEnumerable<IPokemon?>)_pokemon).GetEnumerator();
/// <inheritdoc />
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
/// <inheritdoc />
public int Count => _pokemon.Length;
/// <inheritdoc />
public IPokemon? this[int index] => _pokemon[index];
}