Add IEquatable interface for items

This commit is contained in:
Kurt
2021-02-05 09:15:25 -08:00
parent 7b832e06cc
commit 5d84d92240

View File

@@ -5,7 +5,7 @@
namespace NHSE.Core
{
[StructLayout(LayoutKind.Explicit, Size = SIZE, Pack = 1)]
public class Item : ICopyableItem<Item>
public class Item : ICopyableItem<Item>, IEquatable<Item>
{
public static readonly Item NO_ITEM = new() {ItemId = NONE};
public const ushort NONE = 0xFFFE;
@@ -197,12 +197,7 @@ public void SetAsExtension(Item tile, byte x, byte y)
public Item(ulong raw) => RawValue = raw;
public Item(ushort itemId = NONE) => ItemId = itemId;
public void Delete()
{
ItemId = NONE;
SystemParam = AdditionalParam = 0;
FreeParam = 0;
}
public void Delete() => RawValue = NONE; // clears & sets the two lowest byte as ItemID
public virtual int Size => SIZE;
@@ -227,5 +222,24 @@ public ushort GetWrappedItemName()
_ => ItemId,
};
}
public bool Equals(Item? other)
{
if (other is null) return false;
if (ReferenceEquals(this, other)) return true;
return RawValue == other.RawValue;
}
public override bool Equals(object? obj)
{
if (obj is null) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals((Item) obj);
}
public override int GetHashCode() => RawValue.GetHashCode();
public static bool operator ==(Item? left, Item? right) => Equals(left, right);
public static bool operator !=(Item? left, Item? right) => !Equals(left, right);
}
}