From 33cf1aa4a4c4116d283c8569ec95c42a0d18e470 Mon Sep 17 00:00:00 2001 From: Maddy Thorson Date: Tue, 23 Jul 2024 17:52:00 -0700 Subject: [PATCH] Better Facing * operators with Point2 and Vector2 --- Framework/Spatial/Facing.cs | 25 ++++++++++++++++++++++++- Framework/Spatial/Point2.cs | 2 -- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/Framework/Spatial/Facing.cs b/Framework/Spatial/Facing.cs index 3539a94..ce116a0 100644 --- a/Framework/Spatial/Facing.cs +++ b/Framework/Spatial/Facing.cs @@ -17,12 +17,30 @@ public readonly struct Facing(int val) : IEquatable /// Returns -1 if Left, or +1 if Right /// public int Sign => value < 0 ? -1 : 1; - + + /// + /// The opposite of our value + /// public Facing Reverse => new(-value); + /// + /// Integers convert to Left if negative, otherwise Right + /// public static implicit operator Facing(int v) => v < 0 ? Left : Right; + + /// + /// -1 for Left, 1 for Right + /// public static implicit operator int(Facing f) => f.Sign; + + /// + /// Cast to a unit Point2 in our direction + /// public static explicit operator Point2(Facing f) => Point2.UnitX * f.Sign; + + /// + /// Cast to a unit Vector2 in our direction + /// public static explicit operator Vector2(Facing f) => Vector2.UnitX * f.Sign; public static bool operator ==(Facing a, Facing b) => a.Sign == b.Sign; @@ -30,6 +48,11 @@ public readonly struct Facing(int val) : IEquatable public static int operator *(Facing a, int b) => (int)a * b; public static int operator *(int a, Facing b) => a * (int)b; + public static Point2 operator *(Point2 point, Facing facing) => new(point.X * facing.Sign, point.Y); + public static Point2 operator *(Facing facing, Point2 point) => new(point.X * facing.Sign, point.Y); + public static Vector2 operator *(Vector2 vec, Facing facing) => new(vec.X * facing.Sign, vec.Y); + public static Vector2 operator *(Facing facing, Vector2 vec) => new(vec.X * facing.Sign, vec.Y); + public override int GetHashCode() => Sign; public override bool Equals(object? obj) => obj != null && obj is Facing f && f == this; diff --git a/Framework/Spatial/Point2.cs b/Framework/Spatial/Point2.cs index 4fd8077..2ed5a03 100644 --- a/Framework/Spatial/Point2.cs +++ b/Framework/Spatial/Point2.cs @@ -149,8 +149,6 @@ public static Point2 FromBools(bool left, bool right, bool up, bool down) public static Point2 operator +(Point2 a, Point2 b) => new(a.X + b.X, a.Y + b.Y); public static Point2 operator -(Point2 a, Point2 b) => new(a.X - b.X, a.Y - b.Y); - public static Point2 operator *(Point2 point, Facing facing) => new(point.X * facing.Sign, point.Y); - public static Rect operator +(Point2 a, Rect b) => new(a.X + b.X, a.Y + b.Y, b.Width, b.Height); public static Rect operator +(Rect a, Point2 b) => new(b.X + a.X, b.Y + a.Y, a.Width, a.Height);