-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathTeleportUtilsShould.cs
More file actions
396 lines (323 loc) · 15.7 KB
/
Copy pathTeleportUtilsShould.cs
File metadata and controls
396 lines (323 loc) · 15.7 KB
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
using DCL.Ipfs;
using ECS.SceneLifeCycle;
using NUnit.Framework;
using System.Collections.Generic;
using UnityEngine;
using Utility;
namespace DCL.SceneLifeCycle.Tests
{
public class TeleportUtilsShould
{
private const int PARCEL = ParcelMathHelper.PARCEL_SIZE;
private const float EPSILON = 0.001f;
[Test]
public void ClampOutOfBoundsRangeToParcelOnSingleParcelScene()
{
var baseParcel = new Vector2Int(17, 59);
SceneEntityDefinition sceneDef = BuildSceneDef(
baseParcel,
new[] { baseParcel },
MakeSpawnPoint(
xRange: new[] { -117f, 71f },
yRange: new[] { 0f, 0f },
zRange: new[] { -23f, 165f },
cameraTarget: new Vector3(8f, 1f, 8f),
isDefault: true));
float minWorldX = baseParcel.x * PARCEL;
float maxWorldX = (baseParcel.x + 1) * PARCEL;
float minWorldZ = baseParcel.y * PARCEL;
float maxWorldZ = (baseParcel.y + 1) * PARCEL;
for (var i = 0; i < 200; i++)
{
(Vector3 worldPos, Vector3? cameraTarget) = TeleportUtils.PickTargetWithOffset(sceneDef, baseParcel);
Assert.GreaterOrEqual(worldPos.x, minWorldX);
Assert.LessOrEqual(worldPos.x, maxWorldX + EPSILON);
Assert.GreaterOrEqual(worldPos.z, minWorldZ);
Assert.LessOrEqual(worldPos.z, maxWorldZ + EPSILON);
Assert.GreaterOrEqual(worldPos.y, 0f);
Assert.NotNull(cameraTarget);
Assert.AreEqual(minWorldX + 8f, cameraTarget!.Value.x, EPSILON);
Assert.AreEqual(1f, cameraTarget.Value.y, EPSILON);
Assert.AreEqual(minWorldZ + 8f, cameraTarget.Value.z, EPSILON);
}
}
[Test]
public void ClampOutOfBoundsSingleValueToParcelEdge()
{
var baseParcel = new Vector2Int(17, 59);
SceneEntityDefinition sceneDef = BuildSceneDef(
baseParcel,
new[] { baseParcel },
MakeSpawnPoint(
xSingle: -50f,
ySingle: 0f,
zSingle: 8f,
isDefault: true));
(Vector3 worldPos, _) = TeleportUtils.PickTargetWithOffset(sceneDef, baseParcel);
Assert.AreEqual(baseParcel.x * PARCEL, worldPos.x, EPSILON);
Assert.AreEqual((baseParcel.y * PARCEL) + 8f, worldPos.z, EPSILON);
}
[Test]
public void NotClampWhenRangeIsInsideMultiParcelScene()
{
var baseParcel = new Vector2Int(0, 0);
var parcels = new[] { new Vector2Int(0, 0), new Vector2Int(1, 0) };
SceneEntityDefinition sceneDef = BuildSceneDef(
baseParcel,
parcels,
MakeSpawnPoint(
xRange: new[] { 4f, 20f },
ySingle: 0f,
zSingle: 8f,
isDefault: true));
for (var i = 0; i < 100; i++)
{
(Vector3 worldPos, _) = TeleportUtils.PickTargetWithOffset(sceneDef, baseParcel);
Assert.GreaterOrEqual(worldPos.x, 4f);
Assert.LessOrEqual(worldPos.x, 20f + EPSILON);
Assert.AreEqual(8f, worldPos.z, EPSILON);
}
}
[Test]
public void ClampNegativeYToZero()
{
var baseParcel = new Vector2Int(0, 0);
SceneEntityDefinition sceneDef = BuildSceneDef(
baseParcel,
new[] { baseParcel },
MakeSpawnPoint(
xSingle: 8f,
yRange: new[] { -50f, -10f },
zSingle: 8f,
isDefault: true));
(Vector3 worldPos, _) = TeleportUtils.PickTargetWithOffset(sceneDef, baseParcel);
Assert.AreEqual(0f, worldPos.y, EPSILON);
}
[Test]
public void PickNamedSpawnPointOverDefault()
{
var baseParcel = new Vector2Int(0, 0);
SceneEntityDefinition sceneDef = BuildSceneDef(
baseParcel,
new[] { baseParcel },
MakeSpawnPoint(xSingle: 2f, ySingle: 0f, zSingle: 2f, isDefault: true, name: "main"),
MakeSpawnPoint(xSingle: 8f, ySingle: 0f, zSingle: 8f, name: "lobby"));
(Vector3 worldPos, _) = TeleportUtils.PickTargetWithOffset(sceneDef, baseParcel, "lobby");
Assert.AreEqual(8f, worldPos.x, EPSILON);
Assert.AreEqual(8f, worldPos.z, EPSILON);
}
[Test]
public void MatchSpawnPointNameCaseInsensitively()
{
var baseParcel = new Vector2Int(0, 0);
SceneEntityDefinition sceneDef = BuildSceneDef(
baseParcel,
new[] { baseParcel },
MakeSpawnPoint(xSingle: 2f, ySingle: 0f, zSingle: 2f, isDefault: true, name: "main"),
MakeSpawnPoint(xSingle: 8f, ySingle: 0f, zSingle: 8f, name: "Lobby"));
(Vector3 worldPos, _) = TeleportUtils.PickTargetWithOffset(sceneDef, baseParcel, "lOBBY");
Assert.AreEqual(8f, worldPos.x, EPSILON);
Assert.AreEqual(8f, worldPos.z, EPSILON);
}
[Test]
public void FallBackToDefaultSelectionWhenNameNotMatched()
{
var baseParcel = new Vector2Int(0, 0);
SceneEntityDefinition sceneDef = BuildSceneDef(
baseParcel,
new[] { baseParcel },
MakeSpawnPoint(xSingle: 2f, ySingle: 0f, zSingle: 2f, isDefault: true, name: "main"),
MakeSpawnPoint(xSingle: 8f, ySingle: 0f, zSingle: 8f, name: "lobby"));
(Vector3 worldPos, _) = TeleportUtils.PickTargetWithOffset(sceneDef, baseParcel, "missing");
Assert.AreEqual(2f, worldPos.x, EPSILON);
Assert.AreEqual(2f, worldPos.z, EPSILON);
}
[Test]
public void PickFirstSpawnPointWhenNamesDuplicate()
{
var baseParcel = new Vector2Int(0, 0);
SceneEntityDefinition sceneDef = BuildSceneDef(
baseParcel,
new[] { baseParcel },
MakeSpawnPoint(xSingle: 2f, ySingle: 0f, zSingle: 2f, isDefault: true, name: "main"),
MakeSpawnPoint(xSingle: 8f, ySingle: 0f, zSingle: 8f, name: "lobby"),
MakeSpawnPoint(xSingle: 12f, ySingle: 0f, zSingle: 12f, name: "lobby"));
(Vector3 worldPos, _) = TeleportUtils.PickTargetWithOffset(sceneDef, baseParcel, "lobby");
Assert.AreEqual(8f, worldPos.x, EPSILON);
Assert.AreEqual(8f, worldPos.z, EPSILON);
}
[Test]
public void ApplyNamedSpawnPointCameraTarget()
{
var baseParcel = new Vector2Int(0, 0);
SceneEntityDefinition sceneDef = BuildSceneDef(
baseParcel,
new[] { baseParcel },
MakeSpawnPoint(xSingle: 2f, ySingle: 0f, zSingle: 2f, isDefault: true, name: "main"),
MakeSpawnPoint(xSingle: 8f, ySingle: 0f, zSingle: 8f, name: "lobby", cameraTarget: new Vector3(10f, 1f, 10f)));
(_, Vector3? cameraTarget) = TeleportUtils.PickTargetWithOffset(sceneDef, baseParcel, "lobby");
Assert.NotNull(cameraTarget);
Assert.AreEqual(10f, cameraTarget!.Value.x, EPSILON);
Assert.AreEqual(1f, cameraTarget.Value.y, EPSILON);
Assert.AreEqual(10f, cameraTarget.Value.z, EPSILON);
}
[Test]
public void AnchorNamedSpawnPointToSceneBaseNotTargetParcel()
{
var baseParcel = new Vector2Int(0, 0);
var parcels = new[] { new Vector2Int(0, 0), new Vector2Int(1, 0) };
SceneEntityDefinition sceneDef = BuildSceneDef(
baseParcel,
parcels,
MakeSpawnPoint(xSingle: 2f, ySingle: 0f, zSingle: 2f, isDefault: true, name: "main"),
MakeSpawnPoint(xSingle: 20f, ySingle: 0f, zSingle: 8f, name: "lobby"));
(Vector3 worldPos, _) = TeleportUtils.PickTargetWithOffset(sceneDef, new Vector2Int(1, 0), "lobby");
Assert.AreEqual(20f, worldPos.x, EPSILON);
Assert.AreEqual(8f, worldPos.z, EPSILON);
}
[Test]
public void ScatterWithinNamedRangeRegardlessOfTargetParcel()
{
var baseParcel = new Vector2Int(0, 0);
var parcels = new[] { new Vector2Int(0, 0), new Vector2Int(1, 0), new Vector2Int(2, 0) };
SceneEntityDefinition sceneDef = BuildSceneDef(
baseParcel,
parcels,
MakeSpawnPoint(xSingle: 2f, ySingle: 0f, zSingle: 2f, isDefault: true, name: "main"),
MakeSpawnPoint(xRange: new[] { 26f, 38f }, ySingle: 0f, zSingle: 8f, name: "scatter"));
var distinctLandings = new HashSet<float>();
for (var i = 0; i < 100; i++)
{
(Vector3 worldPos, _) = TeleportUtils.PickTargetWithOffset(sceneDef, new Vector2Int(2, 0), "scatter");
Assert.GreaterOrEqual(worldPos.x, 26f);
Assert.LessOrEqual(worldPos.x, 38f + EPSILON);
Assert.AreEqual(8f, worldPos.z, EPSILON);
distinctLandings.Add(worldPos.x);
}
Assert.Greater(distinctLandings.Count, 1, "Players must scatter within the range, not stack on one point");
}
/// <summary>
/// The "BBQ Sauce Recipe" event at -148,141: the event parcel is the very parcel holding the
/// scene's spawn point, so a teleport aimed at it must land on that spawn point instead of the
/// parcel centre, where the scene's centrepiece asset stands.
/// </summary>
[Test]
public void PickTheSpawnPointStandingInTheRequestedParcel()
{
var baseParcel = new Vector2Int(-148, 141);
SceneEntityDefinition sceneDef = BuildSceneDef(
baseParcel,
new[] { new Vector2Int(-148, 142), new Vector2Int(-147, 142), baseParcel, new Vector2Int(-147, 141) },
MakeSpawnPoint(
xRange: new[] { 0f, 3f },
yRange: new[] { 0f, 0f },
zRange: new[] { 0f, 3f },
cameraTarget: new Vector3(8f, 1f, 8f),
isDefault: true,
name: "SpawnArea1"));
Assert.That(TeleportUtils.TryPickSpawnPointNameInParcel(sceneDef, baseParcel, out string spawnPointName), Is.True);
Assert.That(spawnPointName, Is.EqualTo("SpawnArea1"));
}
/// <summary>
/// The original land-on-parcel motivation: an event at a parcel that holds no spawn point of its
/// own (e.g. the Theatre at 0,5 inside Genesis Plaza) must keep landing on that parcel.
/// </summary>
[Test]
public void PickNoSpawnPointForParcelThatHoldsNone()
{
var baseParcel = new Vector2Int(0, 0);
var farParcel = new Vector2Int(0, 5);
var parcels = new List<Vector2Int>();
for (int y = baseParcel.y; y <= farParcel.y; y++)
parcels.Add(new Vector2Int(0, y));
SceneEntityDefinition sceneDef = BuildSceneDef(
baseParcel,
parcels,
MakeSpawnPoint(xSingle: 2f, ySingle: 0f, zSingle: 2f, isDefault: true, name: "main"));
Assert.That(TeleportUtils.TryPickSpawnPointNameInParcel(sceneDef, farParcel, out _), Is.False);
Assert.That(TeleportUtils.TryPickSpawnPointNameInParcel(sceneDef, baseParcel, out _), Is.True);
}
/// <summary>
/// A spawn point designated for the requested parcel wins over a default standing elsewhere: the
/// request names a parcel, and honouring the default instead would drop the player even further
/// from the spot he asked for.
/// </summary>
[Test]
public void PreferTheSpawnPointInTheParcelOverACloserDefault()
{
var baseParcel = new Vector2Int(0, 0);
var farParcel = new Vector2Int(0, 3);
SceneEntityDefinition sceneDef = BuildSceneDef(
baseParcel,
new[] { baseParcel, new Vector2Int(0, 1), new Vector2Int(0, 2), farParcel },
MakeSpawnPoint(xSingle: 2f, ySingle: 0f, zSingle: 2f, isDefault: true, name: "entrance"),
MakeSpawnPoint(xSingle: 8f, ySingle: 0f, zSingle: 50f, name: "stage"));
Assert.That(TeleportUtils.TryPickSpawnPointNameInParcel(sceneDef, farParcel, out string spawnPointName), Is.True);
Assert.That(spawnPointName, Is.EqualTo("stage"));
}
/// <summary>
/// A nameless spawn point cannot be addressed through <see cref="TeleportUtils.PickTargetWithOffset" />,
/// so it must not suppress the land-on-parcel fallback.
/// </summary>
[Test]
public void PickNoSpawnPointWhenTheOneInTheParcelIsNameless()
{
var baseParcel = new Vector2Int(0, 0);
SceneEntityDefinition sceneDef = BuildSceneDef(
baseParcel,
new[] { baseParcel },
MakeSpawnPoint(xSingle: 2f, ySingle: 0f, zSingle: 2f, isDefault: true, name: ""));
Assert.That(TeleportUtils.TryPickSpawnPointNameInParcel(sceneDef, baseParcel, out _), Is.False);
}
private static SceneEntityDefinition BuildSceneDef(Vector2Int baseParcel, IReadOnlyList<Vector2Int> parcels, params SceneMetadata.SpawnPoint[] spawnPoints)
{
var sceneSection = new SceneMetadataScene
{
DecodedBase = baseParcel,
DecodedParcels = parcels,
};
var metadata = new SceneMetadata
{
scene = sceneSection,
spawnPoints = new List<SceneMetadata.SpawnPoint>(spawnPoints),
};
return new SceneEntityDefinition("test-scene", metadata);
}
private static SceneMetadata.SpawnPoint MakeSpawnPoint(
float[]? xRange = null, float[]? yRange = null, float[]? zRange = null,
float? xSingle = null, float? ySingle = null, float? zSingle = null,
Vector3? cameraTarget = null, bool isDefault = false, string name = "TestSpawn")
{
var sp = new SceneMetadata.SpawnPoint
{
name = name,
@default = isDefault,
position = new SceneMetadata.SpawnPoint.Position
{
x = MakeCoordinate(xRange, xSingle),
y = MakeCoordinate(yRange, ySingle),
z = MakeCoordinate(zRange, zSingle),
},
};
if (cameraTarget.HasValue)
{
Vector3 target = cameraTarget.Value;
sp.cameraTarget = new SceneMetadata.SpawnPoint.Position
{
x = new SceneMetadata.SpawnPoint.Coordinate { SingleValue = target.x },
y = new SceneMetadata.SpawnPoint.Coordinate { SingleValue = target.y },
z = new SceneMetadata.SpawnPoint.Coordinate { SingleValue = target.z },
};
}
return sp;
}
private static SceneMetadata.SpawnPoint.Coordinate MakeCoordinate(float[]? range, float? single)
{
if (range != null)
return new SceneMetadata.SpawnPoint.Coordinate { MultiValue = range };
if (single.HasValue)
return new SceneMetadata.SpawnPoint.Coordinate { SingleValue = single.Value };
return default(SceneMetadata.SpawnPoint.Coordinate);
}
}
}