From 368630f1095c8e67e8c40b37b1d985f05cae2e5a Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Mon, 10 Aug 2020 02:37:39 +0200 Subject: [PATCH] Added Direction2Helper.To90Direction --- MLEM/Misc/Direction2.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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