From 0ee11e358cf613018b7b750a9e6891874a4aaae4 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Sat, 12 Aug 2023 15:20:23 +0200 Subject: [PATCH] Added additional missing equality operators for HexCell. --- HexCell.cs | 26 +++++++++++++++++++++++++- tests/HexCellTests.cs | 5 +++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/HexCell.cs b/HexCell.cs index 626a58d..c63c17f 100644 --- a/HexCell.cs +++ b/HexCell.cs @@ -1,11 +1,25 @@ using System; using System.Diagnostics; using System.Linq; +using System.Runtime.CompilerServices; using Godot; using Array = Godot.Collections.Array; -public class HexCell : Resource, IEquatable +public class HexCell : IEquatable { + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + if (obj.GetType() != this.GetType()) return false; + return Equals((HexCell)obj); + } + + public override int GetHashCode() + { + return _cubeCoords.GetHashCode(); + } + public static readonly Vector2 size = new Vector2(1, Mathf.Sqrt(3) / 2); public static readonly Vector3 DIR_N = new Vector3(0, 1, -1); public static readonly Vector3 DIR_NE = new Vector3(1, 0, -1); @@ -35,6 +49,16 @@ public class HexCell : Resource, IEquatable return CubeCoords == other.CubeCoords; } + + public static bool operator ==(HexCell cellA, HexCell cellB) + { + return Equals(cellA, cellB); + } + + public static bool operator !=(HexCell cellA, HexCell cellB) + { + return !(cellA == cellB); + } public HexCell(Vector3 cubeCoords) { diff --git a/tests/HexCellTests.cs b/tests/HexCellTests.cs index 65a5bd5..9773ce9 100644 --- a/tests/HexCellTests.cs +++ b/tests/HexCellTests.cs @@ -27,9 +27,14 @@ public class HexCellTests : TestClass cellA.AxialCoords = new Vector2(2, 3); cellB.AxialCoords = new Vector2(2, 3); Assert.Equal(cellA, cellB); + bool result = cellA == cellB; + Assert.True(cellA == cellB); + Assert.False(cellA != cellB); cellB.AxialCoords = new Vector2(3, 2); Assert.NotEqual(cellA, cellB); + Assert.False(cellA == cellB); + Assert.True(cellA != cellB); } [Test]