~ruther/NosSmooth

ref: 3d131cb63fb7f4ea5029300e8487384e94440cc8 NosSmooth/Core/NosSmooth.Game/Data/Info/Position.cs -rw-r--r-- 3.0 KiB
3d131cb6 — Rutherther Merge pull request #51 from Rutherther/feat/easy-api 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//
//  Position.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Diagnostics.CodeAnalysis;

namespace NosSmooth.Game.Data.Info;

/// <summary>
/// Represents nostale position on map.
/// </summary>
[SuppressMessage
(
    "StyleCop.CSharp.NamingRules",
    "SA1313",
    MessageId = "Parameter names should begin with lower-case letter",
    Justification = "Standard."
)]
public record struct Position(short X, short Y)
{
    /// <summary>
    /// Gets the zero position.
    /// </summary>
    public static Position Zero => new Position(0, 0);

    /// <summary>
    /// Get the squared distance to the given position.
    /// </summary>
    /// <param name="position">The position.</param>
    /// <returns>The distance squared.</returns>
    public long DistanceSquared(Position position)
    {
        return ((position.X - X) * (position.X - X)) + ((position.Y - Y) * (position.Y - Y));
    }

    /// <summary>
    /// Gets whether the given position is in the given range.
    /// </summary>
    /// <param name="position">The position.</param>
    /// <param name="range">The range.</param>
    /// <returns>Whether the position is in the range.</returns>
    public bool IsInRange(Position position, float range)
    {
        return DistanceSquared(position) <= range * range;
    }

    /// <summary>
    /// Multiply position.
    /// </summary>
    /// <param name="left">The left position.</param>
    /// <param name="right">The right position.</param>
    /// <returns>The multiplied position.</returns>
    public static Position operator *(short left, Position right)
    {
        return new Position((short)(left * right.X), (short)(left * right.Y));
    }

    /// <summary>
    /// Multiply position.
    /// </summary>
    /// <param name="left">The left position.</param>
    /// <param name="right">The right position.</param>
    /// <returns>The multiplied position.</returns>
    public static Position operator *(double left, Position right)
    {
        return new Position((short)(left * right.X), (short)(left * right.Y));
    }

    /// <summary>
    /// Add two positions.
    /// </summary>
    /// <param name="left">The left position.</param>
    /// <param name="right">The right position.</param>
    /// <returns>The added position.</returns>
    public static Position operator +(Position left, Position right)
    {
        return new Position((short)(left.X + right.X), (short)(left.Y + right.Y));
    }

    /// <summary>
    /// Subtract two positions.
    /// </summary>
    /// <param name="left">The left position.</param>
    /// <param name="right">The right position.</param>
    /// <returns>The subtracted position.</returns>
    public static Position operator -(Position left, Position right)
    {
        return new Position((short)(left.X - right.X), (short)(left.Y - right.Y));
    }

    /// <inheritdoc />
    public override string ToString()
        => $"{X}, {Y}";
}
Do not follow this link