-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameUtils.ts
333 lines (282 loc) · 9.35 KB
/
GameUtils.ts
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
/**
* Game-wide enums and constants. Advanced curve functions and their visualizer
*/
import * as hz from "horizon/core";
export enum GameState {
"ReadyForMatch", // Default, nothing is going on, we can start a match
"StartingMatch", //A match has been started by players
"PlayingMatch", //A match is ongoing
"EndingMatch", //A match is ending
"CompletedMatch", //A match has just been completed
}
export enum PlayerGameStatus {
"Lobby",
"Standby",
"Playing",
}
// Pool Class
export class Pool<T> {
all: T[] = [];
available: T[] = [];
active: T[] = [];
hasAvailable(): boolean {
return this.available.length > 0;
}
hasActive(): boolean {
return this.active.length > 0;
}
isAvailable(t: T): boolean {
return this.available.includes(t);
}
getNextAvailable(): T | null {
if (this.hasAvailable()) {
const available = this.available.shift()!;
if (!this.active.includes(available)) {
this.active.push(available);
}
return available;
} else {
return null;
}
}
getRandomAvailable(): T | null {
if (this.hasAvailable()) {
const rand = Math.floor(Math.random() * this.available.length);
const available = this.available.splice(rand, 1)[0]!;
if (!this.active.includes(available)) {
this.active.push(available);
}
return available;
} else {
return null;
}
}
getRandomActive(): T | null {
if (this.hasActive()) {
const rand = Math.floor(Math.random() * this.active.length);
const active = this.active.splice(rand, 1)[0]!;
return active;
} else {
return null;
}
}
addToPool(t: T): void {
if (!this.all.includes(t)) {
this.all.push(t);
}
if (!this.available.includes(t)) {
this.available.push(t);
}
if (this.active.includes(t)) {
this.active.splice(this.active.indexOf(t), 1);
}
}
removeFromPool(t: T): void {
if (this.active.includes(t)) {
this.active.splice(this.active.indexOf(t), 1);
}
if (this.available.includes(t)) {
this.available.splice(this.available.indexOf(t), 1);
}
if (this.all.includes(t)) {
this.all.splice(this.all.indexOf(t), 1);
}
}
resetAvailability(): void {
this.available = this.all.slice();
}
}
export function msToMinutesAndSeconds(time: number): string {
const baseTime = Math.floor(time);
let minutes = Math.floor(baseTime / 60);
let seconds = baseTime % 60;
let ms = time % 1;
seconds = seconds === 60 ? 0 : seconds;
return `${(minutes < 10 ? '0' : '') + minutes} : ${(seconds < 10 ? '0' : '') + seconds.toFixed(0)} : ${ms.toFixed(2).substring(2)}`;
}
export function timedIntervalActionFunction(
timerMS: number,
component: hz.Component,
onTickAction: (timerMS: number) => void, // Function to be run during the timer tick
onEndAction: () => void // Function to be run at the end of the timer
): number {
let timerID = component.async.setInterval(() => {
if (timerMS > 0) {
onTickAction(timerMS); // Call the onTick function
timerMS -= 1000;
} else {
if (timerID !== undefined) {
onEndAction();
component.async.clearInterval(timerID);
}
}
}, 1000);
return timerID;
}
export class Curve {
//not ideal as the array itself can still be changed
private _controlPoints: hz.Vec3[] = [];
public get controlPoints(): hz.Vec3[] {
return this._controlPoints;
}
private set controlPoints(value: hz.Vec3[]) {
this._controlPoints = value;
}
constructor(controlPoints: hz.Vec3[]) {
this.controlPoints = controlPoints;
}
interpolate(t: number): hz.Vec3 {
const n = this.controlPoints.length - 1;
const index = Math.floor(t * n);
const t0 = index > 0 ? index / n : 0;
const t1 = (index + 1) / n;
//console.log("index:", index);
const p0 = this.controlPoints[Math.max(0, index > 1 ? index - 1 : 0)];
const p0a =
index > 1
? this.controlPoints[index - 1]
: this.controlPoints[0].add(
this.controlPoints[0].sub(this.controlPoints[1])
); //deal with negative index, should project missing control points instead
const p1 = this.controlPoints[index];
const p2 =
this.controlPoints[
Math.min(n, index < n ? index + 1 : this.controlPoints.length - 1)
]; //deal with out of bounds index, should project missing control points instead
const p2a =
index + 1 < n ? this.controlPoints[index + 1] : this.controlPoints[n]; //deal with negative index, should project missing control points instead
const p3 =
this.controlPoints[
Math.min(n, index < n - 1 ? index + 2 : this.controlPoints.length - 1)
];
const p3a =
index + 2 < n ? this.controlPoints[index + 2] : this.controlPoints[n];
/*: this.controlPoints[n].add(
this.controlPoints[n].sub(this.controlPoints[n - 1])
); //deal with negative index, should project missing control points instead*/
const tNormalized = (t - t0) / (t1 - t0);
return this.interpolateCatmullRom(p0a, p1, p2a, p3, tNormalized);
}
//0.0 to 1.0
findClosestPointCurveProgress(target: hz.Vec3): number {
const f = (t: number) => {
const point = this.interpolate(t);
return this.calculateDistance(target, point);
};
const tMin = this.goldenSectionSearch(f, 0, 1, 1e-4); // adjust tolarence value as needed, smaller values increases precision and runtime cost
return tMin;
}
private interpolateCatmullRom(
//uses a Catmull-Rom algorithm for the spline
p0: hz.Vec3,
p1: hz.Vec3,
p2: hz.Vec3,
p3: hz.Vec3,
t: number
): hz.Vec3 {
const t2 = t * t;
const t3 = t2 * t;
const v0 = (p2.x - p0.x) * 0.5;
const v1 = (p3.x - p1.x) * 0.5;
const a = 2 * p1.x - 2 * p2.x + v0 + v1;
const b = -3 * p1.x + 3 * p2.x - 2 * v0 - v1;
const c = v0;
const d = p1.x;
const x = a * t3 + b * t2 + c * t + d;
const v0y = (p2.y - p0.y) * 0.5;
const v1y = (p3.y - p1.y) * 0.5;
const ay = 2 * p1.y - 2 * p2.y + v0y + v1y;
const by = -3 * p1.y + 3 * p2.y - 2 * v0y - v1y;
const cy = v0y;
const dy = p1.y;
const y = ay * t3 + by * t2 + cy * t + dy;
const v0z = (p2.z - p0.z) * 0.5;
const v1z = (p3.z - p1.z) * 0.5;
const az = 2 * p1.z - 2 * p2.z + v0z + v1z;
const bz = -3 * p1.z + 3 * p2.z - 2 * v0z - v1z;
const cz = v0z;
const dz = p1.z;
const z = az * t3 + bz * t2 + cz * t + dz;
return new hz.Vec3(x, y, z);
}
//Golden Section search is statistically a little more efficient than a binary seive when trying to find a number using an over/under check
private goldenSectionSearch(
f: (x: number) => number,
a: number,
b: number,
tol: number
): number {
const gr = 1.6180339887498948482; //Aproximation of phi to avoid the classic (1+sqrt(5))/2 being called thousands of times
let c = b - (b - a) / gr;
let d = a + (b - a) / gr;
while (Math.abs(b - a) > tol) {
if (f(c) < f(d)) {
b = d;
d = c;
c = b - (b - a) / gr;
} else {
a = c;
c = d;
d = a + (b - a) / gr;
}
}
return (b + a) / 2;
}
private calculateDistance(point1: hz.Vec3, point2: hz.Vec3): number {
return point1.sub(point2).magnitudeSquared(); //using squared to avoid unnecessary sqrt call, don't need the actual distance, just the smallest
}
}
export class CurveVisualizer extends hz.Component<typeof CurveVisualizer> {
// define the inputs available in the property panel in the UI as well as default values
static propsDefinition = {
showPath: { type: hz.PropTypes.Boolean },
trailRenderer: { type: hz.PropTypes.Entity },
};
public static SetCurve = new hz.LocalEvent<{ curve: Curve }>("SetCurve");
public static StartDrawingCurve = new hz.LocalEvent("StartDrawingCurve");
public static StopDrawingCurve = new hz.LocalEvent("StopDrawingCurve");
private splineProgress: number = 0;
private curve!: Curve;
private showPath: boolean = false;
preStart() {
this.showPath = this.props.showPath;
this.connectLocalBroadcastEvent(
CurveVisualizer.SetCurve,
(data) => {
this.curve = data.curve;
});
this.connectLocalBroadcastEvent(
CurveVisualizer.StartDrawingCurve,
() => {
this.showPath = true;
this.entity.as(hz.TrailGizmo)!.play();
});
this.connectLocalBroadcastEvent(
CurveVisualizer.StopDrawingCurve,
() => {
this.showPath = false;
this.entity.as(hz.TrailGizmo)!.stop();
});
//For drawing the curve
this.connectLocalBroadcastEvent(
hz.World.onUpdate,
(data) => {
if (this.showPath && this.curve && this.props.trailRenderer) {
this.splineProgress = this.drawTrackWithProgress(
this.props.trailRenderer!,
this.splineProgress,
data.deltaTime,
this.curve);
}
});
}
start() { }
private drawTrackWithProgress(trailRenderer: hz.Entity, splineProgress: number, deltaTime: number, curve: Curve) {
splineProgress = (splineProgress + deltaTime * 0.1) % 1;
// Edit mode visuals
const interpolatedPoint = curve.interpolate(splineProgress);
trailRenderer.position.set(interpolatedPoint); // this currently moves self, might want to split the debug visuals from the script container
return splineProgress;
}
}
hz.Component.register(CurveVisualizer);