-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.js
103 lines (93 loc) · 2.55 KB
/
Game.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
class Game {
static KEY_CODES = {
LEFT: 'ArrowLeft',
RIGHT: 'ArrowRight',
UP: 'ArrowUp',
DOWN: 'ArrowDown',
J: 'j',
K: 'k',
L: 'l',
D: 'd',
G: 'g',
R: 'r',
F: 'f',
Q: 'q',
W: 'w',
E: 'e',
};
constructor(document, canvasElement, width, height) {
this.context = canvasElement.getContext('2d');
this.width = width;
this.height = height;
canvasElement.width = this.width;
canvasElement.height = this.height;
this.listenKeys = [
Game.KEY_CODES.LEFT,
Game.KEY_CODES.RIGHT,
Game.KEY_CODES.UP,
Game.KEY_CODES.DOWN,
Game.KEY_CODES.J,
Game.KEY_CODES.K,
Game.KEY_CODES.L,
Game.KEY_CODES.D,
Game.KEY_CODES.G,
Game.KEY_CODES.R,
Game.KEY_CODES.F,
Game.KEY_CODES.Q,
Game.KEY_CODES.W,
Game.KEY_CODES.E,
];
this.keysPressed = {};
this.listenKeys.forEach(keyCode => this.keysPressed[keyCode] = false);
this.playground = new Playground(
this.width,
this.height,
[
new Player(this, 260, 23, Math.PI * 1.5, {
[Player.CONTROL_ACTIONS.TURN_LEFT]: Game.KEY_CODES.LEFT,
[Player.CONTROL_ACTIONS.TURN_RIGHT]: Game.KEY_CODES.RIGHT,
[Player.CONTROL_ACTIONS.GO_FORWARD]: Game.KEY_CODES.UP,
[Player.CONTROL_ACTIONS.GO_BACKWARD]: Game.KEY_CODES.DOWN,
[Player.CONTROL_ACTIONS.GO_LEFT]: Game.KEY_CODES.J,
[Player.CONTROL_ACTIONS.GO_RIGHT]: Game.KEY_CODES.K,
[Player.CONTROL_ACTIONS.SHOOT]: Game.KEY_CODES.L,
}, 5),
new Player(this, 150, 575, Math.PI * 1.5, {
[Player.CONTROL_ACTIONS.TURN_LEFT]: Game.KEY_CODES.D,
[Player.CONTROL_ACTIONS.TURN_RIGHT]: Game.KEY_CODES.G,
[Player.CONTROL_ACTIONS.GO_FORWARD]: Game.KEY_CODES.R,
[Player.CONTROL_ACTIONS.GO_BACKWARD]: Game.KEY_CODES.F,
[Player.CONTROL_ACTIONS.GO_LEFT]: Game.KEY_CODES.Q,
[Player.CONTROL_ACTIONS.GO_RIGHT]: Game.KEY_CODES.W,
[Player.CONTROL_ACTIONS.SHOOT]: Game.KEY_CODES.E,
}, 5),
],
new Ball(420, 300),
[
new Wall(50, 50, 20, 420),
new Wall(120, 50, 420, 20),
new Wall(520, 120, 20, 430),
new Wall(50, 530, 420, 20),
new Wall(350, 150, 40, 200),
],
);
document.addEventListener('keydown', (event) => {
this.onKeyPress(event.key, true);
});
document.addEventListener('keyup', (event) => {
this.onKeyPress(event.key, false);
});
}
render() {
this.playground.render(this.context);
window.requestAnimationFrame(this.render.bind(this));
}
onKeyPress(keyCode, wasPressed) {
for (let i = 0; i < this.listenKeys.length; i++) {
if (keyCode === this.listenKeys[i]) {
this.keysPressed[keyCode] = wasPressed;
break;
}
}
}
}