Skip to content

Commit

Permalink
Better Facing * operators with Point2 and Vector2
Browse files Browse the repository at this point in the history
  • Loading branch information
MaddyThorson committed Jul 24, 2024
1 parent 4f478a7 commit 33cf1aa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
25 changes: 24 additions & 1 deletion Framework/Spatial/Facing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,42 @@ public readonly struct Facing(int val) : IEquatable<Facing>
/// Returns -1 if Left, or +1 if Right
/// </summary>
public int Sign => value < 0 ? -1 : 1;


/// <summary>
/// The opposite of our value
/// </summary>
public Facing Reverse => new(-value);

/// <summary>
/// Integers convert to Left if negative, otherwise Right
/// </summary>
public static implicit operator Facing(int v) => v < 0 ? Left : Right;

/// <summary>
/// -1 for Left, 1 for Right
/// </summary>
public static implicit operator int(Facing f) => f.Sign;

/// <summary>
/// Cast to a unit Point2 in our direction
/// </summary>
public static explicit operator Point2(Facing f) => Point2.UnitX * f.Sign;

/// <summary>
/// Cast to a unit Vector2 in our direction
/// </summary>
public static explicit operator Vector2(Facing f) => Vector2.UnitX * f.Sign;

public static bool operator ==(Facing a, Facing b) => a.Sign == b.Sign;
public static bool operator !=(Facing a, Facing b) => a.Sign != b.Sign;
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;
Expand Down
2 changes: 0 additions & 2 deletions Framework/Spatial/Point2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 33cf1aa

Please sign in to comment.