mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-26 06:28:35 +01:00
improved Direction2 code style
This commit is contained in:
parent
f352e6b437
commit
8d92131630
1 changed files with 36 additions and 37 deletions
|
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Runtime.Serialization;
|
using System.Runtime.Serialization;
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
|
using static MLEM.Misc.Direction2;
|
||||||
|
|
||||||
namespace MLEM.Misc {
|
namespace MLEM.Misc {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -83,9 +84,9 @@ namespace MLEM.Misc {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// All directions except <see cref="Direction2.None"/>
|
/// All directions except <see cref="Direction2.None"/>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static readonly Direction2[] AllExceptNone = All.Where(dir => dir != Direction2.None).ToArray();
|
public static readonly Direction2[] AllExceptNone = All.Where(dir => dir != None).ToArray();
|
||||||
|
|
||||||
private static readonly Direction2[] Clockwise = {Direction2.Up, Direction2.UpRight, Direction2.Right, Direction2.DownRight, Direction2.Down, Direction2.DownLeft, Direction2.Left, Direction2.UpLeft};
|
private static readonly Direction2[] Clockwise = {Up, UpRight, Right, DownRight, Down, DownLeft, Left, UpLeft};
|
||||||
private static readonly Dictionary<Direction2, int> ClockwiseLookup = Clockwise.Select((d, i) => (d, i)).ToDictionary(kv => kv.d, kv => kv.i);
|
private static readonly Dictionary<Direction2, int> ClockwiseLookup = Clockwise.Select((d, i) => (d, i)).ToDictionary(kv => kv.d, kv => kv.i);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -95,7 +96,7 @@ namespace MLEM.Misc {
|
||||||
/// <param name="dir">The direction to query</param>
|
/// <param name="dir">The direction to query</param>
|
||||||
/// <returns>Whether the direction is adjacent</returns>
|
/// <returns>Whether the direction is adjacent</returns>
|
||||||
public static bool IsAdjacent(this Direction2 dir) {
|
public static bool IsAdjacent(this Direction2 dir) {
|
||||||
return dir == Direction2.Up || dir == Direction2.Right || dir == Direction2.Down || dir == Direction2.Left;
|
return dir == Up || dir == Right || dir == Down || dir == Left;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -104,7 +105,7 @@ namespace MLEM.Misc {
|
||||||
/// <param name="dir">The direction to query</param>
|
/// <param name="dir">The direction to query</param>
|
||||||
/// <returns>Whether the direction is diagonal</returns>
|
/// <returns>Whether the direction is diagonal</returns>
|
||||||
public static bool IsDiagonal(this Direction2 dir) {
|
public static bool IsDiagonal(this Direction2 dir) {
|
||||||
return dir == Direction2.UpRight || dir == Direction2.DownRight || dir == Direction2.UpLeft || dir == Direction2.DownLeft;
|
return dir == UpRight || dir == DownRight || dir == UpLeft || dir == DownLeft;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -115,21 +116,21 @@ namespace MLEM.Misc {
|
||||||
/// <returns>The direction's offset</returns>
|
/// <returns>The direction's offset</returns>
|
||||||
public static Point Offset(this Direction2 dir) {
|
public static Point Offset(this Direction2 dir) {
|
||||||
switch (dir) {
|
switch (dir) {
|
||||||
case Direction2.Up:
|
case Up:
|
||||||
return new Point(0, -1);
|
return new Point(0, -1);
|
||||||
case Direction2.Right:
|
case Right:
|
||||||
return new Point(1, 0);
|
return new Point(1, 0);
|
||||||
case Direction2.Down:
|
case Down:
|
||||||
return new Point(0, 1);
|
return new Point(0, 1);
|
||||||
case Direction2.Left:
|
case Left:
|
||||||
return new Point(-1, 0);
|
return new Point(-1, 0);
|
||||||
case Direction2.UpRight:
|
case UpRight:
|
||||||
return new Point(1, -1);
|
return new Point(1, -1);
|
||||||
case Direction2.DownRight:
|
case DownRight:
|
||||||
return new Point(1, 1);
|
return new Point(1, 1);
|
||||||
case Direction2.DownLeft:
|
case DownLeft:
|
||||||
return new Point(-1, 1);
|
return new Point(-1, 1);
|
||||||
case Direction2.UpLeft:
|
case UpLeft:
|
||||||
return new Point(-1, -1);
|
return new Point(-1, -1);
|
||||||
default:
|
default:
|
||||||
return Point.Zero;
|
return Point.Zero;
|
||||||
|
@ -155,24 +156,24 @@ namespace MLEM.Misc {
|
||||||
/// <returns>The opposite of the direction</returns>
|
/// <returns>The opposite of the direction</returns>
|
||||||
public static Direction2 Opposite(this Direction2 dir) {
|
public static Direction2 Opposite(this Direction2 dir) {
|
||||||
switch (dir) {
|
switch (dir) {
|
||||||
case Direction2.Up:
|
case Up:
|
||||||
return Direction2.Down;
|
return Down;
|
||||||
case Direction2.Right:
|
case Right:
|
||||||
return Direction2.Left;
|
return Left;
|
||||||
case Direction2.Down:
|
case Down:
|
||||||
return Direction2.Up;
|
return Up;
|
||||||
case Direction2.Left:
|
case Left:
|
||||||
return Direction2.Right;
|
return Right;
|
||||||
case Direction2.UpRight:
|
case UpRight:
|
||||||
return Direction2.DownLeft;
|
return DownLeft;
|
||||||
case Direction2.DownRight:
|
case DownRight:
|
||||||
return Direction2.UpLeft;
|
return UpLeft;
|
||||||
case Direction2.DownLeft:
|
case DownLeft:
|
||||||
return Direction2.UpRight;
|
return UpRight;
|
||||||
case Direction2.UpLeft:
|
case UpLeft:
|
||||||
return Direction2.DownRight;
|
return DownRight;
|
||||||
default:
|
default:
|
||||||
return Direction2.None;
|
return None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -204,9 +205,7 @@ namespace MLEM.Misc {
|
||||||
/// <returns>The rotated direction</returns>
|
/// <returns>The rotated direction</returns>
|
||||||
public static Direction2 RotateCcw(this Direction2 dir, bool fortyFiveDegrees = false) {
|
public static Direction2 RotateCcw(this Direction2 dir, bool fortyFiveDegrees = false) {
|
||||||
var index = ClockwiseLookup[dir] - (fortyFiveDegrees ? 1 : 2);
|
var index = ClockwiseLookup[dir] - (fortyFiveDegrees ? 1 : 2);
|
||||||
if (index < 0)
|
return Clockwise[index < 0 ? index + Clockwise.Length : index];
|
||||||
index += Clockwise.Length;
|
|
||||||
return Clockwise[index % Clockwise.Length];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -220,7 +219,7 @@ namespace MLEM.Misc {
|
||||||
if (Math.Abs(dir.Angle() - offsetAngle) <= MathHelper.PiOver4 / 2)
|
if (Math.Abs(dir.Angle() - offsetAngle) <= MathHelper.PiOver4 / 2)
|
||||||
return dir;
|
return dir;
|
||||||
}
|
}
|
||||||
return Direction2.None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -231,10 +230,10 @@ namespace MLEM.Misc {
|
||||||
/// <returns>The vector's direction</returns>
|
/// <returns>The vector's direction</returns>
|
||||||
public static Direction2 To90Direction(this Vector2 offset) {
|
public static Direction2 To90Direction(this Vector2 offset) {
|
||||||
if (offset.X == 0 && offset.Y == 0)
|
if (offset.X == 0 && offset.Y == 0)
|
||||||
return Direction2.None;
|
return None;
|
||||||
if (Math.Abs(offset.X) > Math.Abs(offset.Y))
|
if (Math.Abs(offset.X) > Math.Abs(offset.Y))
|
||||||
return offset.X > 0 ? Direction2.Right : Direction2.Left;
|
return offset.X > 0 ? Right : Left;
|
||||||
return offset.Y > 0 ? Direction2.Down : Direction2.Up;
|
return offset.Y > 0 ? Down : Up;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -244,7 +243,7 @@ namespace MLEM.Misc {
|
||||||
/// <param name="reference">The direction to rotate by</param>
|
/// <param name="reference">The direction to rotate by</param>
|
||||||
/// <param name="start">The direction to use as the default direction</param>
|
/// <param name="start">The direction to use as the default direction</param>
|
||||||
/// <returns>The direction, rotated by the reference direction</returns>
|
/// <returns>The direction, rotated by the reference direction</returns>
|
||||||
public static Direction2 RotateBy(this Direction2 dir, Direction2 reference, Direction2 start = Direction2.Up) {
|
public static Direction2 RotateBy(this Direction2 dir, Direction2 reference, Direction2 start = Up) {
|
||||||
var diff = ClockwiseLookup[reference] - ClockwiseLookup[start];
|
var diff = ClockwiseLookup[reference] - ClockwiseLookup[start];
|
||||||
if (diff < 0)
|
if (diff < 0)
|
||||||
diff += Clockwise.Length;
|
diff += Clockwise.Length;
|
||||||
|
|
Loading…
Reference in a new issue