-
Notifications
You must be signed in to change notification settings - Fork 0
/
g1.js
2177 lines (1745 loc) · 53.4 KB
/
g1.js
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Timing multiplier for entire game engine.
let gameSpeed = 1;
// Colors
const BLUE = { r: 0x67, g: 0xd7, b: 0xf0 };
const GREEN = { r: 0xa6, g: 0xe0, b: 0x2c };
const PINK = { r: 0xfa, g: 0x24, b: 0x73 };
const ORANGE = { r: 0xfe, g: 0x95, b: 0x22 };
const allColors = [BLUE, GREEN, PINK, ORANGE];
// Gameplay
const getSpawnDelay = () => {
const spawnDelayMax = 1400;
const spawnDelayMin = 550;
const spawnDelay = spawnDelayMax - state.game.cubeCount * 3.1;
return Math.max(spawnDelay, spawnDelayMin);
}
const doubleStrongEnableScore = 2000;
// Number of cubes that must be smashed before activating a feature.
const slowmoThreshold = 10;
const strongThreshold = 25;
const spinnerThreshold = 25;
// Interaction state
let pointerIsDown = false;
// The last known position of the primary pointer in screen coordinates.`
let pointerScreen = { x: 0, y: 0 };
// Same as `pointerScreen`, but converted to scene coordinates in rAF.
let pointerScene = { x: 0, y: 0 };
// Minimum speed of pointer before "hits" are counted.
const minPointerSpeed = 60;
// The hit speed affects the direction the target post-hit. This number dampens that force.
const hitDampening = 0.1;
// Backboard receives shadows and is the farthest negative Z position of entities.
const backboardZ = -400;
const shadowColor = '#262e36';
// How much air drag is applied to standard objects
const airDrag = 0.022;
const gravity = 0.3;
// Spark config
const sparkColor = 'rgba(170,221,255,.9)';
const sparkThickness = 2.2;
const airDragSpark = 0.1;
// Track pointer positions to show trail
const touchTrailColor = 'rgba(170,221,255,.62)';
const touchTrailThickness = 7;
const touchPointLife = 120;
const touchPoints = [];
// Size of in-game targets. This affects rendered size and hit area.
const targetRadius = 40;
const targetHitRadius = 50;
const makeTargetGlueColor = target => {
// const alpha = (target.health - 1) / (target.maxHealth - 1);
// return `rgba(170,221,255,${alpha.toFixed(3)})`;
return 'rgb(170,221,255)';
};
// Size of target fragments
const fragRadius = targetRadius / 3;
// Game canvas element needed in setup.js and interaction.js
const canvas = document.querySelector('#c');
// 3D camera config
// Affects perspective
const cameraDistance = 900;
// Does not affect perspective
const sceneScale = 1;
// Objects that get too close to the camera will be faded out to transparent over this range.
// const cameraFadeStartZ = 0.8*cameraDistance - 6*targetRadius;
const cameraFadeStartZ = 0.45*cameraDistance;
const cameraFadeEndZ = 0.65*cameraDistance;
const cameraFadeRange = cameraFadeEndZ - cameraFadeStartZ;
// Globals used to accumlate all vertices/polygons in each frame
const allVertices = [];
const allPolys = [];
const allShadowVertices = [];
const allShadowPolys = [];
// state.js
// ============================================================================
// ============================================================================
///////////
// Enums //
///////////
// Game Modes
const GAME_MODE_RANKED = Symbol('GAME_MODE_RANKED');
const GAME_MODE_CASUAL = Symbol('GAME_MODE_CASUAL');
// Available Menus
const MENU_MAIN = Symbol('MENU_MAIN');
const MENU_PAUSE = Symbol('MENU_PAUSE');
const MENU_SCORE = Symbol('MENU_SCORE');
//////////////////
// Global State //
//////////////////
const state = {
game: {
mode: GAME_MODE_RANKED,
// Run time of current game.
time: 0,
// Player score.
score: 0,
// Total number of cubes smashed in game.
cubeCount: 0
},
menus: {
// Set to `null` to hide all menus
active: MENU_MAIN
}
};
////////////////////////////
// Global State Selectors //
////////////////////////////
const isInGame = () => !state.menus.active;
const isMenuVisible = () => !!state.menus.active;
const isCasualGame = () => state.game.mode === GAME_MODE_CASUAL;
const isPaused = () => state.menus.active === MENU_PAUSE;
///////////////////
// Local Storage //
///////////////////
const highScoreKey = '__menja__highScore';
const getHighScore = () => {
const raw = localStorage.getItem(highScoreKey);
return raw ? parseInt(raw, 10) : 0;
};
let _lastHighscore = getHighScore();
const setHighScore = score => {
_lastHighscore = getHighScore();
localStorage.setItem(highScoreKey, String(score));
};
const isNewHighScore = () => state.game.score > _lastHighscore;
// utils.js
// ============================================================================
// ============================================================================
const invariant = (condition, message) => {
if (!condition) throw new Error(message);
};
/////////
// DOM //
/////////
const $ = selector => document.querySelector(selector);
const handleClick = (element, handler) => element.addEventListener('click', handler);
const handlePointerDown = (element, handler) => {
element.addEventListener('touchstart', handler);
element.addEventListener('mousedown', handler);
};
////////////////////////
// Formatting Helpers //
////////////////////////
// Converts a number into a formatted string with thousand separators.
const formatNumber = num => num.toLocaleString();
////////////////////
// Math Constants //
////////////////////
const PI = Math.PI;
const TAU = Math.PI * 2;
const ETA = Math.PI * 0.5;
//////////////////
// Math Helpers //
//////////////////
// Clamps a number between min and max values (inclusive)
const clamp = (num, min, max) => Math.min(Math.max(num, min), max);
// Linearly interpolate between numbers a and b by a specific amount.
// mix >= 0 && mix <= 1
const lerp = (a, b, mix) => (b - a) * mix + a;
////////////////////
// Random Helpers //
////////////////////
// Generates a random number between min (inclusive) and max (exclusive)
const random = (min, max) => Math.random() * (max - min) + min;
// Generates a random integer between and possibly including min and max values
const randomInt = (min, max) => ((Math.random() * (max - min + 1)) | 0) + min;
// Returns a random element from an array
const pickOne = arr => arr[Math.random() * arr.length | 0];
///////////////////
// Color Helpers //
///////////////////
// Converts an { r, g, b } color object to a 6-digit hex code.
const colorToHex = color => {
return '#' +
(color.r | 0).toString(16).padStart(2, '0') +
(color.g | 0).toString(16).padStart(2, '0') +
(color.b | 0).toString(16).padStart(2, '0');
};
// Operates on an { r, g, b } color object.
// Returns string hex code.
// `lightness` must range from 0 to 1. 0 is pure black, 1 is pure white.
const shadeColor = (color, lightness) => {
let other, mix;
if (lightness < 0.5) {
other = 0;
mix = 1 - (lightness * 2);
} else {
other = 255;
mix = lightness * 2 - 1;
}
return '#' +
(lerp(color.r, other, mix) | 0).toString(16).padStart(2, '0') +
(lerp(color.g, other, mix) | 0).toString(16).padStart(2, '0') +
(lerp(color.b, other, mix) | 0).toString(16).padStart(2, '0');
};
////////////////////
// Timing Helpers //
////////////////////
const _allCooldowns = [];
const makeCooldown = (rechargeTime, units=1) => {
let timeRemaining = 0;
let lastTime = 0;
const initialOptions = { rechargeTime, units };
const updateTime = () => {
const now = state.game.time;
// Reset time remaining if time goes backwards.
if (now < lastTime) {
timeRemaining = 0;
} else {
// update...
timeRemaining -= now-lastTime;
if (timeRemaining < 0) timeRemaining = 0;
}
lastTime = now;
};
const canUse = () => {
updateTime();
return timeRemaining <= (rechargeTime * (units-1));
};
const cooldown = {
canUse,
useIfAble() {
const usable = canUse();
if (usable) timeRemaining += rechargeTime;
return usable;
},
mutate(options) {
if (options.rechargeTime) {
// Apply recharge time delta so change takes effect immediately.
timeRemaining -= rechargeTime-options.rechargeTime;
if (timeRemaining < 0) timeRemaining = 0;
rechargeTime = options.rechargeTime;
}
if (options.units) units = options.units;
},
reset() {
timeRemaining = 0;
lastTime = 0;
this.mutate(initialOptions);
}
};
_allCooldowns.push(cooldown);
return cooldown;
};
const resetAllCooldowns = () => _allCooldowns.forEach(cooldown => cooldown.reset());
const makeSpawner = ({ chance, cooldownPerSpawn, maxSpawns }) => {
const cooldown = makeCooldown(cooldownPerSpawn, maxSpawns);
return {
shouldSpawn() {
return Math.random() <= chance && cooldown.useIfAble();
},
mutate(options) {
if (options.chance) chance = options.chance;
cooldown.mutate({
rechargeTime: options.cooldownPerSpawn,
units: options.maxSpawns
});
}
};
};
////////////////////
// Vector Helpers //
////////////////////
const normalize = v => {
const mag = Math.hypot(v.x, v.y, v.z);
return {
x: v.x / mag,
y: v.y / mag,
z: v.z / mag
};
}
// Curried math helpers
const add = a => b => a + b;
// Curried vector helpers
const scaleVector = scale => vector => {
vector.x *= scale;
vector.y *= scale;
vector.z *= scale;
};
////////////////
// 3D Helpers //
////////////////
// Clone array and all vertices.
function cloneVertices(vertices) {
return vertices.map(v => ({ x: v.x, y: v.y, z: v.z }));
}
// Copy vertex data from one array into another.
// Arrays must be the same length.
function copyVerticesTo(arr1, arr2) {
const len = arr1.length;
for (let i=0; i<len; i++) {
const v1 = arr1[i];
const v2 = arr2[i];
v2.x = v1.x;
v2.y = v1.y;
v2.z = v1.z;
}
}
// Compute triangle midpoint.
// Mutates `middle` property of given `poly`.
function computeTriMiddle(poly) {
const v = poly.vertices;
poly.middle.x = (v[0].x + v[1].x + v[2].x) / 3;
poly.middle.y = (v[0].y + v[1].y + v[2].y) / 3;
poly.middle.z = (v[0].z + v[1].z + v[2].z) / 3;
}
// Compute quad midpoint.
// Mutates `middle` property of given `poly`.
function computeQuadMiddle(poly) {
const v = poly.vertices;
poly.middle.x = (v[0].x + v[1].x + v[2].x + v[3].x) / 4;
poly.middle.y = (v[0].y + v[1].y + v[2].y + v[3].y) / 4;
poly.middle.z = (v[0].z + v[1].z + v[2].z + v[3].z) / 4;
}
function computePolyMiddle(poly) {
if (poly.vertices.length === 3) {
computeTriMiddle(poly);
} else {
computeQuadMiddle(poly);
}
}
// Compute distance from any polygon (tri or quad) midpoint to camera.
// Sets `depth` property of given `poly`.
// Also triggers midpoint calculation, which mutates `middle` property of `poly`.
function computePolyDepth(poly) {
computePolyMiddle(poly);
const dX = poly.middle.x;
const dY = poly.middle.y;
const dZ = poly.middle.z - cameraDistance;
poly.depth = Math.hypot(dX, dY, dZ);
}
// Compute normal of any polygon. Uses normalized vector cross product.
// Mutates `normalName` property of given `poly`.
function computePolyNormal(poly, normalName) {
// Store quick refs to vertices
const v1 = poly.vertices[0];
const v2 = poly.vertices[1];
const v3 = poly.vertices[2];
// Calculate difference of vertices, following winding order.
const ax = v1.x - v2.x;
const ay = v1.y - v2.y;
const az = v1.z - v2.z;
const bx = v1.x - v3.x;
const by = v1.y - v3.y;
const bz = v1.z - v3.z;
// Cross product
const nx = ay*bz - az*by;
const ny = az*bx - ax*bz;
const nz = ax*by - ay*bx;
// Compute magnitude of normal and normalize
const mag = Math.hypot(nx, ny, nz);
const polyNormal = poly[normalName];
polyNormal.x = nx / mag;
polyNormal.y = ny / mag;
polyNormal.z = nz / mag;
}
// Apply translation/rotation/scale to all given vertices.
// If `vertices` and `target` are the same array, the vertices will be mutated in place.
// If `vertices` and `target` are different arrays, `vertices` will not be touched, instead the
// transformed values from `vertices` will be written to `target` array.
function transformVertices(vertices, target, tX, tY, tZ, rX, rY, rZ, sX, sY, sZ) {
// Matrix multiplcation constants only need calculated once for all vertices.
const sinX = Math.sin(rX);
const cosX = Math.cos(rX);
const sinY = Math.sin(rY);
const cosY = Math.cos(rY);
const sinZ = Math.sin(rZ);
const cosZ = Math.cos(rZ);
// Using forEach() like map(), but with a (recycled) target array.
vertices.forEach((v, i) => {
const targetVertex = target[i];
// X axis rotation
const x1 = v.x;
const y1 = v.z*sinX + v.y*cosX;
const z1 = v.z*cosX - v.y*sinX;
// Y axis rotation
const x2 = x1*cosY - z1*sinY;
const y2 = y1;
const z2 = x1*sinY + z1*cosY;
// Z axis rotation
const x3 = x2*cosZ - y2*sinZ;
const y3 = x2*sinZ + y2*cosZ;
const z3 = z2;
// Scale, Translate, and set the transform.
targetVertex.x = x3 * sX + tX;
targetVertex.y = y3 * sY + tY;
targetVertex.z = z3 * sZ + tZ;
});
}
// 3D projection on a single vertex.
// Directly mutates the vertex.
const projectVertex = v => {
const focalLength = cameraDistance * sceneScale;
const depth = focalLength / (cameraDistance - v.z);
v.x = v.x * depth;
v.y = v.y * depth;
};
// 3D projection on a single vertex.
// Mutates a secondary target vertex.
const projectVertexTo = (v, target) => {
const focalLength = cameraDistance * sceneScale;
const depth = focalLength / (cameraDistance - v.z);
target.x = v.x * depth;
target.y = v.y * depth;
};
// PERF.js
// ============================================================================
// ============================================================================
// Dummy no-op functions.
// I use these in a special build for custom performance profiling.
const PERF_START = () => {};
const PERF_END = () => {};
const PERF_UPDATE = () => {};
// 3dModels.js
// ============================================================================
// ============================================================================
// Define models once. The origin is the center of the model.
// A simple cube, 8 vertices, 6 quads.
// Defaults to an edge length of 2 units, can be influenced with `scale`.
function makeCubeModel({ scale=1 }) {
return {
vertices: [
// top
{ x: -scale, y: -scale, z: scale },
{ x: scale, y: -scale, z: scale },
{ x: scale, y: scale, z: scale },
{ x: -scale, y: scale, z: scale },
// bottom
{ x: -scale, y: -scale, z: -scale },
{ x: scale, y: -scale, z: -scale },
{ x: scale, y: scale, z: -scale },
{ x: -scale, y: scale, z: -scale }
],
polys: [
// z = 1
{ vIndexes: [0, 1, 2, 3] },
// z = -1
{ vIndexes: [7, 6, 5, 4] },
// y = 1
{ vIndexes: [3, 2, 6, 7] },
// y = -1
{ vIndexes: [4, 5, 1, 0] },
// x = 1
{ vIndexes: [5, 6, 2, 1] },
// x = -1
{ vIndexes: [0, 3, 7, 4] }
]
};
}
// Not very optimized - lots of duplicate vertices are generated.
function makeRecursiveCubeModel({ recursionLevel, splitFn, color, scale=1 }) {
const getScaleAtLevel = level => 1 / (3 ** level);
// We can model level 0 manually. It's just a single, centered, cube.
let cubeOrigins = [{ x: 0, y: 0, z: 0 }];
// Recursively replace cubes with smaller cubes.
for (let i=1; i<=recursionLevel; i++) {
const scale = getScaleAtLevel(i) * 2;
const cubeOrigins2 = [];
cubeOrigins.forEach(origin => {
cubeOrigins2.push(...splitFn(origin, scale));
});
cubeOrigins = cubeOrigins2;
}
const finalModel = { vertices: [], polys: [] };
// Generate single cube model and scale it.
const cubeModel = makeCubeModel({ scale: 1 });
cubeModel.vertices.forEach(scaleVector(getScaleAtLevel(recursionLevel)));
// Compute the max distance x, y, or z origin values will be.
// Same result as `Math.max(...cubeOrigins.map(o => o.x))`, but much faster.
const maxComponent = getScaleAtLevel(recursionLevel) * (3 ** recursionLevel - 1);
// Place cube geometry at each origin.
cubeOrigins.forEach((origin, cubeIndex) => {
// To compute occlusion (shading), find origin component with greatest
// magnitude and normalize it relative to `maxComponent`.
const occlusion = Math.max(
Math.abs(origin.x),
Math.abs(origin.y),
Math.abs(origin.z)
) / maxComponent;
// At lower iterations, occlusion looks better lightened up a bit.
const occlusionLighter = recursionLevel > 2
? occlusion
: (occlusion + 0.8) / 1.8;
// Clone, translate vertices to origin, and apply scale
finalModel.vertices.push(
...cubeModel.vertices.map(v => ({
x: (v.x + origin.x) * scale,
y: (v.y + origin.y) * scale,
z: (v.z + origin.z) * scale
}))
);
// Clone polys, shift referenced vertex indexes, and compute color.
finalModel.polys.push(
...cubeModel.polys.map(poly => ({
vIndexes: poly.vIndexes.map(add(cubeIndex * 8))
}))
);
});
return finalModel;
}
// o: Vector3D - Position of cube's origin (center).
// s: Vector3D - Determines size of menger sponge.
function mengerSpongeSplit(o, s) {
return [
// Top
{ x: o.x + s, y: o.y - s, z: o.z + s },
{ x: o.x + s, y: o.y - s, z: o.z + 0 },
{ x: o.x + s, y: o.y - s, z: o.z - s },
{ x: o.x + 0, y: o.y - s, z: o.z + s },
{ x: o.x + 0, y: o.y - s, z: o.z - s },
{ x: o.x - s, y: o.y - s, z: o.z + s },
{ x: o.x - s, y: o.y - s, z: o.z + 0 },
{ x: o.x - s, y: o.y - s, z: o.z - s },
// Bottom
{ x: o.x + s, y: o.y + s, z: o.z + s },
{ x: o.x + s, y: o.y + s, z: o.z + 0 },
{ x: o.x + s, y: o.y + s, z: o.z - s },
{ x: o.x + 0, y: o.y + s, z: o.z + s },
{ x: o.x + 0, y: o.y + s, z: o.z - s },
{ x: o.x - s, y: o.y + s, z: o.z + s },
{ x: o.x - s, y: o.y + s, z: o.z + 0 },
{ x: o.x - s, y: o.y + s, z: o.z - s },
// Middle
{ x: o.x + s, y: o.y + 0, z: o.z + s },
{ x: o.x + s, y: o.y + 0, z: o.z - s },
{ x: o.x - s, y: o.y + 0, z: o.z + s },
{ x: o.x - s, y: o.y + 0, z: o.z - s }
];
}
// Helper to optimize models by merging duplicate vertices within a threshold,
// and removing all polys that share the same vertices.
// Directly mutates the model.
function optimizeModel(model, threshold=0.0001) {
const { vertices, polys } = model;
const compareVertices = (v1, v2) => (
Math.abs(v1.x - v2.x) < threshold &&
Math.abs(v1.y - v2.y) < threshold &&
Math.abs(v1.z - v2.z) < threshold
);
const comparePolys = (p1, p2) => {
const v1 = p1.vIndexes;
const v2 = p2.vIndexes;
return (
(
v1[0] === v2[0] ||
v1[0] === v2[1] ||
v1[0] === v2[2] ||
v1[0] === v2[3]
) && (
v1[1] === v2[0] ||
v1[1] === v2[1] ||
v1[1] === v2[2] ||
v1[1] === v2[3]
) && (
v1[2] === v2[0] ||
v1[2] === v2[1] ||
v1[2] === v2[2] ||
v1[2] === v2[3]
) && (
v1[3] === v2[0] ||
v1[3] === v2[1] ||
v1[3] === v2[2] ||
v1[3] === v2[3]
)
);
};
vertices.forEach((v, i) => {
v.originalIndexes = [i];
});
for (let i=vertices.length-1; i>=0; i--) {
for (let ii=i-1; ii>=0; ii--) {
const v1 = vertices[i];
const v2 = vertices[ii];
if (compareVertices(v1, v2)) {
vertices.splice(i, 1);
v2.originalIndexes.push(...v1.originalIndexes);
break;
}
}
}
vertices.forEach((v, i) => {
polys.forEach(p => {
p.vIndexes.forEach((vi, ii, arr) => {
const vo = v.originalIndexes;
if (vo.includes(vi)) {
arr[ii] = i;
}
});
});
});
polys.forEach(p => {
const vi = p.vIndexes;
p.sum = vi[0] + vi[1] + vi[2] + vi[3];
});
polys.sort((a, b) => b.sum - a.sum);
// Assumptions:
// 1. Each poly will either have no duplicates or 1 duplicate.
// 2. If two polys are equal, they are both hidden (two cubes touching),
// therefore both can be removed.
for (let i=polys.length-1; i>=0; i--) {
for (let ii=i-1; ii>=0; ii--) {
const p1 = polys[i];
const p2 = polys[ii];
if (p1.sum !== p2.sum) break;
if (comparePolys(p1, p2)) {
polys.splice(i, 1);
polys.splice(ii, 1);
i--;
break;
}
}
}
return model;
}
// Entity.js
// ============================================================================
// ============================================================================
class Entity {
constructor({ model, color, wireframe=false }) {
const vertices = cloneVertices(model.vertices);
const shadowVertices = cloneVertices(model.vertices);
const colorHex = colorToHex(color);
const darkColorHex = shadeColor(color, 0.4);
const polys = model.polys.map(p => ({
vertices: p.vIndexes.map(vIndex => vertices[vIndex]),
color: color, // custom rgb color object
wireframe: wireframe,
strokeWidth: wireframe ? 2 : 0, // Set to non-zero value to draw stroke
strokeColor: colorHex, // must be a CSS color string
strokeColorDark: darkColorHex, // must be a CSS color string
depth: 0,
middle: { x: 0, y: 0, z: 0 },
normalWorld: { x: 0, y: 0, z: 0 },
normalCamera: { x: 0, y: 0, z: 0 }
}));
const shadowPolys = model.polys.map(p => ({
vertices: p.vIndexes.map(vIndex => shadowVertices[vIndex]),
wireframe: wireframe,
normalWorld: { x: 0, y: 0, z: 0 }
}));
this.projected = {}; // Will store 2D projected data
this.model = model;
this.vertices = vertices;
this.polys = polys;
this.shadowVertices = shadowVertices;
this.shadowPolys = shadowPolys;
this.reset();
}
// Better names: resetEntity, resetTransform, resetEntityTransform
reset() {
this.x = 0;
this.y = 0;
this.z = 0;
this.xD = 0;
this.yD = 0;
this.zD = 0;
this.rotateX = 0;
this.rotateY = 0;
this.rotateZ = 0;
this.rotateXD = 0;
this.rotateYD = 0;
this.rotateZD = 0;
this.scaleX = 1;
this.scaleY = 1;
this.scaleZ = 1;
this.projected.x = 0;
this.projected.y = 0;
}
transform() {
transformVertices(
this.model.vertices,
this.vertices,
this.x,
this.y,
this.z,
this.rotateX,
this.rotateY,
this.rotateZ,
this.scaleX,
this.scaleY,
this.scaleZ
);
copyVerticesTo(this.vertices, this.shadowVertices);
}
// Projects origin point, stored as `projected` property.
project() {
projectVertexTo(this, this.projected);
}
}
// getTarget.js
// ============================================================================
// ============================================================================
// All active targets
const targets = [];
// Pool target instances by color, using a Map.
// keys are color objects, and values are arrays of targets.
// Also pool wireframe instances separately.
const targetPool = new Map(allColors.map(c=>([c, []])));
const targetWireframePool = new Map(allColors.map(c=>([c, []])));
const getTarget = (() => {
const slowmoSpawner = makeSpawner({
chance: 0.5,
cooldownPerSpawn: 10000,
maxSpawns: 1
});
let doubleStrong = false;
const strongSpawner = makeSpawner({
chance: 0.3,
cooldownPerSpawn: 12000,
maxSpawns: 1
});
const spinnerSpawner = makeSpawner({
chance: 0.1,
cooldownPerSpawn: 10000,
maxSpawns: 1
});
// Cached array instances, no need to allocate every time.
const axisOptions = [
['x', 'y'],
['y', 'z'],
['z', 'x']
];
function getTargetOfStyle(color, wireframe) {
const pool = wireframe ? targetWireframePool : targetPool;
let target = pool.get(color).pop();
if (!target) {
target = new Entity({
model: optimizeModel(makeRecursiveCubeModel({
recursionLevel: 1,
splitFn: mengerSpongeSplit,
scale: targetRadius
})),
color: color,
wireframe: wireframe
});
// Init any properties that will be used.
// These will not be automatically reset when recycled.
target.color = color;
target.wireframe = wireframe;
// Some properties don't have their final value yet.
// Initialize with any value of the right type.
target.hit = false;
target.maxHealth = 0;
target.health = 0;
}
return target;
}
return function getTarget() {
if (doubleStrong && state.game.score <= doubleStrongEnableScore) {
doubleStrong = false;
// Spawner is reset automatically when game resets.
} else if (!doubleStrong && state.game.score > doubleStrongEnableScore) {
doubleStrong = true;
strongSpawner.mutate({ maxSpawns: 2 });
}
// Target Parameters
// --------------------------------
let color = pickOne([BLUE, GREEN, ORANGE]);
let wireframe = false;
let health = 1;
let maxHealth = 3;
const spinner = state.game.cubeCount >= spinnerThreshold && isInGame() && spinnerSpawner.shouldSpawn();
// Target Parameter Overrides
// --------------------------------
if (state.game.cubeCount >= slowmoThreshold && slowmoSpawner.shouldSpawn()) {
color = BLUE;
wireframe = true;
}
else if (state.game.cubeCount >= strongThreshold && strongSpawner.shouldSpawn()) {
color = PINK;
health = 3;
}
// Target Creation
// --------------------------------
const target = getTargetOfStyle(color, wireframe);
target.hit = false;
target.maxHealth = maxHealth;
target.health = health;
updateTargetHealth(target, 0);
const spinSpeeds = [
Math.random() * 0.1 - 0.05,
Math.random() * 0.1 - 0.05
];
if (spinner) {
// Ends up spinning a random axis
spinSpeeds[0] = -0.25;
spinSpeeds[1] = 0;
target.rotateZ = random(0, TAU);
}
const axes = pickOne(axisOptions);
spinSpeeds.forEach((spinSpeed, i) => {
switch (axes[i]) {
case 'x':
target.rotateXD = spinSpeed;
break;
case 'y':
target.rotateYD = spinSpeed;
break;
case 'z':
target.rotateZD = spinSpeed;
break;
}
});
return target;
}
})();
const updateTargetHealth = (target, healthDelta) => {
target.health += healthDelta;
// Only update stroke on non-wireframe targets.
// Showing "glue" is a temporary attempt to display health. For now, there's
// no reason to have wireframe targets with high health, so we're fine.
if (!target.wireframe) {
const strokeWidth = target.health - 1;
const strokeColor = makeTargetGlueColor(target);
for (let p of target.polys) {
p.strokeWidth = strokeWidth;
p.strokeColor = strokeColor;
}
}
};