PkmnLibSharp/PkmnLibSharp/Battling/PokemonParty.cs

124 lines
3.9 KiB
C#
Raw Normal View History

2020-07-25 13:23:01 +00:00
using System;
2020-08-10 17:13:17 +00:00
using System.Collections;
using System.Collections.Generic;
using System.Linq;
2020-07-25 13:23:01 +00:00
using PkmnLibSharp.Utilities;
namespace PkmnLibSharp.Battling
{
public class PokemonParty : PointerWrapper, IReadOnlyList<Pokemon?>
2020-07-25 13:23:01 +00:00
{
public delegate void OnSwitchDelegate(ulong a, ulong b);
public event OnSwitchDelegate? OnSwitch;
public delegate void OnSwapIntoDelegate(ulong a);
public event OnSwapIntoDelegate? OnSwapInto;
private Pokemon?[]? _cache;
2020-08-24 16:33:04 +00:00
private ReadOnlyNativePtrArray<Pokemon>? _party;
2020-07-25 13:23:01 +00:00
internal PokemonParty(IntPtr ptr) : base(ptr){}
public PokemonParty(byte size = 6) : base(Creaturelib.Generated.CreatureParty.ConstructWithSize(size))
2020-07-25 13:23:01 +00:00
{}
public PokemonParty(Pokemon?[] pokemon) : base(
Creaturelib.Generated.CreatureParty.ConstructFromArray(pokemon.ArrayPtr(), (ulong) pokemon.Length))
{
_cache = pokemon;
}
protected internal override void Initialize(IntPtr ptr)
{
base.Initialize(ptr);
_cache = Party.ToArray();
}
2020-10-17 16:16:00 +00:00
public virtual Pokemon? this[int i] => GetAtIndex((ulong) i);
2020-07-25 13:23:01 +00:00
2020-10-17 16:16:00 +00:00
public Pokemon? GetAtIndex(ulong index)
2020-07-25 13:23:01 +00:00
{
var ptr = IntPtr.Zero;
Creaturelib.Generated.CreatureParty.GetAtIndex(ref ptr, Ptr, index).Assert();
return TryResolvePointer(ptr, out Pokemon? pkmn) ? pkmn! : Constructor.Active.ConstructPokemon(ptr)!;
2020-07-25 13:23:01 +00:00
}
public void Switch(ulong indexA, ulong indexB)
{
Creaturelib.Generated.CreatureParty.Switch(Ptr, indexA, indexB);
var temp = _cache![indexA];
_cache[indexA] = _cache[indexB];
_cache[indexB] = temp;
OnSwitch?.Invoke(indexA, indexB);
2020-07-25 13:23:01 +00:00
}
2020-10-17 16:16:00 +00:00
public Pokemon? SwapInto(ulong indexA, Pokemon? pokemon)
2020-07-25 13:23:01 +00:00
{
2020-08-22 13:45:50 +00:00
var ptr = IntPtr.Zero;
2020-10-17 16:16:00 +00:00
var pkmnPtr = IntPtr.Zero;
if (pokemon != null) pkmnPtr = pokemon.Ptr;
Creaturelib.Generated.CreatureParty.SwapInto(ref ptr, Ptr, indexA, pkmnPtr).Assert();
_cache![indexA] = pokemon;
OnSwapInto?.Invoke(indexA);
if (TryResolvePointer(ptr, out Pokemon? newPokemon))
return newPokemon!;
return Constructor.Active.ConstructPokemon(ptr)!;
2020-07-25 13:23:01 +00:00
}
public bool HasAvailablePokemon()
{
return Creaturelib.Generated.CreatureParty.HasAvailableCreatures(Ptr) == 1;
2020-07-25 13:23:01 +00:00
}
2020-08-24 16:33:04 +00:00
public ReadOnlyNativePtrArray<Pokemon> Party
2020-07-25 13:23:01 +00:00
{
get
{
if (_party != null) return _party;
var ptr = Creaturelib.Generated.CreatureParty.GetParty(Ptr);
2020-08-24 16:33:04 +00:00
_party = new ReadOnlyNativePtrArray<Pokemon>(ptr, (int) Length, Constructor.GenericType.Pokemon);
2020-07-25 13:23:01 +00:00
return _party;
}
}
public ulong Length => Creaturelib.Generated.CreatureParty.GetLength(Ptr);
2020-08-10 17:13:17 +00:00
public void PackParty()
{
Creaturelib.Generated.CreatureParty.PackParty(Ptr);
_cache = Party.ToArray();
2020-08-10 17:13:17 +00:00
}
2020-07-25 13:23:01 +00:00
2020-08-10 17:13:17 +00:00
public IEnumerator<Pokemon?> GetEnumerator()
{
return Party.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
2020-07-25 13:23:01 +00:00
protected override void DeletePtr()
{
Creaturelib.Generated.CreatureParty.Destruct(Ptr);
2020-07-25 13:23:01 +00:00
}
protected internal override void MarkAsDeleted()
{
base.MarkAsDeleted();
for (var index = 0; index < Party.Count; index++)
2020-07-25 13:23:01 +00:00
{
var pokemon = Party.GetDontInitialise(index);
2020-08-10 17:13:17 +00:00
pokemon?.MarkAsDeleted();
2020-07-25 13:23:01 +00:00
}
_cache = null;
2020-07-25 13:23:01 +00:00
}
public int Count => (int)Length;
2020-08-10 17:13:17 +00:00
public int IndexOf(Pokemon? pokemon)
{
return Party.IndexOf(pokemon);
}
2020-07-25 13:23:01 +00:00
}
}