From 3be37740f7dbf3c58f1c1ec0c811ea200cd996ce Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Tue, 24 Mar 2020 13:57:15 -0400 Subject: [PATCH] Geometry: add equality operators & GetHashCode for structs --- Shared/Geometry.cs | 49 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/Shared/Geometry.cs b/Shared/Geometry.cs index 98ff0fb..4abd3f4 100644 --- a/Shared/Geometry.cs +++ b/Shared/Geometry.cs @@ -66,6 +66,23 @@ namespace SemiColinGames { Normal = normal; Time = time; } + + public static bool operator ==(Hit a, Hit b) { + return a.Position == b.Position && a.Delta == b.Delta && a.Normal == b.Normal + && a.Time == b.Time && a.Collider == b.Collider; + } + + public static bool operator !=(Hit a, Hit b) { + return !(a == b); + } + + public override bool Equals(object o) { + return o is Hit h && this == h; + } + + public override int GetHashCode() { + return (Collider, Position, Delta, Normal, Time).GetHashCode(); + } } public readonly struct Sweep { @@ -78,6 +95,22 @@ namespace SemiColinGames { Position = position; Time = time; } + + public static bool operator ==(Sweep a, Sweep b) { + return a.Hit == b.Hit && a.Position == b.Position && a.Time == b.Time; + } + + public static bool operator !=(Sweep a, Sweep b) { + return !(a == b); + } + + public override bool Equals(object o) { + return o is Sweep s && this == s; + } + + public override int GetHashCode() { + return (Hit, Position, Time).GetHashCode(); + } } public readonly struct AABB { @@ -94,6 +127,22 @@ namespace SemiColinGames { Tile = tile; } + public static bool operator ==(AABB a, AABB b) { + return a.Position == b.Position && a.HalfSize == b.HalfSize && a.Tile == b.Tile; + } + + public static bool operator !=(AABB a, AABB b) { + return !(a == b); + } + + public override bool Equals(object o) { + return o is AABB a && this == a; + } + + public override int GetHashCode() { + return (Position, HalfSize, Tile).GetHashCode(); + } + public float Top { get { return Position.Y - HalfSize.Y; } }