Serialization of Pokemon, general fixes

This commit is contained in:
2024-09-03 09:31:32 +02:00
parent 2a0aaed4c3
commit 3214a6f29a
11 changed files with 387 additions and 51 deletions

View File

@@ -1,3 +1,4 @@
using System.Collections;
using System.Diagnostics.CodeAnalysis;
namespace PkmnLib.Static;
@@ -49,6 +50,16 @@ public record ImmutableStatisticSet<T>
SpecialDefense = specialDefense;
Speed = speed;
}
public ImmutableStatisticSet(ImmutableStatisticSet<T> set)
{
Hp = set.Hp;
Attack = set.Attack;
Defense = set.Defense;
SpecialAttack = set.SpecialAttack;
SpecialDefense = set.SpecialDefense;
Speed = set.Speed;
}
/// <summary>
/// Gets a statistic from the set.
@@ -72,7 +83,7 @@ public record ImmutableStatisticSet<T>
/// A set of statistics that can be changed.
/// </summary>
/// <typeparam name="T"></typeparam>
public record StatisticSet<T> : ImmutableStatisticSet<T>
public record StatisticSet<T> : ImmutableStatisticSet<T>, IEnumerable<T>
where T : struct
{
/// <inheritdoc cref="StatisticSet{T}"/>
@@ -85,6 +96,10 @@ public record StatisticSet<T> : ImmutableStatisticSet<T>
defense, specialAttack, specialDefense, speed)
{
}
public StatisticSet(StatisticSet<T> set) : base(set)
{
}
/// <summary>
/// Helper function to add two numerics together.
@@ -189,6 +204,23 @@ public record StatisticSet<T> : ImmutableStatisticSet<T>
return true;
}
/// <inheritdoc />
public IEnumerator<T> GetEnumerator()
{
yield return Hp;
yield return Attack;
yield return Defense;
yield return SpecialAttack;
yield return SpecialDefense;
yield return Speed;
}
/// <inheritdoc />
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
/// <summary>
@@ -211,6 +243,10 @@ public abstract record ClampedStatisticSet<T> : StatisticSet<T>
SpecialDefense = Clamp(SpecialDefense, Min, Max);
Speed = Clamp(Speed, Min, Max);
}
protected ClampedStatisticSet(ClampedStatisticSet<T> set) : base(set)
{
}
private static T Clamp(T value, T min, T max)
{
@@ -304,6 +340,10 @@ public record IndividualValueStatisticSet : ClampedStatisticSet<byte>
byte speed) : base(hp, attack, defense, specialAttack, specialDefense, speed)
{
}
public IndividualValueStatisticSet(IndividualValueStatisticSet ivs) : base(ivs)
{
}
}
/// <summary>
@@ -327,4 +367,8 @@ public record EffortValueStatisticSet : ClampedStatisticSet<byte>
byte speed) : base(hp, attack, defense, specialAttack, specialDefense, speed)
{
}
public EffortValueStatisticSet(EffortValueStatisticSet evs) : base(evs)
{
}
}

View File

@@ -26,7 +26,10 @@ public readonly record struct StringKey
/// <summary>
/// Converts a <see cref="string"/> to a <see cref="StringKey"/>.
/// </summary>
public static implicit operator StringKey(string key) => new(key);
public static implicit operator StringKey(string key)
{
return string.IsNullOrWhiteSpace(key) ? default : new StringKey(key);
}
/// <inheritdoc cref="string.ToString()"/>
public override string ToString() => _key.ToLowerInvariant();