Added additional missing equality operators for HexCell.

WorldChunkRefactoring
Martin Felis 2023-08-12 15:20:23 +02:00
parent 3b73480845
commit 0ee11e358c
2 changed files with 30 additions and 1 deletions

View File

@ -1,11 +1,25 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices;
using Godot; using Godot;
using Array = Godot.Collections.Array; using Array = Godot.Collections.Array;
public class HexCell : Resource, IEquatable<HexCell> public class HexCell : IEquatable<HexCell>
{ {
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 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_N = new Vector3(0, 1, -1);
public static readonly Vector3 DIR_NE = new Vector3(1, 0, -1); public static readonly Vector3 DIR_NE = new Vector3(1, 0, -1);
@ -36,6 +50,16 @@ public class HexCell : Resource, IEquatable<HexCell>
return CubeCoords == other.CubeCoords; 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) public HexCell(Vector3 cubeCoords)
{ {
CubeCoords = cubeCoords; CubeCoords = cubeCoords;

View File

@ -27,9 +27,14 @@ public class HexCellTests : TestClass
cellA.AxialCoords = new Vector2(2, 3); cellA.AxialCoords = new Vector2(2, 3);
cellB.AxialCoords = new Vector2(2, 3); cellB.AxialCoords = new Vector2(2, 3);
Assert.Equal(cellA, cellB); Assert.Equal(cellA, cellB);
bool result = cellA == cellB;
Assert.True(cellA == cellB);
Assert.False(cellA != cellB);
cellB.AxialCoords = new Vector2(3, 2); cellB.AxialCoords = new Vector2(3, 2);
Assert.NotEqual(cellA, cellB); Assert.NotEqual(cellA, cellB);
Assert.False(cellA == cellB);
Assert.True(cellA != cellB);
} }
[Test] [Test]