/
FOVCalculateParameters.cs
182 lines (161 loc) · 7.48 KB
/
FOVCalculateParameters.cs
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using JetBrains.Annotations;
using SadRogue.Primitives;
namespace GoRogue.FOV
{
/// <summary>
/// Represents a set of parameters that were passed to a call to Calculate.
/// </summary>
[PublicAPI]
public readonly struct FOVCalculateParameters : IEquatable<FOVCalculateParameters>, IMatchable<FOVCalculateParameters>
{
/// <summary>
/// Position of the FOV origin point.
/// </summary>
[DataMember] public readonly Point Origin;
/// <summary>
/// The maximum radius -- eg. the maximum distance of the field of view if completely unobstructed.
/// </summary>
[DataMember] public readonly double Radius;
/// <summary>
/// The distance calculation used to determine what shape the radius has (or a type
/// implicitly convertible to <see cref="SadRogue.Primitives.Distance" />, eg. <see cref="SadRogue.Primitives.Radius" />).
/// </summary>
[DataMember] public readonly Distance DistanceCalc;
/// <summary>
/// The angle in degrees that specifies the outermost center point of the field of view cone. 0 degrees
/// points upward, and increases move clockwise (like a compass)
/// </summary>
[DataMember] public readonly double Angle;
/// <summary>
/// The angle, in degrees, that specifies the full arc contained in the field of view cone --
/// <see cref="Span"/> / 2 degrees are included on either side of the span line.
/// </summary>
[DataMember] public readonly double Span;
/// <summary>
/// Constructor.
/// </summary>
/// <param name="origin">Position of the FOV origin point.</param>
/// <param name="radius">The maximum radius -- eg. the maximum distance of the field of view if completely unobstructed.</param>
/// <param name="distanceCalc">
/// The distance calculation used to determine what shape the radius has (or a type
/// implicitly convertible to <see cref="SadRogue.Primitives.Distance" />, eg. <see cref="SadRogue.Primitives.Radius" />).
/// </param>
/// <param name="angle">
/// The angle in degrees that specifies the outermost center point of the field of view cone. 0 degrees
/// points up, and increases move the cone clockwise (like a compass).
/// </param>
/// <param name="span">
/// The angle, in degrees, that specifies the full arc contained in the field of view cone --
/// <paramref name="span"/>> / 2 degrees are included on either side of the span line.
/// </param>
public FOVCalculateParameters(Point origin, double radius, Distance distanceCalc, double angle = 0.0, double span = 360.0)
{
Origin = origin;
Radius = radius;
DistanceCalc = distanceCalc;
Angle = angle;
Span = span;
}
#region Tuple Compatibility
/// <summary>
/// Supports C# Deconstruction syntax.
/// </summary>
/// <param name="origin"/>
/// <param name="radius"/>
/// <param name="distanceCalc"/>
/// <param name="angle"/>
/// <param name="span"/>
public void Deconstruct(out Point origin, out double radius, out Distance distanceCalc, out double angle, out double span)
{
origin = Origin;
radius = Radius;
distanceCalc = DistanceCalc;
angle = Angle;
span = Span;
}
/// <summary>
/// Implicitly converts an FOVCalculateParameters object to an equivalent tuple.
/// </summary>
/// <param name="pair"/>
/// <returns/>
public static implicit operator (Point origin, double radius, Distance distanceCalc, double angle, double span)(FOVCalculateParameters pair)
=> pair.ToTuple();
/// <summary>
/// Implicitly converts a tuple to its equivalent FOVCalculateParameters.
/// </summary>
/// <param name="tuple"/>
/// <returns/>
public static implicit operator FOVCalculateParameters((Point origin, double radius, Distance distanceCalc, double angle, double span) tuple)
=> FromTuple(tuple);
/// <summary>
/// Converts this FOVCalculateParameters object to an equivalent tuple.
/// </summary>
/// <returns/>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public (Point origin, double radius, Distance distanceCalc, double angle, double span) ToTuple()
=> (Origin, Radius, DistanceCalc, Angle, Span);
/// <summary>
/// Converts the tuple to an equivalent FOVCalculateParameters object.
/// </summary>
/// <param name="tuple"/>
/// <returns/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static FOVCalculateParameters FromTuple((Point origin, double radius, Distance distanceCalc, double angle, double span) tuple)
=> new FOVCalculateParameters(tuple.origin, tuple.radius, tuple.distanceCalc, tuple.angle, tuple.span);
#endregion
#region EqualityComparison
/// <summary>
/// True if the given objects have the same parameter values; false otherwise.
/// </summary>
/// <param name="other"/>
/// <returns/>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(FOVCalculateParameters other)
{
// ReSharper disable CompareOfFloatsByEqualityOperator
return Origin == other.Origin && Radius == other.Radius && DistanceCalc.Matches(other.DistanceCalc) && Angle == other.Angle && Span == other.Span;
// ReSharper restore CompareOfFloatsByEqualityOperator
}
/// <summary>
/// True if the given object has the same parameter values; false otherwise.
/// </summary>
/// <param name="other"/>
/// <returns/>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Matches(FOVCalculateParameters other) => Equals(other);
/// <summary>
/// True if the given object is a FOVCalculateParameters and has the same parameter values; false otherwise.
/// </summary>
/// <param name="obj"/>
/// <returns/>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override bool Equals(object? obj) => obj is FOVCalculateParameters pair && Equals(pair);
/// <summary>
/// Returns a hash code based on all of the object's fields.
/// </summary>
/// <returns/>
public override int GetHashCode() => HashCode.Combine(Origin, Radius, DistanceCalc, Angle, Span);
/// <summary>
/// True if the given objects have the same parameter values; false otherwise.
/// </summary>
/// <param name="left"/>
/// <param name="right"/>
/// <returns/>
public static bool operator ==(FOVCalculateParameters left, FOVCalculateParameters right) => left.Equals(right);
/// <summary>
/// True if the given objects have different parameter values; false otherwise.
/// </summary>
/// <param name="left"/>
/// <param name="right"/>
/// <returns/>
public static bool operator !=(FOVCalculateParameters left, FOVCalculateParameters right) => !(left == right);
#endregion
}
}