diff --git a/ConsoleGameLib/CoreTypes/Point.cs b/ConsoleGameLib/CoreTypes/Point.cs index 7884804..f199e75 100644 --- a/ConsoleGameLib/CoreTypes/Point.cs +++ b/ConsoleGameLib/CoreTypes/Point.cs @@ -15,5 +15,64 @@ public Point(int x, int y) public int X; public int Y; + + public override int GetHashCode() + { + // Uses http://stackoverflow.com/questions/263400/what-is-the-best-algorithm-for-an-overridden-system-object-gethashcode + unchecked // Overflow is fine, just wrap + { + int hash = 37; + hash = hash * 67 + X.GetHashCode(); + hash = hash * 67 + Y.GetHashCode(); + return hash; + } + } + + public override bool Equals(object obj) + { + if (obj == null || !(obj is Point)) + { + return false; + } + Point oth = (Point)obj; + return X == oth.X && Y == oth.Y; + } + + public static bool operator ==(Point left, Point right) + { + // Struct, so no need for null check + return left.X == right.X && left.Y == right.Y; + } + + public static bool operator !=(Point left, Point right) + { + // Struct, so no need for null check + return left.X != right.X || left.Y != right.Y; + } + + public static Point operator +(Point left, Point right) + { + return new Point(left.X + right.X, left.Y + right.Y); + } + + public static Point operator -(Point left, Point right) + { + return new Point(left.X - right.X, left.Y - right.Y); + } + + public static Point operator -(Point unary) + { + return new Point(-unary.X, -unary.Y); + } + + public static Point operator *(Point point, int scalar) + { + return new Point(point.X * scalar, point.Y * scalar); + } + + public static Point operator /(Point point, int scalar) + { + return new Point(point.X / scalar, point.Y / scalar); + } } } diff --git a/ConsoleGameLib/CoreTypes/Size.cs b/ConsoleGameLib/CoreTypes/Size.cs index c92ce0b..7a7738a 100644 --- a/ConsoleGameLib/CoreTypes/Size.cs +++ b/ConsoleGameLib/CoreTypes/Size.cs @@ -15,5 +15,59 @@ public Size(int width, int height) public int Width; public int Height; + + public override int GetHashCode() + { + // Uses http://stackoverflow.com/questions/263400/what-is-the-best-algorithm-for-an-overridden-system-object-gethashcode + unchecked // Overflow is fine, just wrap + { + int hash = 29; + hash = hash * 89 + Width.GetHashCode(); + hash = hash * 89 + Height.GetHashCode(); + return hash; + } + } + + public override bool Equals(object obj) + { + if (obj == null || !(obj is Size)) + { + return false; + } + Size oth = (Size)obj; + return Width == oth.Width && Height == oth.Height; + } + + public static bool operator ==(Size left, Size right) + { + // Struct, so no need for null check + return left.Width == right.Width && left.Height == right.Height; + } + + public static bool operator !=(Size left, Size right) + { + // Struct, so no need for null check + return left.Width != right.Width || left.Height != right.Height; + } + + public static Size operator +(Size left, Size right) + { + return new Size(left.Width + right.Width, left.Height + right.Height); + } + + public static Size operator -(Size left, Size right) + { + return new Size(left.Width - right.Width, left.Height - right.Height); + } + + public static Size operator *(Size point, int scalar) + { + return new Size(point.Width * scalar, point.Height * scalar); + } + + public static Size operator /(Size point, int scalar) + { + return new Size(point.Width / scalar, point.Width / scalar); + } } -} +} \ No newline at end of file