-
Notifications
You must be signed in to change notification settings - Fork 27
/
GridSensorComponent2D.cs
291 lines (248 loc) · 9.45 KB
/
GridSensorComponent2D.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
using UnityEngine;
using NaughtyAttributes;
namespace MBaske.Sensors.Grid
{
/// <summary>
/// Sensor component for detecting gameobjects on a plane.
/// </summary>
[HelpURL("https://github.com/mbaske/grid-sensor")]
[RequireComponent(typeof(GridEditorHelper2D))]
public class GridSensorComponent2D : GridSensorComponentBaseGO
{
protected override DetectorSpaceType DetectorSpaceType => DetectorSpaceType.Box;
#region Geometry Settings
/// <summary>
/// Whether and how to rotate detection bounds with the sensor component.
/// </summary>
public GameObjectDetector2D.SensorRotationType RotationType
{
get { return m_RotationType; }
set { m_RotationType = value; OnRotationTypeChange(); }
}
[SerializeField]
[OnValueChanged("OnRotationTypeChange")]
[Foldout("Geometry")]
[Tooltip("Sensor rotation type.")]
private GameObjectDetector2D.SensorRotationType m_RotationType
= GameObjectDetector2D.SensorRotationType.AgentY;
// Default world rotation.
private Quaternion m_WorldRotation = Quaternion.LookRotation(Vector3.forward);
private void OnRotationTypeChange()
{
if (HasSensor)
{
((GameObjectDetector2D)m_GridSensor.Detector)
.RotationType = RotationType;
}
}
/// <summary>
/// Rounded detection bounds (actual sensor bounds).
/// </summary>
public Bounds DetectionBounds
{
get { return m_DetectionBounds; }
}
[SerializeField, HideInInspector]
private Bounds m_DetectionBounds = new Bounds(Vector3.zero, new Vector3(20, 1, 20));
/// <summary>
/// Unrounded Scene GUI bounds used for editing.
/// </summary>
public Bounds EditorBounds
{
get { return m_EditorBounds; }
set
{
m_EditorBounds = value;
OnEditorBoundsChange();
}
}
[SerializeField, HideInInspector]
private Bounds m_EditorBounds = new Bounds(Vector3.zero, new Vector3(20, 1, 20));
private void OnEditorBoundsChange()
{
m_Offset = m_EditorBounds.center;
m_BoundsSize = m_EditorBounds.size;
m_DetectionBounds.center = m_EditorBounds.center;
RoundDetectionBoundsSize();
// Saving for Scene GUI Undo.
SaveState(OnEditorBoundsChange, "Grid Sensor Change");
}
/// <summary>
/// X/Z size of each grid cell.
/// </summary>
public float CellSize
{
get { return m_CellSize; }
}
[SerializeField]
[OnValueChanged("OnCellSizeChange")]
[Foldout("Geometry")]
[Tooltip("X/Z size of individual grid cells.")]
[Min(0.1f)] private float m_CellSize = 1;
private void OnCellSizeChange()
{
Bounds b = m_EditorBounds;
b.size = new Vector3(
Mathf.Max(b.size.x, m_CellSize),
b.size.y,
Mathf.Max(b.size.z, m_CellSize));
EditorBounds = b;
}
[SerializeField]
[OnValueChanged("OnNumCellsChange")]
[Foldout("Geometry")]
[Tooltip("The number of grid cells per axis.")]
private Vector3Int m_NumCells = new Vector3Int(20, 1, 20);
private void OnNumCellsChange()
{
Bounds b = m_EditorBounds;
// Number of cells we can fit into bounds.
Vector3Int n = new Vector3Int(
Mathf.RoundToInt(b.size.x / m_CellSize),
Mathf.RoundToInt(b.size.y / m_CellSize),
Mathf.RoundToInt(b.size.z / m_CellSize));
if (n != m_NumCells)
{
m_NumCells = Vector3Int.Max(m_NumCells, Vector3Int.one);
m_NumCells.y = 1;
// Resize, fit x/z to cells.
Vector3 size = (Vector3)m_NumCells * m_CellSize;
size.y = b.size.y; // keep height
b.size = size;
}
EditorBounds = b;
}
[SerializeField]
[ReadOnly]
[Foldout("Geometry")]
[Tooltip("Actual detection bounds size of the grid sensor. Values are rounded " +
"to match cell size. Visualized by the blue box in scene view (Gizmos).")]
private Vector3 m_DetectionSize = new Vector3(20, 1, 20);
[SerializeField]
[OnValueChanged("OnBoundsSizeChange")]
[Foldout("Geometry")]
[Tooltip("Unrounded editor bounds, visualized by the white box in scene view (Gizmos)." +
" Drag handles to change size. Key commands in scene GUI: "
+ "\nS - Snap to cell size\nC - Center on X-axis\nShift+C - Center on all axes")]
private Vector3 m_BoundsSize = new Vector3(20, 1, 20);
private void OnBoundsSizeChange()
{
Bounds b = m_EditorBounds;
b.size = new Vector3(
Mathf.Max(m_BoundsSize.x, m_CellSize),
Mathf.Max(m_BoundsSize.y, 1),
Mathf.Max(m_BoundsSize.z, m_CellSize));
EditorBounds = b;
}
[SerializeField]
[OnValueChanged("OnOffsetChange")]
[Foldout("Geometry")]
[Tooltip("Detection offset from sensor transform position.")]
private Vector3 m_Offset;
private void OnOffsetChange()
{
Bounds b = m_EditorBounds;
b.center = m_Offset;
EditorBounds = b;
}
#endregion
// Invoked after setting EditorBounds property.
private void RoundDetectionBoundsSize()
{
Vector3 s = new Vector3(
Mathf.Round(m_EditorBounds.size.x / m_CellSize), 0,
Mathf.Round(m_EditorBounds.size.z / m_CellSize));
s = Vector3.Max(s, Vector3.one);
m_NumCells.x = (int)s.x;
m_NumCells.y = 1;
m_NumCells.z = (int)s.z;
s *= m_CellSize;
s.y = Mathf.Max(m_EditorBounds.size.y, 1);
m_DetectionBounds.size = s;
m_DetectionSize = s;
UpdateGridSize(m_NumCells.x, m_NumCells.z);
}
/// <summary>
/// Invoked by custom editor via key command.
/// </summary>
public void RoundDetectionBoundsCenter()
{
Vector3 c = new Vector3(
Mathf.Round(m_EditorBounds.center.x / m_CellSize),
Mathf.Round(m_EditorBounds.center.y / m_CellSize),
Mathf.Round(m_EditorBounds.center.z / m_CellSize));
m_DetectionBounds.center = c * m_CellSize;
// Saving for Scene GUI Undo.
SaveState(OnEditorBoundsChange, "Grid Sensor Change");
}
/// <summary>
/// Invoked by custom editor via key command.
/// </summary>
public void CenterDetectionBoundsOnAxes(bool xOnly)
{
Vector3 c = Vector3.zero;
c.y = xOnly ? m_DetectionBounds.center.y : c.y;
c.z = xOnly ? m_DetectionBounds.center.z : c.z;
m_DetectionBounds.center = c;
// Saving for Scene GUI Undo.
SaveState(OnEditorBoundsChange, "Grid Sensor Change");
}
/// <summary>
/// Get rotation for the selected <see cref="GameObjectDetector2D.SensorRotationType"/>.
/// </summary>
/// <returns>The current rotation applied to the detector</returns>
public Quaternion GetRotation()
{
return m_RotationType switch
{
GameObjectDetector2D.SensorRotationType.AgentY
=> Quaternion.AngleAxis(transform.eulerAngles.y, Vector3.up),
GameObjectDetector2D.SensorRotationType.AgentXYZ
=> transform.rotation,
_ => m_WorldRotation,
};
}
/// <summary>
/// Invoked by <see cref="GridEditorHelper2D"/>.
/// </summary>
public override void OnEditorInit()
{
base.OnEditorInit();
// Write back serialized value, will save
// initial state and update grid shape info.
EditorBounds = m_EditorBounds;
}
protected override GameObjectDetector CreateDetector()
{
return new GameObjectDetector2D()
{
Result = new DetectionResult(
m_GameObjectSettingsMeta.DetectableTags,
ColliderBufferSize),
Settings = m_GameObjectSettingsMeta,
ColliderBufferSize = ColliderBufferSize,
ClearCacheOnReset = ClearCacheOnReset,
SensorTransform = transform,
SensorOwner = GetComponentInParent<DetectableGameObject>(),
RotationType = RotationType,
WorldRotation = m_WorldRotation,
Constraint = (DetectionConstraint2D)CreateConstraint()
};
}
protected override Encoder CreateEncoder()
{
return new Encoder()
{
Settings = m_GameObjectSettingsMeta,
GridBuffer = GridBuffer
};
}
protected override DetectionConstraint CreateConstraint()
{
return new DetectionConstraint2D()
{
Bounds = DetectionBounds
};
}
}
}