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

107 lines
2.9 KiB
C#
Raw Normal View History

2024-07-27 14:26:45 +00:00
using System.Collections;
using PkmnLib.Static.Utils;
2024-07-27 14:26:45 +00:00
namespace PkmnLib.Dynamic.Models;
2024-07-28 10:52:17 +00:00
/// <summary>
/// A collection of Pokemon.
/// </summary>
public interface IPokemonParty : IReadOnlyList<IPokemon?>, IDeepCloneable
2024-07-27 14:26:45 +00:00
{
2024-09-30 17:23:56 +00:00
event EventHandler<(IPokemon?, int index)>? OnSwapInto;
event EventHandler<(int index1, int index2)>? OnSwap;
2025-03-02 16:19:57 +00:00
2024-07-27 14:26:45 +00:00
/// <summary>
/// Sets the Pokemon at an index to a Pokemon, returning the old Pokemon.
/// </summary>
2024-09-30 17:23:56 +00:00
IPokemon? SwapInto(IPokemon? pokemon, int index);
2024-07-27 14:26:45 +00:00
/// <summary>
/// Swaps two Pokemon in the party around.
/// </summary>
void Swap(int index1, int index2);
2024-07-28 10:52:17 +00:00
/// <summary>
/// Whether the party has any Pokemon that could be used in battle.
/// </summary>
/// <remarks>
/// This will return false if all Pokemon are fainted, or eggs, etc.
/// </remarks>
2024-07-27 14:26:45 +00:00
bool HasUsablePokemon();
2025-03-02 16:19:57 +00:00
2024-09-30 17:23:56 +00:00
/// <summary>
/// Packs the party so that all Pokémon are at the front, and the empty slots are at the back.
/// </summary>
void Pack();
2024-07-27 14:26:45 +00:00
}
2024-07-28 10:52:17 +00:00
/// <inheritdoc />
2024-07-27 14:26:45 +00:00
public class PokemonParty : IPokemonParty
{
private readonly IPokemon?[] _pokemon;
2024-07-28 10:52:17 +00:00
/// <inheritdoc cref="PokemonParty" />
2024-07-27 14:26:45 +00:00
public PokemonParty(int size)
{
_pokemon = new IPokemon[size];
}
2024-09-30 17:23:56 +00:00
/// <inheritdoc />
public event EventHandler<(IPokemon?, int index)>? OnSwapInto;
/// <inheritdoc />
public event EventHandler<(int index1, int index2)>? OnSwap;
2024-07-27 14:26:45 +00:00
/// <summary>
/// Sets the Pokemon at an index to a Pokemon, returning the old Pokemon.
/// </summary>
2024-09-30 12:20:45 +00:00
public IPokemon? SwapInto(IPokemon? pokemon, int index)
2024-07-27 14:26:45 +00:00
{
var old = _pokemon[index];
_pokemon[index] = pokemon;
2024-09-30 17:23:56 +00:00
OnSwapInto?.Invoke(this, (pokemon, index));
2024-07-27 14:26:45 +00:00
return old;
}
/// <summary>
/// Swaps two Pokemon in the party around.
/// </summary>
2024-09-30 17:23:56 +00:00
public void Swap(int index1, int index2)
{
2024-07-27 14:26:45 +00:00
(_pokemon[index1], _pokemon[index2]) = (_pokemon[index2], _pokemon[index1]);
2024-09-30 17:23:56 +00:00
OnSwap?.Invoke(this, (index1, index2));
}
2024-07-27 14:26:45 +00:00
2024-07-28 10:52:17 +00:00
/// <inheritdoc />
public bool HasUsablePokemon() => _pokemon.Any(p => p is { IsUsable: true });
2025-03-02 16:19:57 +00:00
2024-07-27 14:26:45 +00:00
/// <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];
2024-09-30 12:20:45 +00:00
2024-09-30 17:23:56 +00:00
/// <inheritdoc />
2024-09-30 12:20:45 +00:00
public void Pack()
{
// Pack the party so that all Pokémon are at the front.
for (var i = 0; i < _pokemon.Length; i++)
{
2025-03-02 16:19:57 +00:00
if (_pokemon[i] != null)
2024-09-30 12:20:45 +00:00
continue;
for (var j = i + 1; j < _pokemon.Length; j++)
{
2025-03-02 16:19:57 +00:00
if (_pokemon[j] == null)
2024-09-30 12:20:45 +00:00
continue;
Swap(i, j);
break;
}
}
}
2024-07-27 14:26:45 +00:00
}