-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
78 lines (64 loc) · 2.03 KB
/
script.js
File metadata and controls
78 lines (64 loc) · 2.03 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
/**
* Main game script
*/
// Requires ./Bomb.js
function Game() {
var isLost = false;
var score = 0;
var incrementScore = (function () {
const scoreEl = document.getElementById('score');
return function () {
score++;
scoreEl.textContent = score;
}
})();
function loose() {
isLost = true;
}
function init() {
var canvas = document.getElementById('canvas');
canvas.addEventListener('exploded', loose, true);
canvas.addEventListener('died', incrementScore, true);
}
var update = (function () {
var canvas = document.getElementById('canvas');
var lastTime = window.performance.now();
function addBomb() {
var newLifetime = 2 + Math.floor(Math.random() * 4);
var newBomb = new Bomb(newLifetime);
canvas.appendChild(newBomb.element);
}
return function (tFrame) {
var elapsedSeconds = Math.floor((tFrame - lastTime) / 1000);
for (let second = 0; second < elapsedSeconds; second++) {
// update all bombs
for (bomb of canvas.children) {
bomb.tick();
}
// add a new bomb
const LOG_BASE = 3;
let newBombsCount = Math.floor(Math.log(score + LOG_BASE) / Math.log(LOG_BASE))
for (let i = 0; i < newBombsCount; i++) {
addBomb();
}
lastTime = tFrame;
}
}
})();
// Game loop
this.main = function () {
init();
function loop(tFrame) {
if (isLost) {
let youLooseScreen = document.getElementById('you-loose');
youLooseScreen.style.visibility = 'visible';
} else {
window.requestAnimationFrame(loop);
update(tFrame);
}
}
window.requestAnimationFrame(loop);
};
}
const game = new Game();
game.main();