diff --git a/MLEM/Misc/Direction2.cs b/MLEM/Misc/Direction2.cs index 790b575..71e8ee4 100644 --- a/MLEM/Misc/Direction2.cs +++ b/MLEM/Misc/Direction2.cs @@ -244,5 +244,19 @@ namespace MLEM.Misc { return Direction2.None; } + /// + /// Returns the that is closest to the given position's facing direction, only taking directions into account. + /// Diagonal directions will be rounded to the nearest vertical direction. + /// + /// The vector whose corresponding direction to get + /// The vector's direction + public static Direction2 To90Direction(this Vector2 offset) { + if (offset.X == 0 && offset.Y == 0) + return Direction2.None; + if (Math.Abs(offset.X) > Math.Abs(offset.Y)) + return offset.X > 0 ? Direction2.Right : Direction2.Left; + return offset.Y > 0 ? Direction2.Down : Direction2.Up; + } + } } \ No newline at end of file