-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
221 lines (199 loc) · 5.63 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
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
/*
Game Loop
*/
export const purple = "#993f70";
import { create_show_send_score_button, hide_send_score_button } from "./compat.js";
import { addUIEventListeners, drawUI, clearUI, drawPauseScreen } from "./ui.js";
import { Grid } from "./grid.js";
import { Paddle } from "./paddle.js";
import { Wall, walls } from "./wall.js";
import { Ball, balls } from "./ball.js";
let gamediv = document.getElementById("game");
export const canvas = document.createElement("canvas");
canvas.setAttribute("id", "game-layer");
window.addEventListener("load", () => {
/* set the size of canvas element after stylesheets are loaded */
canvas.width = gamediv.offsetWidth;
canvas.height = gamediv.offsetHeight;
gamediv.appendChild(canvas);
});
export const ctx = canvas.getContext("2d", { alpha: false });
export const fps_ratio = (ms) => {
return Math.min(ms / (1000 / 60), 2);
};
export const paddle = new Paddle();
export const grid = new Grid();
export let score = 0;
export let percent = 0;
export let lives = 3;
export let gameOver = false;
export let gamePaused = false;
let then = Date.now();
const show_send_score_button = create_show_send_score_button(score);
// Timer for pausing the ball during countdowns
// and giving invincibility frames after a death
export let timer = {
active: false,
ballPause: 0,
diedRecently: 0,
dying: 0,
};
function resetWalls() {
while (walls.length > 0) {
walls.pop();
}
}
function resetBall() {
// remove all balls but the first two, and replace them to center of screen
while (balls.length > 2) {
balls.pop();
}
timer.ballPause = 180;
timer.active = true;
timer.dying = 0;
}
let level = {
ballCount: 2,
};
export function initGame() {
/* Create global objects defined in other js files */
balls[0] = new Ball(0);
balls[1] = new Ball(1);
swapDirection();
/* Add event listeners */
addUIEventListeners();
startGame();
}
export function startGame() {
/* Starts a new game */
if (hide_send_score_button != undefined) {
hide_send_score_button();
}
balls[0].x = canvas.width / 2 - 48;
balls[0].y = canvas.height - 96;
balls[1].x = canvas.width / 2 + 48;
balls[1].y = canvas.height - 96;
balls[0].dy = -Ball.speed;
balls[0].dx = -Ball.speed;
balls[1].dy = Ball.speed;
balls[1].dx = Ball.speed;
level.ballCount = 2;
percent = 0;
score = 0;
lives = 3;
gameOver = false;
grid.clear();
resetWalls();
resetBall();
/* Start game loop by drawing the first frame */
draw();
}
/*
* MAIN GAME LOOP
*/
function draw() {
let now = Date.now();
let delta = now - then;
/* Clear the game-layer canvas on every frame & abort early if game is paused */
ctx.fillStyle = "#ffffff";
ctx.fillRect(0, 0, canvas.width, canvas.height);
if (gamePaused == true) {
drawPauseScreen();
requestAnimationFrame(draw);
return;
}
/* Expand walls, turn them into buildings, & flood_fill the grid if building is created */
for (let wall of walls.filter((wall) => {
if (wall.building === null) {
return wall;
}
})) {
wall.expand(delta);
}
/* Move and collide balls & delete expanding walls that intersect balls */
for (let i_ = 0; i_ < balls.length; i_++) {
let collision = balls[i_].move(delta);
// Collision will be -1 if no collision occurred
if (collision > -1) {
// Collision is the index of an expanding wall
if (timer.dying == 0) {
// Decrease `lives` if not already decreasing from a recent collision
timer.dying = 30;
if (lives > 0) {
timer.diedRecently = 90;
}
lives--;
}
Wall.remove(collision);
}
}
/* If the player is dying, tick down the timer that will cause a gameOver soon */
if (timer.dying > 0) {
timer.dying--;
if (timer.dying == 0) {
if (lives < 0) {
show_send_score_button();
gameOver = true;
}
}
}
drawUI();
if (gameOver) return
for (let i = 0; i < walls.length; i++) {
walls[i].draw();
}
for (let i = 0; i < balls.length; i++) {
balls[i].draw();
}
paddle.draw();
requestAnimationFrame(draw);
then = Date.now();
}
export function swapDirection() {
if (paddle.direction == 0) {
paddle.direction = 1;
} else {
paddle.direction = 0;
}
}
export function pauseGame() {
if (gamePaused == true) {
gamePaused = false;
clearUI();
drawUI(true);
} else {
gamePaused = true;
}
}
export function updateScore() {
percent = Math.round(grid.percentFilled());
if (percent > 75) {
score += percent - 75;
nextLevel();
}
}
function nextLevel() {
score += 5;
percent = 0;
level.ballCount += 1;
resetBall();
resetWalls();
grid.clear();
for (let i = 2; i < level.ballCount; i++) {
balls[balls.length] = new Ball(balls.length);
balls[balls.length - 1].x =
canvas.width * (Math.min(0.8, Math.random()) + 0.1);
balls[balls.length - 1].y =
canvas.height * (Math.min(0.8, Math.random()) + 0.1);
if (Math.random() > 0.5) {
balls[balls.length - 1].dx = -Ball.speed;
} else {
balls[balls.length - 1].dx = Ball.speed;
}
if (Math.random() > 0.5) {
balls[balls.length - 1].dy = -Ball.speed;
} else {
balls[balls.length - 1].dy = Ball.speed;
}
}
}