From 3aff5a3c3fe54ddf90b6c2df0f09658338e4c1ed Mon Sep 17 00:00:00 2001 From: Maddy Thorson Date: Sat, 13 Feb 2021 16:50:50 -0800 Subject: [PATCH] Struct helpers --- src/Struct/Facings.bf | 3 +++ src/Struct/Point.bf | 26 +++++++++++++++++++------- src/Struct/Vector.bf | 9 +++++++++ 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/Struct/Facings.bf b/src/Struct/Facings.bf index a40db87..57a6e3f 100644 --- a/src/Struct/Facings.bf +++ b/src/Struct/Facings.bf @@ -6,6 +6,9 @@ namespace Strawberry case Right = 1; case Left = -1; + [Inline] + public float Angle => this == .Right ? Calc.Right : Calc.Left; + public Facings Opposite() { if (this == .Right) diff --git a/src/Struct/Point.bf b/src/Struct/Point.bf index a8d4587..0b4e513 100644 --- a/src/Struct/Point.bf +++ b/src/Struct/Point.bf @@ -64,7 +64,7 @@ namespace Strawberry [Inline] static public explicit operator Point(Vector a) { - return Point((int)a.X, (int)a.Y); + return .((int)a.X, (int)a.Y); } [Inline] @@ -82,37 +82,49 @@ namespace Strawberry [Inline] static public Point operator+(Point a, Point b) { - return Point(a.X + b.X, a.Y + b.Y); + return .(a.X + b.X, a.Y + b.Y); } [Inline] static public Point operator-(Point a, Point b) { - return Point(a.X - b.X, a.Y - b.Y); + return .(a.X - b.X, a.Y - b.Y); } [Inline, Commutable] static public Point operator*(Point a, int b) { - return Point(a.X * b, a.Y * b); + return .(a.X * b, a.Y * b); } [Inline] static public Point operator*(Point a, Point b) { - return Point(a.X * b.X, a.Y * b.Y); + return .(a.X * b.X, a.Y * b.Y); } [Inline] static public Point operator/(Point a, int b) { - return Point(a.X / b, a.Y / b); + return .(a.X / b, a.Y / b); + } + + [Inline, Commutable] + static public Vector operator*(Point a, float b) + { + return .(a.X * b, a.Y * b); + } + + [Inline] + static public Vector operator/(Point a, float b) + { + return .(a.X / b, a.Y / b); } [Inline] static public Point operator/(Point a, Point b) { - return Point(a.X / b.X, a.Y / b.Y); + return .(a.X / b.X, a.Y / b.Y); } [Inline, Commutable] diff --git a/src/Struct/Vector.bf b/src/Struct/Vector.bf index 5ceebc9..9cb17b4 100644 --- a/src/Struct/Vector.bf +++ b/src/Struct/Vector.bf @@ -49,6 +49,9 @@ namespace Strawberry [Inline] public float LengthSquared => X * X + Y * Y; + [Inline] + public float Angle => Math.Atan2(Y, X); + [Inline] public Point Round() { @@ -67,6 +70,12 @@ namespace Strawberry return Transform(this, mat); } + [Inline] + static public Vector FromAngle(float radians) + { + return .(Math.Cos(radians), Math.Sin(radians)); + } + [Inline] static public Vector Lerp(Vector a, Vector b, float t) {