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;
|
2020-08-24 17:52:14 +00:00
|
|
|
using System.Linq;
|
2020-07-25 13:23:01 +00:00
|
|
|
using PkmnLibSharp.Utilities;
|
|
|
|
|
|
|
|
namespace PkmnLibSharp.Battling
|
|
|
|
{
|
2020-10-17 16:16:00 +00:00
|
|
|
public class PokemonParty : PointerWrapper, IEnumerable<Pokemon?>
|
2020-07-25 13:23:01 +00:00
|
|
|
{
|
2020-10-31 15:13:10 +00:00
|
|
|
public delegate void OnSwitchDelegate(ulong a, ulong b);
|
|
|
|
public event OnSwitchDelegate? OnSwitch;
|
|
|
|
public delegate void OnSwapIntoDelegate(ulong a);
|
|
|
|
public event OnSwapIntoDelegate? OnSwapInto;
|
|
|
|
|
2020-08-25 15:56:57 +00:00
|
|
|
private Pokemon?[]? _cache;
|
2020-08-24 16:33:04 +00:00
|
|
|
private ReadOnlyNativePtrArray<Pokemon>? _party;
|
2020-07-25 13:23:01 +00:00
|
|
|
|
2020-07-31 12:19:21 +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
|
|
|
{}
|
|
|
|
|
2020-08-24 17:52:14 +00:00
|
|
|
public PokemonParty(Pokemon?[] pokemon) : base(
|
2020-07-31 12:19:21 +00:00
|
|
|
Creaturelib.Generated.CreatureParty.ConstructFromArray(pokemon.ArrayPtr(), (ulong) pokemon.Length))
|
2020-08-24 17:52:14 +00:00
|
|
|
{
|
2020-08-25 15:56:57 +00:00
|
|
|
_cache = pokemon;
|
2020-08-24 17:52:14 +00:00
|
|
|
}
|
2020-08-25 15:56:57 +00:00
|
|
|
|
|
|
|
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;
|
2020-07-31 12:19:21 +00:00
|
|
|
Creaturelib.Generated.CreatureParty.GetAtIndex(ref ptr, Ptr, index).Assert();
|
2020-08-22 14:26:14 +00:00
|
|
|
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)
|
|
|
|
{
|
2020-07-31 12:19:21 +00:00
|
|
|
Creaturelib.Generated.CreatureParty.Switch(Ptr, indexA, indexB);
|
2020-08-25 15:56:57 +00:00
|
|
|
var temp = _cache![indexA];
|
|
|
|
_cache[indexA] = _cache[indexB];
|
|
|
|
_cache[indexB] = temp;
|
2020-10-31 15:13:10 +00:00
|
|
|
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();
|
2020-08-25 15:56:57 +00:00
|
|
|
_cache![indexA] = pokemon;
|
2020-10-31 15:13:10 +00:00
|
|
|
OnSwapInto?.Invoke(indexA);
|
2020-08-08 12:32:27 +00:00
|
|
|
if (TryResolvePointer(ptr, out Pokemon? newPokemon))
|
|
|
|
return newPokemon!;
|
2020-08-22 14:26:14 +00:00
|
|
|
return Constructor.Active.ConstructPokemon(ptr)!;
|
2020-07-25 13:23:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public bool HasAvailablePokemon()
|
|
|
|
{
|
2020-07-31 12:19:21 +00:00
|
|
|
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;
|
2020-07-31 12:19:21 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-31 12:19:21 +00:00
|
|
|
public ulong Length => Creaturelib.Generated.CreatureParty.GetLength(Ptr);
|
2020-08-10 17:13:17 +00:00
|
|
|
|
|
|
|
public void PackParty()
|
|
|
|
{
|
|
|
|
Creaturelib.Generated.CreatureParty.PackParty(Ptr);
|
2020-08-25 15:56:57 +00:00
|
|
|
_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()
|
|
|
|
{
|
2020-07-31 12:19:21 +00:00
|
|
|
Creaturelib.Generated.CreatureParty.Destruct(Ptr);
|
2020-07-25 13:23:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected internal override void MarkAsDeleted()
|
|
|
|
{
|
|
|
|
base.MarkAsDeleted();
|
2021-01-15 15:53:32 +00:00
|
|
|
for (var index = 0; index < Party.Count; index++)
|
2020-07-25 13:23:01 +00:00
|
|
|
{
|
2021-01-15 15:53:32 +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
|
|
|
}
|
2021-01-15 15:53:32 +00:00
|
|
|
|
2020-08-25 15:56:57 +00:00
|
|
|
_cache = null;
|
2020-07-25 13:23:01 +00:00
|
|
|
}
|
2020-08-10 17:13:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2020-07-25 13:23:01 +00:00
|
|
|
}
|
|
|
|
}
|