From af9dc24f0a53aa48383d8289033aa37a567b4749 Mon Sep 17 00:00:00 2001 From: Rutherther Date: Fri, 18 Feb 2022 23:23:50 +0100 Subject: [PATCH] feat(game): add position operators --- Core/NosSmooth.Game/Data/Info/Position.cs | 70 ++++++++++++++++++++++- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/Core/NosSmooth.Game/Data/Info/Position.cs b/Core/NosSmooth.Game/Data/Info/Position.cs index da8268b..af727e8 100644 --- a/Core/NosSmooth.Game/Data/Info/Position.cs +++ b/Core/NosSmooth.Game/Data/Info/Position.cs @@ -11,9 +11,20 @@ namespace NosSmooth.Game.Data.Info; /// /// Represents nostale position on map. /// -[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1313", MessageId = "Parameter names should begin with lower-case letter", Justification = "Standard.")] -public record struct Position(long X, long Y) +[SuppressMessage +( + "StyleCop.CSharp.NamingRules", + "SA1313", + MessageId = "Parameter names should begin with lower-case letter", + Justification = "Standard." +)] +public record struct Position(short X, short Y) { + /// + /// Gets the zero position. + /// + public static Position Zero => new Position(0, 0); + /// /// Get the squared distance to the given position. /// @@ -23,4 +34,59 @@ public record struct Position(long X, long Y) { return ((position.X - X) * (position.X - X)) + ((position.Y - Y) * (position.Y - Y)); } + + /// + /// Gets whether the given position is in the given range. + /// + /// The position. + /// The range. + /// Whether the position is in the range. + public bool IsInRange(Position position, float range) + { + return DistanceSquared(position) <= range * range; + } + + /// + /// Multiply position. + /// + /// The left position. + /// The right position. + /// The multiplied position. + public static Position operator *(short left, Position right) + { + return new Position((short)(left * right.X), (short)(left * right.Y)); + } + + /// + /// Multiply position. + /// + /// The left position. + /// The right position. + /// The multiplied position. + public static Position operator *(double left, Position right) + { + return new Position((short)(left * right.X), (short)(left * right.Y)); + } + + /// + /// Add two positions. + /// + /// The left position. + /// The right position. + /// The added position. + public static Position operator +(Position left, Position right) + { + return new Position((short)(left.X + right.X), (short)(left.Y + right.Y)); + } + + /// + /// Subtract two positions. + /// + /// The left position. + /// The right position. + /// The subtracted position. + public static Position operator -(Position left, Position right) + { + return new Position((short)(left.X - right.X), (short)(left.Y - right.Y)); + } } \ No newline at end of file -- 2.49.0