Bug fixes, add type name to TypeIdentifier

This commit is contained in:
2025-03-07 12:57:06 +01:00
parent 3571b2130e
commit 2c987e32ee
9 changed files with 29 additions and 34 deletions

View File

@@ -13,11 +13,6 @@ public interface IReadOnlyTypeLibrary
/// </summary>
bool TryGetTypeIdentifier(StringKey key, out TypeIdentifier typeIdentifier);
/// <summary>
/// Gets the type name from the type identifier.
/// </summary>
bool TryGetTypeName(TypeIdentifier t, [NotNullWhen(true)] out StringKey? stringKey);
/// <summary>
/// Gets the effectiveness for a single attacking type against a single defending type.
/// </summary>
@@ -35,25 +30,19 @@ public interface IReadOnlyTypeLibrary
/// <inheritdoc />
public class TypeLibrary : IReadOnlyTypeLibrary
{
private readonly Dictionary<StringKey, TypeIdentifier> _types = new();
private readonly List<TypeIdentifier> _types = new();
private readonly List<List<float>> _effectiveness = new();
/// <inheritdoc />
public bool TryGetTypeIdentifier(StringKey key, out TypeIdentifier type) => _types.TryGetValue(key, out type);
/// <inheritdoc />
public bool TryGetTypeName(TypeIdentifier t, [NotNullWhen(true)] out StringKey? stringKey)
public bool TryGetTypeIdentifier(StringKey key, out TypeIdentifier type)
{
foreach (var (key, value) in _types)
var found = _types.FirstOrDefault(t => t.Name.Equals(key));
if (found.Value is not 0)
{
if (value == t)
{
stringKey = key;
return true;
}
type = found;
return true;
}
stringKey = default;
type = default;
return false;
}
@@ -79,7 +68,8 @@ public class TypeLibrary : IReadOnlyTypeLibrary
throw new ArgumentOutOfRangeException(nameof(attacking));
for (var i = 0; i < _effectiveness.Count; i++)
{
yield return (new TypeIdentifier((byte)(i + 1)), _effectiveness[attacking.Value - 1][i]);
var type = _types[i];
yield return (type, _effectiveness[attacking.Value - 1][i]);
}
}
@@ -88,8 +78,8 @@ public class TypeLibrary : IReadOnlyTypeLibrary
/// </summary>
public TypeIdentifier RegisterType(StringKey name)
{
var id = new TypeIdentifier((byte)(_types.Count + 1));
_types.Add(name, id);
var id = new TypeIdentifier((byte)(_types.Count + 1), name);
_types.Add(id);
_effectiveness.Add(Enumerable.Repeat(1.0f, _effectiveness.Count).ToList());
foreach (var list in _effectiveness)
{

View File

@@ -1,4 +1,5 @@
using PkmnLib.Static.Libraries;
using PkmnLib.Static.Utils;
namespace PkmnLib.Static;
@@ -7,22 +8,23 @@ namespace PkmnLib.Static;
/// </summary>
public readonly record struct TypeIdentifier
{
/// <summary>
/// The name of the type identifier.
/// </summary>
public StringKey Name { get; }
/// <summary>
/// The underlying value of the type identifier.
/// </summary>
public byte Value { get; }
/// <inheritdoc cref="TypeIdentifier"/>
public TypeIdentifier(byte value)
public TypeIdentifier(byte value, StringKey name)
{
Value = value;
Name = name;
}
/// <summary>
/// Converts a byte to a type identifier.
/// </summary>
public static implicit operator TypeIdentifier(byte value) => new(value);
/// <inheritdoc />
public override int GetHashCode() => Value.GetHashCode();
}