-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGridVector.cs
164 lines (127 loc) · 5.78 KB
/
GridVector.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
using System;
using System.Grid;
namespace UnityEngine
{
/// <summary>
/// Represent the coordinates of the 2D grid. The value of each component is greater than or equal to 0.
/// </summary>
[Serializable]
public struct GridVector : IEquatable<GridVector>
{
[SerializeField, Min(0)]
private int row;
public int Row
{
get => this.row;
set => this.row = Mathf.Max(value, 0);
}
[SerializeField, Min(0)]
private int column;
public int Column
{
get => this.column;
set => this.column = Mathf.Max(value, 0);
}
public int this[int index]
{
get
{
if (index == 0) return this.row;
if (index == 1) return this.column;
throw new IndexOutOfRangeException();
}
}
public GridVector(int row, int column)
{
this.row = Mathf.Max(row, 0);
this.column = Mathf.Max(column, 0);
}
public void Deconstruct(out int row, out int column)
{
row = this.row;
column = this.column;
}
public GridVector With(int? Row = null, int? Column = null)
=> new GridVector(
Row ?? this.row,
Column ?? this.column
);
public int ToIndex1(int columnCount)
=> columnCount <= 0 ? 0 : this.column + this.row * columnCount;
public int ToIndex1(in GridVector size)
=> size.column <= 0 ? 0 : this.column + this.row * size.column;
public override bool Equals(object obj)
=> obj is GridVector other && this.row == other.row && this.column == other.column;
public bool Equals(GridVector other)
=> this.row == other.row && this.column == other.column;
public bool Equals(in GridVector other)
=> this.row == other.row && this.column == other.column;
public override int GetHashCode()
{
#if USE_SYSTEM_HASHCODE
return HashCode.Combine(this.row, this.column);
#endif
#pragma warning disable CS0162 // Unreachable code detected
var hashCode = -1663278630;
hashCode = hashCode * -1521134295 + this.row.GetHashCode();
hashCode = hashCode * -1521134295 + this.column.GetHashCode();
return hashCode;
#pragma warning restore CS0162 // Unreachable code detected
}
public override string ToString()
=> $"({this.row}, {this.column})";
/// <summary>
/// Shorthand for writing GridVector(0, 0)
/// </summary>
public static GridVector Zero { get; } = new GridVector(0, 0);
/// <summary>
/// Shorthand for writing GridVector(1, 1)
/// </summary>
public static GridVector One { get; } = new GridVector(1, 1);
/// <summary>
/// Shorthand for writing GridVector(0, 1)
/// </summary>
public static GridVector Right { get; } = new GridVector(0, 1);
/// <summary>
/// Shorthand for writing GridVector(1, 0)
/// </summary>
public static GridVector Up { get; } = new GridVector(1, 0);
public static implicit operator GridVector(in (int row, int column) value)
=> new GridVector(value.row, value.column);
public static implicit operator GridIndex(in GridVector value)
=> new GridIndex(value.row, value.column);
public static implicit operator ClampedGridSize(in GridVector value)
=> new GridIndex(value.row, value.column);
public static implicit operator GridSize(in GridVector value)
=> new GridIndex(value.row, value.column);
public static implicit operator GridVector( in GridIndex value)
=> new GridVector(value.Row, value.Column);
public static bool operator ==(in GridVector lhs, in GridVector rhs)
=> lhs.row == rhs.row && lhs.column == rhs.column;
public static bool operator !=(in GridVector lhs, in GridVector rhs)
=> lhs.row != rhs.row || lhs.column != rhs.column;
public static GridVector operator +(in GridVector lhs, in GridVector rhs)
=> new GridVector(lhs.row + rhs.row, lhs.column + rhs.column);
public static GridVector operator -(in GridVector lhs, in GridVector rhs)
=> new GridVector(lhs.row - rhs.row, lhs.column - rhs.column);
public static GridVector operator *(in GridVector lhs, int rhs)
=> new GridVector(lhs.row * rhs, lhs.column * rhs);
public static GridVector operator *(int lhs, in GridVector rhs)
=> new GridVector(rhs.row * lhs, rhs.column * lhs);
public static GridVector operator *(in GridVector lhs, in GridVector rhs)
=> new GridVector(lhs.row * rhs.Row, lhs.column * rhs.Column);
public static GridVector operator /(in GridVector lhs, int rhs)
=> new GridVector(lhs.row / rhs, lhs.column / rhs);
public static GridVector operator /(in GridVector lhs, in GridVector rhs)
=> new GridVector(lhs.row / rhs.Row, lhs.column / rhs.Column);
public static GridVector operator %(in GridVector lhs, int rhs)
=> new GridVector(lhs.row % rhs, lhs.column % rhs);
public static GridVector operator %(in GridVector lhs, in GridVector rhs)
=> new GridVector(lhs.row % rhs.Row, lhs.column % rhs.Column);
public static GridVector Clamp(in GridVector value, in GridVector min, in GridVector max)
=> new GridVector(
value.row < min.row ? min.row : (value.row > max.row ? max.row : value.row),
value.column < min.column ? min.column : (value.column > max.column ? max.column : value.column)
);
}
}