-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.ts
1192 lines (1046 loc) · 25.9 KB
/
game.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
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
import { WebEngineAPI, webEngine } from "sprig/web";
import {
fbNew,
fbGetRender,
fbClearColor,
fbRender,
mat4Identity,
mat4Translate,
mat4Scale,
mat4Rotate,
Vec3,
Framebuffer,
Color,
vecMulScalar,
vecDistance,
vecSubVec,
vecAddVec,
vecNormalize,
vec3CrossProduct,
fbClearDepth,
vecLength,
vecDot,
RenderPass,
} from "./graphics";
import { verticesBobPerson, verticesCube } from "./models";
import {
soundPickUp,
soundExplosion,
soundPew,
soundDeath,
soundEnemyAttack,
soundStageUp,
soundSettingChange,
soundMenuMusic,
} from "./sounds";
//
// ____ _____ _____
// /\ _`\ /\ __`\/\ __`\ /'\_/`\
// \ \ \L\ \ \ \/\ \ \ \/\ \/\ \
// \ \ , /\ \ \ \ \ \ \ \ \ \ \__\ \
// \ \ \\ \\ \ \_\ \ \ \_\ \ \ \_/\ \
// \ \_\ \_\ \_____\ \_____\ \_\\ \_\
// \/_/\/ /\/_____/\/_____/\/_/ \/_/
//
// This is ROOM, a DOOM inspired 3D [pew pew]-er, built for the sprig.
//
// / w [oooo] i \
// \ a s d [oooo] j k l /
//
// w = Forward i = Pew Pew
// s = Go Left j = Look Left
// d = Go Right l = Look Right
// a = Backward k = Look Behind
//
// Trapped in a claustrophobic room, you must survive for as long as possible.
// Your health slowly drains, so you must run around and collect medkits.
// Be sure to avoid the spin-y people.
// They are not your friend.
//
// Hey, why does this code look weird?
// Well, ROOM was written in typescript and compiled into javascript.
// The original source is at https://github.com/davnotdev/Room.
//
// Want to edit the game?
// Go ahead!
// Game logic is in the game.ts section.
// You can create your own models in the models.ts section.
// Sounds are at the very bottom.
// If you touch graphics.ts, try not to freeze the game!
//
// -- Framebuffer Globals --
var api: WebEngineAPI;
const FPS = 24;
var tileXCount = 0;
var tileYCount = 0;
const RES_LEVELS = [
[1, 1],
[2, 2],
[3, 2],
[3, 3],
[4, 3],
[4, 4],
[5, 4],
[6, 6],
[8, 8],
[10, 8],
[20, 16],
];
var selectedResLevel = 6;
var fb: Framebuffer;
recreateFramebuffer();
// -- Bullet Globals --
const BULLET_DAMAGE = 40;
const BULLET_COOLDOWN_THRESHOLD = 0.12;
const BULLET_MAX_DISTANCE = 40;
const BULLET_SPEED = 2.5;
interface Bullet {
origin: Vec3;
position: Vec3;
direction: Vec3;
}
var bullets: Bullet[] = [];
// -- Player Globals --
const PLAYER_MAX_HEALTH = 50;
const PLAYER_POISON_TICK = 0.01;
const PLAYER_MAX_VELOCITY = 0.65;
const PLAYER_FRICTION_SCALAR = 0.006;
const PLAYER_WALL_BOUNCE_SCALAR = 0.8;
const PLAYER_ACCELERATION = 0.13;
interface Player {
position: Vec3;
velocity: Vec3;
direction: Vec3;
health: number;
bobTick: number;
yaw: number;
lastBulletTime: number;
}
var player: Player;
var playerDead: boolean = false;
const MEDKIT_HEAL_AMOUNT = 18;
const MEDKIT_PLAYER_SPAWN_MIN_RADIUS = 40;
const MEDKIT_PLAYER_SPAWN_MAX_RADIUS = 200;
const MEDKIT_PICKUP_RANGE = 5;
const MEDKIT_POPUP_RISE_SPEED = 0.3;
const MEDKIT_POPUP_MAX_HEIGHT = 4;
interface Medkit {
position: Vec3;
}
var medkit: Medkit | null = null;
interface MedkitHealPopup {
y: number;
}
var medkitHealPopups: MedkitHealPopup[] = [];
// -- Enemy Globals --
const ENEMY_DAMAGE = 0.2;
const ENEMY_MAX_DODGE = 0.3;
const ENEMY_REACH = 1.8;
const ENEMY_SPEED_INCREMENT_SCALAR = 0.0006;
const ENEMY_PLAYER_SPAWN_MIN_RADIUS = 60;
const ENEMY_PLAYER_SPAWN_MAX_RADIUS = 350;
interface Enemy {
speed: number;
health: number;
color: number;
position: Vec3;
dodgeEntropy: number;
}
var enemies: Enemy[] = [];
// -- Stage Globals --
const ENEMY_CAP_STAGES = [3, 5, 8, 10, 15, 18, 50];
const ENEMY_SPAWN_FREQUENCY_STAGES = [0.03, 0.1, 0.15, 0.3, 0.4, 0.5, 10];
const KILL_SCREEN_STAGE = 6;
function getStageNumber() {
if (startTime <= 20) {
return 0;
} else if (startTime <= 100) {
return 1;
} else if (startTime <= 200) {
return 2;
} else if (startTime <= 300) {
return 3;
} else if (startTime <= 400) {
return 4;
} else if (startTime <= 999) {
return 5;
} else {
return KILL_SCREEN_STAGE;
}
}
// -- Explosion Globals --
const EXPLOSION_MAX_SIZE = 2.0;
const EXPLOSION_GROWTH_INCREMENT = 0.3;
interface Explosion {
position: Vec3;
sizeScalar: number;
}
var explosions: Explosion[] = [];
// -- Map Settings --
interface MapSetting {
name: string;
bound: number;
wallCount: number;
wallMaxScale: number;
}
const MAP_SETTINGS: MapSetting[] = [
{
name: "SS ",
bound: 60,
wallCount: 12,
wallMaxScale: 4,
},
{
name: " S ",
bound: 75,
wallCount: 24,
wallMaxScale: 4,
},
{
name: " M ",
bound: 85,
wallCount: 30,
wallMaxScale: 5,
},
{
name: " L ",
bound: 100,
wallCount: 40,
wallMaxScale: 7,
},
{
name: "XL ",
bound: 150,
wallCount: 110,
wallMaxScale: 8,
},
{
name: "XXL",
bound: 500,
wallCount: 1300,
wallMaxScale: 12,
},
{
name: "XXX",
bound: 800,
wallCount: 2500,
wallMaxScale: 12,
},
];
var selectedMapSetting = 2;
const MAP_WALL_Y = 6;
const MAP_WALL_TO_WALL_MIN_DISTANCE = 20;
interface Wall {
scale: Vec3;
position: Vec3;
color: Color;
}
var walls: Wall[] = [];
function incMapSetting() {
api.playTune(soundSettingChange());
selectedMapSetting += 1;
if (selectedMapSetting >= MAP_SETTINGS.length)
selectedMapSetting = MAP_SETTINGS.length - 1;
}
function decMapSetting() {
api.playTune(soundSettingChange());
selectedMapSetting -= 1;
if (selectedMapSetting < 0) selectedMapSetting = 0;
}
// -- Game State --
enum GameState {
MENU,
PLAY,
}
var gameState = GameState.MENU;
let startTime = 0;
// -- Spawn Functions --
function spawnWalls() {
let settings = MAP_SETTINGS[selectedMapSetting];
let north = {
scale: [1, MAP_WALL_Y, settings.bound] as Vec3,
position: [settings.bound, 0, 0] as Vec3,
color: "1",
};
let south = {
scale: [1, MAP_WALL_Y, settings.bound] as Vec3,
position: [-settings.bound, 0, 0] as Vec3,
color: "1",
};
let west = {
scale: [settings.bound, MAP_WALL_Y, 1] as Vec3,
position: [0, 0, settings.bound] as Vec3,
color: "1",
};
let east = {
scale: [settings.bound, MAP_WALL_Y, 1] as Vec3,
position: [0, 0, -settings.bound] as Vec3,
color: "1",
};
walls.push(north);
walls.push(south);
walls.push(west);
walls.push(east);
for (let i = 0; i < settings.wallCount; i++) {
let scaleX = Math.random() * settings.wallMaxScale + 3;
let scaleZ = Math.random() * settings.wallMaxScale + 3;
let positionX = Math.random() * (settings.bound - 2) * 2 - settings.bound;
let positionZ = Math.random() * (settings.bound - 2) * 2 - settings.bound;
let failedWall = false;
for (let wi in walls) {
let other = walls[wi];
if (
vecDistance(
[other.position[0], 0, other.position[2]],
[positionX, 0, positionZ]
) < MAP_WALL_TO_WALL_MIN_DISTANCE
) {
failedWall = true;
break;
}
}
if (
Math.abs(positionX) < settings.wallMaxScale + 4 ||
Math.abs(positionZ) < settings.wallMaxScale + 4
) {
failedWall = true;
}
if (
settings.bound - Math.abs(positionX) < 18 ||
settings.bound - Math.abs(positionZ) < 18
) {
failedWall = true;
}
if (failedWall) {
i--;
continue;
}
let wall = {
scale: [scaleX, MAP_WALL_Y, scaleZ] as Vec3,
position: [positionX, 0, positionZ] as Vec3,
color: "1",
};
walls.push(wall);
}
}
function spawnMedkit(argPositionX: number | null, argPositionZ: number | null) {
let finalPositionX = argPositionX;
let finalPositionZ = argPositionZ;
if (argPositionX == null && argPositionZ == null) {
let mapSetting = MAP_SETTINGS[selectedMapSetting];
while (true) {
let positionX = Math.random() * mapSetting.bound * 2 - mapSetting.bound;
let positionZ = Math.random() * mapSetting.bound * 2 - mapSetting.bound;
let distToPlayer = vecDistance(player.position, [
positionX,
0,
positionZ,
]);
if (
getCollisionWall([positionX, 0, positionZ]) == null &&
distToPlayer <= MEDKIT_PLAYER_SPAWN_MAX_RADIUS &&
distToPlayer >= MEDKIT_PLAYER_SPAWN_MIN_RADIUS
) {
if (finalPositionX == null) {
finalPositionX = positionX;
}
if (finalPositionZ == null) {
finalPositionZ = positionZ;
}
break;
}
}
}
medkit = {
position: [finalPositionX!, 3, finalPositionZ!],
};
}
function spawnMedkitHealPopup() {
let popup = {
y: 0,
};
medkitHealPopups.push(popup);
}
function spawnEnemy() {
let stage = getStageNumber();
if (enemies.length >= ENEMY_CAP_STAGES[stage]) return;
let mapSetting = MAP_SETTINGS[selectedMapSetting];
let position;
while (true) {
let positionX = Math.random() * mapSetting.bound * 2 - mapSetting.bound;
let positionY = Math.random() * mapSetting.bound * 2 - mapSetting.bound;
position = [positionX, 0, positionY] as Vec3;
let distToPlayer = vecDistance(player.position, position);
if (
distToPlayer >= ENEMY_PLAYER_SPAWN_MIN_RADIUS &&
distToPlayer <= ENEMY_PLAYER_SPAWN_MAX_RADIUS
) {
break;
}
}
let randomSpeed = Math.random() * 0.4 + 0.1;
let randomHealth = Math.random() * 100 + 40;
let numColors = [0, 3, 5, 7, 4, 6, 8, 9];
let randomColor = Math.floor(Math.random() * 8);
let randomDodge;
if (Math.random() >= 0.8) {
randomDodge = 0;
} else {
randomDodge = Math.random() * ENEMY_MAX_DODGE;
}
let enemy = {
speed: randomSpeed,
health: randomHealth,
color: numColors[randomColor],
dodgeEntropy: randomDodge,
position,
};
enemies.push(enemy);
}
function spawnBullet(origin: Vec3, direction: Vec3) {
api.playTune(soundPew());
let bullet = {
origin,
direction,
position: origin,
};
bullets.push(bullet);
}
function spawnExplosion(position: Vec3) {
api.playTune(soundExplosion());
let explosion = {
position,
sizeScalar: 0.2,
};
explosions.push(explosion);
}
// -- Game Start --
function initInput() {
api.onInput("w", inputLeftUp);
api.onInput("s", inputLeftDown);
api.onInput("a", inputLeftLeft);
api.onInput("d", inputLeftRight);
api.onInput("i", inputRightUp);
api.onInput("j", inputRightLeft);
api.onInput("l", inputRightRight);
api.onInput("k", inputRightDown);
}
function initGame() {
player = {
position: [0, 0, 0],
velocity: [0, 0, 0],
direction: [1, 0, 0],
health: PLAYER_MAX_HEALTH - MEDKIT_HEAL_AMOUNT,
bobTick: 0,
yaw: 0,
lastBulletTime: 0,
};
playerDead = false;
enemies = [];
bullets = [];
explosions = [];
medkit = null;
medkitHealPopups = [];
walls = [];
startTime = 0;
spawnWalls();
spawnMedkit(6, 0);
}
// -- Tick Functions --
function tickPlayer() {
// Look in the direction you should be looking.
player.direction[0] = Math.cos(player.yaw);
player.direction[2] = Math.sin(player.yaw);
player.direction = vecNormalize(player.direction);
// Cap the speed.
if (vecLength(player.velocity) > PLAYER_MAX_VELOCITY) {
player.velocity = vecMulScalar(
vecNormalize(player.velocity),
PLAYER_MAX_VELOCITY
);
}
// Friction.
player.velocity = vecAddVec(
player.velocity,
vecMulScalar(vecNormalize(player.velocity), -PLAYER_FRICTION_SCALAR)
);
// Move forward unless if there's a wall (then you should bounce!).
let next_position = vecAddVec(player.position, player.velocity);
let collisionWall = getCollisionWall(next_position);
if (collisionWall == null) {
player.position = next_position;
} else {
api.playTune(soundPew());
// Over-engineered bouncing.
let wallToPlayer = vecNormalize(
vecSubVec(collisionWall.position, player.position)
);
// Well, our walls are never rotated anyway ¯\_ (ツ)_/¯.
let possibleNormals: Vec3[] = [
[-1, 0, 0],
[1, 0, 0],
[0, -1, 0],
[0, 1, 0],
[0, 0, -1],
[0, 0, 1],
];
let normal = possibleNormals[0];
let minDist = vecDistance(possibleNormals[0], wallToPlayer);
for (let i in possibleNormals) {
let n = possibleNormals[i];
let dist = vecDistance(n, wallToPlayer);
if (dist < minDist) {
minDist = dist;
normal = n;
}
}
let reflected = vecAddVec(
vecMulScalar(normal, -2 * vecDot(normal, player.velocity)),
player.velocity
);
player.velocity = vecMulScalar(reflected, PLAYER_WALL_BOUNCE_SCALAR);
}
}
function tickBullets() {
// Bullet go forward.
for (let i in bullets) {
let bullet = bullets[i];
bullet.position = vecAddVec(
bullet.position,
vecMulScalar(bullet.direction, BULLET_SPEED)
);
}
// Remove bullets that have gone too far.
bullets = bullets.filter(
(bullet) =>
vecDistance(bullet.position, bullet.origin) <= BULLET_MAX_DISTANCE
);
}
function tickEnemies() {
let enemySpeedIncrement = ENEMY_SPEED_INCREMENT_SCALAR;
for (let i in enemies) {
let enemy = enemies[i];
// Add speed. They will always outrun you. (Not really.)
enemy.speed += enemySpeedIncrement;
// Get closer if enemy isn't already too close.
if (vecDistance(player.position, enemy.position) >= ENEMY_REACH) {
let direction = vecNormalize(vecSubVec(player.position, enemy.position));
// Adding dodge makes things more interesting.
let dodgeDirection = vecMulScalar(
vec3CrossProduct(direction, [0, 1, 0]),
Math.sin(startTime * 6 * enemy.dodgeEntropy) * enemy.dodgeEntropy
);
let next_position = vecAddVec(
enemy.position,
vecMulScalar(direction, enemy.speed)
);
next_position = vecAddVec(next_position, dodgeDirection);
// Don't go through walls. Go around instead. (Or try to anyway.)
if (getCollisionWall(enemy.position) == null) {
enemy.position = next_position;
} else {
// Pretty scuffed solution, but hey.
enemy.position = vecAddVec(
enemy.position,
vecMulScalar(
vec3CrossProduct([0, 1, 0], player.direction),
enemy.speed
)
);
}
} else {
// BAM. Attack the player.
api.playTune(soundEnemyAttack());
player.health -= ENEMY_DAMAGE;
}
// BAM. Attacked by player.
if (hitByBullet(enemy.position)) {
enemy.health -= BULLET_DAMAGE;
if (enemy.health <= 0) {
spawnExplosion(enemy.position);
}
}
}
// Remove the deceased.
enemies = enemies.filter((enemy) => enemy.health > 0);
}
// Check if hit by bullet.
// Destroy the bullet if so.
function hitByBullet(agent: Vec3): boolean {
const HITBOX_RADIUS = 2.5;
let newBullets = bullets.filter((bullet) => {
return vecDistance(agent, bullet.position) >= HITBOX_RADIUS;
});
let ret = newBullets.length != bullets.length;
bullets = newBullets;
return ret;
}
function tickExplosions() {
// BOOM!
for (let i in explosions) {
let explosion = explosions[i];
explosion.sizeScalar += EXPLOSION_GROWTH_INCREMENT;
}
// If too big, remove.
explosions = explosions.filter(
(explosion) => explosion.sizeScalar <= EXPLOSION_MAX_SIZE
);
}
function tickMedkit() {
if (medkit) {
// Pick up the medkit if it's close enough.
if (vecDistance(player.position, medkit.position) <= MEDKIT_PICKUP_RANGE) {
api.playTune(soundPickUp());
medkit = null;
spawnMedkitHealPopup();
player.health += MEDKIT_HEAL_AMOUNT;
if (player.health > PLAYER_MAX_HEALTH) {
player.health = PLAYER_MAX_HEALTH;
}
}
} else {
// Spawn medkit if there isn't aleady one.
spawnMedkit(null, null);
}
}
function tickPoison() {
player.health -= PLAYER_POISON_TICK * (getStageNumber() + 1);
}
function tickMedkitHealPopup() {
// Popup go up.
for (let i in medkitHealPopups) {
let popup = medkitHealPopups[i];
popup.y += MEDKIT_POPUP_RISE_SPEED;
}
// Popup go away.
medkitHealPopups = medkitHealPopups.filter(
(popup) => popup.y <= MEDKIT_POPUP_MAX_HEIGHT
);
}
function tickEnemySpawn() {
// Spawn enemies based on stage #.
let spawnParam = ENEMY_SPAWN_FREQUENCY_STAGES[getStageNumber()];
if (Math.abs(startTime % 8) < spawnParam) {
spawnEnemy();
}
}
var lastStageNumber = getStageNumber();
function tickGame() {
// Game over.
if (player.health <= 0) {
api.playTune(soundDeath());
playerDead = true;
setTimeout(() => {
gameState = GameState.MENU;
}, 3000);
}
if (!playerDead) {
startTime += 1 / FPS;
if (lastStageNumber != getStageNumber()) {
api.playTune(soundStageUp());
}
lastStageNumber = getStageNumber();
tickPlayer();
tickEnemies();
tickBullets();
tickMedkit();
tickExplosions();
tickPoison();
tickEnemySpawn();
tickMedkitHealPopup();
} else {
// Look up when you die.
if (player.direction[1] >= -Math.PI / 2) {
player.direction[1] -= 0.2;
}
}
}
function tickMenu() {
startTime += 1 / FPS;
}
// -- Collision Detection --
function getCollisionWall(d: Vec3): Wall | null {
for (let i in walls) {
let wall = walls[i];
let boxMinX = Math.min(
wall.position[0] - wall.scale[0],
wall.position[0] + wall.scale[0]
);
let boxMaxX = Math.max(
wall.position[0] - wall.scale[0],
wall.position[0] + wall.scale[0]
);
let boxMinZ = Math.min(
wall.position[2] - wall.scale[2],
wall.position[2] + wall.scale[2]
);
let boxMaxZ = Math.max(
wall.position[2] - wall.scale[2],
wall.position[2] + wall.scale[2]
);
if (
d[0] >= boxMinX &&
d[0] <= boxMaxX &&
d[2] >= boxMinZ &&
d[2] <= boxMaxZ
) {
return wall;
}
}
return null;
}
// -- Input --
function playerBob() {
player.bobTick += 1;
player.position[1] = Math.sin(player.bobTick * 0.8) * 0.2 + 0.1;
}
function inputLeftUp() {
switch (gameState) {
case GameState.MENU:
gameState = GameState.PLAY;
initGame();
break;
case GameState.PLAY:
playerBob();
player.velocity = vecAddVec(
player.velocity,
vecMulScalar(player.direction, PLAYER_ACCELERATION)
);
break;
}
}
function inputLeftDown() {
switch (gameState) {
case GameState.MENU:
break;
case GameState.PLAY:
playerBob();
player.velocity = vecAddVec(
player.velocity,
vecMulScalar(player.direction, -PLAYER_ACCELERATION)
);
break;
}
}
function inputLeftLeft() {
switch (gameState) {
case GameState.MENU:
break;
case GameState.PLAY:
playerBob();
player.velocity = vecAddVec(
player.velocity,
vecMulScalar(
vecNormalize(vec3CrossProduct(player.direction, [0, 1, 0])),
PLAYER_ACCELERATION
)
);
break;
}
}
function inputLeftRight() {
switch (gameState) {
case GameState.MENU:
break;
case GameState.PLAY:
playerBob();
player.velocity = vecAddVec(
player.velocity,
vecMulScalar(
vecNormalize(vec3CrossProduct(player.direction, [0, 1, 0])),
-PLAYER_ACCELERATION
)
);
break;
}
}
function inputRightUp() {
switch (gameState) {
case GameState.MENU:
incrementRes();
break;
case GameState.PLAY:
if (startTime - player.lastBulletTime >= BULLET_COOLDOWN_THRESHOLD) {
spawnBullet(vecAddVec(player.position, [0, 2, 0]), player.direction);
player.lastBulletTime = startTime;
}
break;
}
}
function inputRightDown() {
switch (gameState) {
case GameState.MENU:
decrementRes();
break;
case GameState.PLAY:
player.yaw -= Math.PI;
break;
}
}
function inputRightLeft() {
switch (gameState) {
case GameState.MENU:
decMapSetting();
break;
case GameState.PLAY:
player.yaw += Math.PI / 30;
break;
}
}
function inputRightRight() {
switch (gameState) {
case GameState.MENU:
incMapSetting();
break;
case GameState.PLAY:
player.yaw -= Math.PI / 30;
break;
}
}
// -- Renderer --
function incrementRes() {
api.playTune(soundSettingChange());
selectedResLevel += 1;
if (selectedResLevel >= RES_LEVELS.length)
selectedResLevel = RES_LEVELS.length - 1;
recreateFramebuffer();
}
function decrementRes() {
api.playTune(soundSettingChange());
selectedResLevel -= 1;
if (selectedResLevel < 0) selectedResLevel = 0;
recreateFramebuffer();
}
function recreateFramebuffer() {
let res = RES_LEVELS[selectedResLevel];
tileXCount = res[0];
tileYCount = res[1];
fb = fbNew(tileXCount, tileYCount);
}
function renderGame() {
fbClearColor(fb, 2);
fbClearDepth(fb, 1000);
let cameraPosition = player.position;
let cameraFront = vecNormalize(player.direction);
let baseRenderPass: RenderPass = {
cameraPosition,
cameraFront,
projection: {
fov_rad: Math.PI / 2.0,
near: 0.1,
far: 100.0,
},
enableDepth: true,
cullScalar: 1,
modelMatrix: mat4Identity(),
colors: "0",
borderColor: "0",
triangles: [],
};
for (let i in walls) {
let wall = walls[i];
let mv = mat4Identity();
mv = mat4Scale(mv, wall.scale);
mv = mat4Translate(mv, wall.position);
baseRenderPass.modelMatrix = mv;
baseRenderPass.colors = "1";
baseRenderPass.borderColor = "0";
baseRenderPass.triangles = verticesCube();
fbRender(fb, baseRenderPass);
}
for (let i in enemies) {
let enemy = enemies[i];
let mv = mat4Identity();
mv = mat4Scale(mv, [0.015, -0.015, 0.01]);
mv = mat4Rotate(mv, startTime * 2 * enemy.color, [0, 1, 0]);
mv = mat4Translate(mv, vecSubVec(enemy.position, [0, -2, 0]));
baseRenderPass.modelMatrix = mv;
baseRenderPass.colors = enemy.color;
baseRenderPass.borderColor = enemy.color;
baseRenderPass.triangles = verticesBobPerson();
fbRender(fb, baseRenderPass);
}
for (let i in bullets) {
let bullet = bullets[i];
let mv = mat4Identity();
mv = mat4Translate(mv, bullet.position);
mv = mat4Scale(mv, [0.3, 0.3, 0.3]);
baseRenderPass.modelMatrix = mv;
baseRenderPass.colors = "6";
baseRenderPass.borderColor = "6";
baseRenderPass.triangles = verticesCube();
fbRender(fb, baseRenderPass);
}
for (let i in explosions) {
let explosion = explosions[i];
let mv = mat4Identity();
mv = mat4Translate(mv, explosion.position);
mv = mat4Scale(mv, vecMulScalar([1, 1, 1], explosion.sizeScalar));
baseRenderPass.modelMatrix = mv;
baseRenderPass.colors = "9";
baseRenderPass.borderColor = "9";