PkmnLibSharp/PkmnLibSharp/Library/Species/Species.cs

126 lines
4.0 KiB
C#

using System;
using System.Collections.Generic;
using Creaturelib.Generated;
using Pkmnlib.Generated;
using PkmnLibSharp.Utilities;
using EvolutionData = PkmnLibSharp.Library.Evolution.EvolutionData;
using Random = PkmnLibSharp.Utilities.Random;
namespace PkmnLibSharp.Library
{
public class Species : PointerWrapper
{
internal Species(IntPtr ptr) : base(ptr)
{
}
public Species(ushort id, string name, Forme defaultForme, float genderRatio, string growthRate,
byte captureRate, byte baseHappiness)
{
var ptr = IntPtr.Zero;
PokemonSpecies.Construct(ref ptr, id, name.ToPtr(), defaultForme.Ptr, genderRatio,
growthRate.ToPtr(), captureRate, baseHappiness).Assert();
_formes.Add("default", defaultForme);
Initialize(ptr);
}
public ushort Id => CreatureSpecies.GetId(Ptr);
public float GenderRate => CreatureSpecies.GetGenderRate(Ptr);
public byte CaptureRate => CreatureSpecies.GetCaptureRate(Ptr);
public string Name => _name ??= CreatureSpecies.GetName(Ptr).PtrString()!;
public string GrowthRate => _growthRate ??= CreatureSpecies.GetGrowthRate(Ptr).PtrString()!;
public ReadOnlyNativePtrArray<EvolutionData> Evolutions
{
get
{
if (_evolutions != null) return _evolutions;
var ptr = IntPtr.Zero;
PokemonSpecies.GetEvolutions(Ptr, ref ptr);
var length = PokemonSpecies.GetEvolutionCount(Ptr);
_evolutions = new ReadOnlyNativePtrArray<EvolutionData>(ptr, (int) length);
return _evolutions;
}
}
public bool HasForme(string s)
{
return CreatureSpecies.HasVariant(Ptr, s.ToPtr()) == MarshalHelper.True;
}
public bool TryGetForme(string s, out Forme? forme)
{
if (_formes.TryGetValue(s, out forme))
return true;
var ptr = IntPtr.Zero;
if (CreatureSpecies.TryGetVariant(Ptr, s.ToPtr(), ref ptr) == MarshalHelper.True)
{
if (TryResolvePointer(ptr, out forme))
{
_formes.Add(s, forme!);
return true;
}
forme = new Forme(ptr);
_formes.Add(s, forme);
return true;
}
return false;
}
public Forme GetForme(string s)
{
if (_formes.TryGetValue(s, out var forme))
return forme;
var ptr = IntPtr.Zero;
CreatureSpecies.GetVariant(ref ptr, Ptr, s.ToPtr()).Assert();
if (TryResolvePointer(ptr, out Forme? f))
{
_formes.Add(s, f!);
return f!;
}
forme = new Forme(ptr);
_formes.Add(s, forme);
return forme;
}
public void SetForme(string s, Forme forme)
{
_formes.Add(s, forme);
CreatureSpecies.SetVariant(Ptr, s.ToPtr(), forme.Ptr).Assert();
}
public Gender GetRandomGender(Random random)
{
return (Gender) CreatureSpecies.GetRandomGender(Ptr, random.Ptr);
}
public void AddEvolution(EvolutionData evolutionData)
{
Pkmnlib.Generated.PokemonSpecies.AddEvolution(Ptr, evolutionData.Ptr);
}
private string? _name;
private string? _growthRate;
private readonly Dictionary<string, Forme> _formes =
new Dictionary<string, Forme>(StringComparer.InvariantCultureIgnoreCase);
private ReadOnlyNativePtrArray<EvolutionData>? _evolutions;
protected internal override void MarkAsDeleted()
{
base.MarkAsDeleted();
foreach (var forme in _formes)
{
forme.Value.MarkAsDeleted();
}
}
protected override void DeletePtr()
{
PokemonSpecies.Destruct(Ptr);
}
}
}