-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
887 lines (851 loc) · 28.9 KB
/
script.js
File metadata and controls
887 lines (851 loc) · 28.9 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
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
// let R = require("ramda");
/* need a Game and Entity class if I can't figure out how to more completely separate these pass game to entity
or call static methods on Game if it doesn't need specific parameters. Different types of entities will subclass Entity
Entity will include player, aliens, shots fired, solid objects, possible subclasses include enemy, wall, bullet, player
Each entity will have a render method which will draw it on the canvas, a destroy method which will remove it
from the list of active entities. An onCollision method which shall desribe how it response to a collision which
may in turn call the destroy method if need be.
There shall be a board component OR a board class that describing a grid of positions aprox 60x45 "blocks"
aprox 2700 represented as a 2D array each representing a row. When an entity moves into a space what it
will do is write a reference to itself to the board.
Each entity that moves will not deliberately move into a position that is occupied by any other entity. If it does try
to do so it will end up calling onCollsion with the pre-existing entity as an argument. The simplest thing to do is probably initially just destroy both either on pre-existing square or by destroying first the item at the square then
moving into the square and being destroyed in turn. Updates shall be processed sequentally starting with the player,
so that logic remains consistent. Eg a missile advances nothing is in that space so nothing further happens. Then
the player is advanced by virtue of having pressed an arrow key and finds itself moving to the same space as the
missile. The missile is destoryed. The player is moved into the position formerly occupied by the missile and is
destroyed. The game end logic happens.
As soon as all logic happens everything is rendered by calling render on the background and then all entities.
Control of player happens by plugging actions into a buffer to be evaluated next tick to be evaluated in turn.
Notably hitting up 17 times really fast doesn't mean the player can move further or fire faster in one turn.
They should be asyncstuffed into the buffer and emptied each tick. If direction exists move in the first direction hit
If space exists fire, if escape exists pause at beginning of tick.
*/
class Game {
constructor(canvasElement) {
this.canvas = canvasElement;
this.ctx = canvas.getContext("2d");
this.canvas.setAttribute("height", getComputedStyle(canvas)["height"]);
this.canvas.setAttribute("width", getComputedStyle(canvas)["width"]);
this.entities = [];
this.graveyard = [];
this.buried = [];
this.width = 128;
this.height = 96;
this.board = [];
this.row = [];
this.ended = false;
this.ended = false;
this.paused = false;
this.enemiesToSpawn = 1;
//used to take an action every nth tick
this.nthTick = 0;
//In this many ticks we will generate another enemy and decrease this value leading to faster spawns
this.ticksToGenerateEnemies = 300;
//filll a column with zeros to represent an empty square
for (let i = 0; i < this.width; i++) {
this.row.push(0);
}
//fill the board with rows of zeros
for (let i = 0; i < this.height; i++) {
this.board.push(this.row.slice(0));
}
//will be used to to reset the board to an empty state
this.emptyBoard = structuredClone(this.board);
//darn you canvas size rounding errors
this.elementSize = this.canvas.width / this.width;
this.canvas.height = this.elementSize * 96;
this.ctx.fillStyle = "red";
this.score = 0;
this.gameOverSound = new Audio("gameover.mp3");
this.renderStartMessage();
}
clear() {
this.ctx.clearRect(0, 0, canvas.width, canvas.height);
}
color(x, y, color) {
this.ctx.fillStyle = color;
this.ctx.fillRect(
x * this.elementSize,
y * this.elementSize,
this.elementSize,
this.elementSize
);
}
positionInsideBoard(p) {
return p.x >= 0 && p.y >= 0 && p.x < this.width && p.y < this.height;
}
render() {
if (this.ended === false) {
this.clear();
this.entities.forEach((e) => {
e.render();
});
this.ctx.fillStyle = "red";
this.renderScore();
this.ctx.fillStyle = "red";
this.renderPaused();
this.ctx.fillStyle = "red";
} else this.renderEnd();
}
rockIt() {
let top = range(this.width);
for (let i = 0; i < top.length; i++) {
new InvulnerableRock(i, 0, this);
new InvulnerableRock(i, this.height - 1, this);
}
let sides = range(this.height);
for (let i = 1; i < sides.length - 1; i++) {
new InvulnerableRock(0, i, this);
new InvulnerableRock(this.width - 1, i, this);
}
}
startLoop() {
this.loop = setInterval(() => {
this.tick();
this.render();
}, 33);
}
tick() {
if (this.paused === false) {
//clear the positions occupied by destroyed entities
//a second tier "buried" shouldn't be needed added to avoid
//double counting aliens for score should be fixed by just not
//doing that in the future
this.graveyard.forEach((e) => {
if (e instanceof AlienShip && !this.buried.includes(e)) {
this.score++;
this.buried.push(e);
}
e.positions().forEach((p) => (this.board[p.y][p.x] = 0));
});
//to hold entities to be destroyed at start of tick
this.graveyard = [];
//this will be examined to allow destoryed enemy ships to be counted
this.buried = [];
this.generateEnemies();
//ensure metadata map holds correct points for each entity
this.entities.forEach((e) => e.claimPointsOnBoard());
// each entity has an array of functions it should evaluate every tick herein termed strategies
// actions are strategies for player entity should simply be merged with strategies as use case is identical
this.entities.forEach((e) => {
e.strategies.forEach((s) => {
s(e);
});
e.actions.forEach((a) => {
a();
});
e.actions = [];
});
}
}
randomPositionWithinBoard() {
let x = rand(3, this.width - 3);
let y = rand(3, this.height - 3);
return [x, y];
}
randomPositionOnPeripheryOfBoard() {
while (true) {
let r = this.randomPositionWithinBoard();
let x = r[0];
let y = r[1];
let xRange = range(3, 21).concat(this.width - 20, this.width - 2);
let yRange = range(3, 21).concat(this.height - 20, this.height - 2);
if (xRange.includes(x) || yRange.includes(y)) {
return r;
}
}
}
// ticks both get closer together and spawn more enemies as time goes by
generateEnemies() {
this.nthTick++;
if (this.nthTick % this.ticksToGenerateEnemies === 0) {
for (let i = 0; i < this.enemiesToSpawn; i++)
new AlienShip(
...this.randomPositionOnPeripheryOfBoard(),
this,
"up"
);
this.ticksToGenerateEnemies = Math.max(
10,
this.ticksToGenerateEnemies - 1
);
this.enemiesToSpawn = Math.min(this.enemiesToSpawn + 1, 8);
}
}
// setup for initial game state
scenario() {
this.player = new PlayerShip(
this.width / 2 + 5,
this.height / 2 + 5,
this,
"up"
);
this.player.face("right");
this.base = new Base(this.width / 2, this.height / 2, this, "up");
for (let i = 0; i < 3; i++) {
new AlienShip(
...this.randomPositionOnPeripheryOfBoard(),
this,
"up"
);
}
this.rockIt();
}
start() {
this.started = true;
this.score = 0;
this.ended = false;
// this.hideInstructions();
this.startLoop();
}
end() {
this.ended = true;
clearInterval(this.loop);
this.clear();
this.entities = [];
this.enemiesToSpawn = 1;
this.ticksToGenerateEnemies = 300;
this.nthTick = 0;
this.paused = false;
this.board = structuredClone(this.emptyBoard);
}
pause() {
if (this.paused) this.paused = false;
else {
this.paused = true;
}
}
restart() {
game.end();
game.scenario();
game.start();
}
renderScore() {
this.printToScreen(
this.score,
this.canvas.width / 15,
this.canvas.width - this.canvas.width / 25,
this.canvas.width / 15
);
}
renderStartMessage() {
this.ctx.fillStyle = "#735399";
this.printToScreen(
"CAVE INVADERS",
this.canvas.width / 10,
this.canvas.width / 2,
this.canvas.width / 2 - this.canvas.width * 0.22
);
this.ctx.fillStyle = "red";
this.printToScreen("Hit Enter to Begin", this.canvas.width / 15);
this.ctx.fillStyle = "white";
this.printToScreen(
" Score points by destroying alien ships until you ",
this.canvas.width / 45,
this.canvas.width / 2,
this.canvas.height / 2 + this.canvas.height * 0.07,
"Istok Web"
);
this.printToScreen(
"get shot, crash, or an alien ship reaches your base.",
this.canvas.width / 45,
this.canvas.width / 2,
this.canvas.height / 2 + this.canvas.height * 0.1,
"Istok Web"
);
}
renderPaused() {
if (this.paused === true) {
this.printToScreen("Paused", this.canvas.width / 5);
}
}
printToScreen(
text,
size,
x = this.canvas.width / 2,
y = this.canvas.height / 2,
font = "Charge Vector"
) {
this.ctx.textAlign = "center";
this.ctx.font = `${size}px ${font}`;
this.fillStyle = "red";
this.ctx.fillText(text, x, y);
}
renderEnd() {
this.ctx.fillStyle = "#735399";
this.printToScreen(
"CAVE INVADERS",
this.canvas.width / 10,
this.canvas.width / 2,
this.canvas.width / 2 - this.canvas.width * 0.22
);
this.ctx.fillStyle = "red";
this.ctx.textAlign = "center";
let rating;
if (this.score === 0) rating = "zero";
else if (this.score > 100) rating = "cheater";
else if (this.score > 25) rating = "killer";
else if (this.score > 10) rating = "beginner";
else if (this.score > 0) rating = "loser";
this.printToScreen(` Hit Escape to Restart`, this.canvas.width / 15);
this.ctx.fillStyle = "white";
this.printToScreen(
`SCORE: ${this.score} RATING: ${rating} `,
this.canvas.width / 45,
this.canvas.width / 2,
this.canvas.height / 2 + this.canvas.height * 0.07
);
}
}
class Entity {
constructor(x, y, game, position) {
this.x = x;
this.y = y;
this.game = game;
this.ticksTowardsMovement = 0;
this.ticksToMove = 1;
game.entities.push(this);
this.position = position;
this.positions = this.positionsUp;
this.colors = ["purple"];
this.priorPositions = this.positions();
this.checkForCollsion();
this.claimPointsOnBoard();
this.strategies = [];
this.actions = [];
}
// positions are obviously going to depend on orientation
positionsUp() {
return [{ x: this.x, y: this.y, color: this.colors[0] }];
}
positionsDown() {
return [{ x: this.x, y: this.y, color: this.colors[0] }];
}
positionsLeft() {
return [{ x: this.x, y: this.y, color: this.colors[0] }];
}
positionsRight() {
return [{ x: this.x, y: this.y, color: this.colors[0] }];
}
face(direction) {
this.position = direction;
switch (direction) {
case "up":
this.positions = this.positionsUp;
break;
case "down":
this.positions = this.positionsDown;
break;
case "left":
this.positions = this.positionsLeft;
break;
case "right":
this.positions = this.positionsRight;
break;
}
}
faceEntity(e) {
if (e.x === this.x) {
if (e.x > this.x) this.face("left");
if (e.x < this.x) this.face("right");
}
if (e.y === this.y) {
if (e.y > this.y) this.face("up");
if (e.y < this.y) this.face("down");
}
}
facingEntity(e) {
if (e.x === this.x) {
if (e.y > this.y && this.positions === this.positionsDown)
return true;
if (e.y < this.y && this.positions === this.positionsUp)
return true;
}
if (e.y === this.y) {
if (e.x > this.x && this.positions === this.positionsRight)
return true;
if (e.x < this.x && this.positions === this.positionsLeft)
return true;
}
return false;
}
render() {
this.positions().forEach((p) => {
this.game.color(p.x, p.y, p.color);
});
}
allPositionsWithinBoard() {
return this.positions().every((p) => {
return game.positionInsideBoard({ x: p.x, y: p.y });
});
}
//move and rotate functions that directly change position or orientation
// are effectively wrapped by changePositionOrReset by virtue of calling
//this function with their operation and having it perform or roll back the op
// to avoid an invalid state
move(direction) {
this.changePositionOrReset(() => this._move(direction));
}
_move(direction) {
if (this.ticksTowardsMovement >= this.ticksToMove) {
switch (direction) {
case "up":
this.position = direction;
this.positions = this.positionsUp;
this.y = this.y - 1;
break;
case "down":
this.position = direction;
this.positions = this.positionsDown;
this.y = this.y + 1;
break;
case "left":
this.position = direction;
this.positions = this.positionsLeft;
this.x = this.x - 1;
break;
case "right":
this.position = direction;
this.positions = this.positionsRight;
this.x = this.x + 1;
break;
}
this.ticksTowardsMovement = 0;
}
this.ticksTowardsMovement++;
}
forward() {
this.move(this.position);
}
//This is a little dumb because move direction will change orientation meaning going back would
// without adjustment simply go back and forth in a singular small area
back() {
switch (this.position) {
case "up":
this.move("down");
this.position = "up";
this.positions = this.positionsUp;
break;
case "down":
this.move("up");
this.position = "down";
this.positions = this.positionsDown;
break;
case "left":
this.move("right");
this.position = "left";
this.positions = this.positionsLeft;
break;
case "right":
this.move("left");
this.position = "right";
this.positions = this.positionsRight;
break;
}
}
moveAbsolute(direction) {
this.move(direction);
}
claimPointsOnBoard() {
this.priorPositions.forEach((p) => {
this.game.board[p.y][p.x] = 0;
});
this.positions().forEach((p) => {
this.game.board[p.y][p.x] = this;
});
}
rotateCounter() {
this.changePositionOrReset(this._rotateCounter.bind(this));
}
_rotateCounter() {
switch (this.position) {
case "up":
this.position = "left";
this.positions = this.positionsLeft;
break;
case "down":
this.position = "right";
this.positions = this.positionsRight;
break;
case "left":
this.position = "down";
this.positions = this.positionsDown;
break;
case "right":
this.position = "up";
this.positions = this.positionsUp;
break;
}
}
rotateClockwise() {
this.changePositionOrReset(this._rotateClockwise.bind(this));
}
_rotateClockwise() {
switch (this.position) {
case "up":
this.position = "right";
this.positions = this.positionsRight;
break;
case "down":
this.position = "left";
this.positions = this.positionsLeft;
break;
case "left":
this.position = "up";
this.positions = this.positionsUp;
break;
case "right":
this.position = "down";
this.positions = this.positionsDown;
break;
}
}
// TODO there should be a better way than filtering the whole list access via ID?
destroy() {
game.entities = game.entities.filter((e) => e !== this);
this.onDestroy();
this.game.graveyard.push(this);
}
onDestroy() {}
onCollide(thing) {
this.destroy();
}
checkForCollsion() {
this.positions().forEach((p) => {
let thingAtPosition = game.board[p.y][p.x];
if (
thingAtPosition !== 0 &&
thingAtPosition !== this &&
thingAtPosition
.positions()
.filter((e) => e.x === p.x && e.y === p.y).length > 0
) {
thingAtPosition.onCollide(this);
this.onCollide(thingAtPosition);
return true;
}
});
return false;
}
// performs OP but rolls back values if it results in an invalid state intended to be used by movement and rotation functions
changePositionOrReset(op) {
let priorX = this.x;
let priorY = this.y;
this.priorPositions = this.positions();
op();
if (this.allPositionsWithinBoard() && !this.checkForCollsion()) {
this.claimPointsOnBoard();
} else {
this.x = priorX;
this.y = priorY;
}
}
//=================================================================
}
class Ship extends Entity {
constructor(x, y, game, position) {
super(x, y, game, position);
this.shotType = AlienShot;
this.ticksToShoot = 80;
this.ticksChargedTowardsShot = 0;
this.zapSound = new Audio("zap.mp3");
this.dieSound = new Audio("die.mp3");
}
shoot() {
if (this.ticksChargedTowardsShot >= this.ticksToShoot) {
this.ticksChargedTowardsShot = 0;
this.zapSound.play();
switch (this.position) {
case "up":
new this.shotType(
this.x,
this.y - 3,
this.game,
this.position
);
break;
case "down":
new this.shotType(
this.x,
this.y + 3,
this.game,
this.position
);
break;
case "left":
new this.shotType(
this.x - 3,
this.y,
this.game,
this.position
);
break;
case "right":
new this.shotType(
this.x + 3,
this.y,
this.game,
this.position
);
break;
}
} else {
}
}
positionsUp() {
return [
{ x: this.x, y: this.y, color: this.colors[0] },
{ x: this.x, y: this.y - 1, color: this.colors[1] },
];
}
positionsDown() {
return [
{ x: this.x, y: this.y, color: this.colors[0] },
{ x: this.x, y: this.y + 1, color: this.colors[1] },
];
}
positionsLeft() {
return [
{ x: this.x, y: this.y, color: this.colors[0] },
{ x: this.x - 1, y: this.y, color: this.colors[1] },
];
}
positionsRight() {
return [
{ x: this.x, y: this.y, color: this.colors[0] },
{ x: this.x + 1, y: this.y, color: this.colors[1] },
];
}
}
class PlayerShip extends Ship {
constructor(x, y, game, position) {
super(x, y, game, position);
this.shotType = PlayerShot;
this.colors = ["blue", "lightblue"];
this.ticksToShoot = 5;
this.strategies.push(() => chargeShot(this));
}
onDestroy() {
this.game.gameOverSound.play();
this.game.end();
}
}
class AlienShip extends Ship {
constructor(x, y, game, position) {
super(x, y, game, position);
this.colors = ["green", "lightgreen"];
this.ticksToShoot = 80;
this.ticksToMove = 7;
this.target = undefined;
this.strategies.push(() => chargeShot(this));
this.strategies.push(() => shootPlayer(this));
this.strategies.push(() => followTarget(this, this.target));
this.strategies.push(() =>
selectNearestTargetFromArrayWeightedByPriority(
this,
this.game.player,
this.game.base
)
);
}
onDestroy() {
this.dieSound.play();
}
}
class Shot extends Entity {
constructor(x, y, game, position) {
super(x, y, game, position);
this.strategies.push(() => {
moveForward(this);
});
}
}
class PlayerShot extends Shot {
constructor(x, y, game, position) {
super(x, y, game, position);
this.colors = ["red"];
this.ticksToMove = 1;
}
}
class AlienShot extends Shot {
constructor(x, y, game, position) {
super(x, y, game, position);
this.colors = ["yellow"];
this.ticksToMove = 1;
}
onCollide(thing) {
if (thing instanceof AlienShip) {
} else this.destroy();
}
}
class Rock extends Entity {
constructor(x, y, game) {
super(x, y, game);
this.colors = ["brown"];
}
}
class InvulnerableRock extends Entity {
constructor(x, y, game) {
super(x, y, game);
this.colors = ["maroon"];
}
destroy() {}
onCollide(thing) {
thing.back();
thing.back();
thing.back();
thing.back();
// if thing overlapped it might have claimed ponts incorrectly
this.claimPointsOnBoard();
}
}
class Base extends Entity {
constructor(x, y, game) {
super(x, y, game);
this.colors = ["gold"];
}
positionsUp() {
return [
{ x: this.x, y: this.y, color: this.colors[0] },
{ x: this.x - 1, y: this.y, color: this.colors[0] },
{ x: this.x + 1, y: this.y, color: this.colors[0] },
{ x: this.x, y: this.y - 1, color: this.colors[0] },
{ x: this.x - 1, y: this.y - 1, color: this.colors[0] },
{ x: this.x + 1, y: this.y - 1, color: this.colors[0] },
{ x: this.x, y: this.y + 1, color: this.colors[0] },
{ x: this.x - 1, y: this.y + 1, color: this.colors[0] },
{ x: this.x + 1, y: this.y + 1, color: this.colors[0] },
];
}
positionsDown() {
return this.positionsUp();
}
positionsLeft() {
return this.positionsUp();
}
positionsRight() {
return this.positionsUp();
}
onCollide(thing) {
if (thing instanceof AlienShip) this.destroy();
}
onDestroy() {
this.game.gameOverSound.play();
game.end();
}
}
//=========================================================================
// Strategies
//=========================================================================
function moveForward(e) {
e.forward();
}
function distanceBetween(a, b) {
return Math.abs(a.x - b.x) + Math.abs(a.y - b.y);
}
function selectNearestTargetFromArrayWeightedByPriority(e, player, base) {
// lets make this simple for starters we are just going to pick between player and base
let distanceToBase = distanceBetween(e, base);
let distanceToPlayer = distanceBetween(e, player);
if (distanceToPlayer < distanceToBase / 2) e.target = player;
else e.target = base;
}
function followTarget(e, target) {
if (typeof target !== "undefined") {
let playerX = target.x;
let playerY = target.y;
if (e.y > playerY) e.move("up");
else if (e.y < playerY) e.move("down");
else if (e.x > playerX) e.move("left");
else if (e.x < playerX) e.move("right");
}
}
function chargeShot(e) {
e.ticksChargedTowardsShot += 1;
}
function shootPlayer(e) {
let playerX = game.player.x;
let playerY = game.player.y;
if (playerX === e.x || playerY === e.y) {
e.faceEntity(game.player);
if (e.facingEntity(game.player)) e.shoot();
}
}
//=======================================================================
// Setup
//=======================================================================
let canvas = document.querySelector("#canvas");
canvas.focus();
let game = new Game(canvas);
game.scenario();
// game.start();
// Logic at the start is a little tortued. We don't want to process key presses while paused but we DO want to process p to unpause
function handleKeys(evt) {
if (game.paused)
if (evt.key === "p") {
game.pause();
return true;
} else return true;
switch (evt.key) {
case "ArrowUp":
game.player.actions.push(() => game.player.forward());
break;
case "ArrowLeft":
game.player.actions.push(() => game.player.rotateCounter());
break;
case "ArrowDown":
game.player.actions.push(() => game.player.back());
break;
case "ArrowRight":
game.player.actions.push(() => game.player.rotateClockwise());
break;
case "w":
game.player.actions.push(() => game.player.move("up"));
break;
case "a":
game.player.actions.push(() => game.player.move("left"));
break;
case "s":
game.player.actions.push(() => game.player.move("down"));
break;
case "d":
game.player.actions.push(() => game.player.move("right"));
break;
case "p":
game.pause();
break;
case "Enter":
if (!game.started) game.start();
break;
case "Escape":
game.restart();
break;
case " ":
game.player.actions.push(() => game.player.shoot());
default:
console.log(evt.key);
}
}
document.addEventListener("keydown", handleKeys.bind(this));
//=======================================================================
// Misc
//=======================================================================
// derived from class discussion
function rand(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
//derived in part from https://stackoverflow.com/questions/3895478/does-javascript-have-a-method-like-range-to-generate-a-range-within-the-supp
function range(start, end) {
let s, e;
if (typeof end === "undefined") {
s = 0;
e = start;
} else {
s = start;
e = end;
}
return Array.from({ length: e - s }, (x, i) => i).map((x) => x + s);
}