Geometry: add equality operators & GetHashCode for structs

This commit is contained in:
Colin McMillen 2020-03-24 13:57:15 -04:00
parent a1bc44296a
commit 3be37740f7

View File

@ -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; }
}