-
Notifications
You must be signed in to change notification settings - Fork 21
/
squirts.js
88 lines (75 loc) · 3.14 KB
/
squirts.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
window.addEventListener('load', function() {
var canvas = document.querySelector('canvas');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
canvas.style.width = window.innerWidth + 'px';
canvas.style.height = window.innerHeight + 'px';
var Squirts = {
game: new GameLoop({
world: new World({
canvas : canvas,
extents : new Rectangle(new Vector2d(0, 0), 2000, 2000),
viewport : new Rectangle(new Vector2d(0, 0), canvas.width, canvas.height),
backgroundEl : document.documentElement
})
})
};
Squirts.game.world.clear();
var buttons = document.querySelectorAll('[data-action=startGame]');
var startScreen = document.querySelector('#startScreen');
var replayScreen = document.querySelector('#replayScreen');
var score = document.querySelector('#score');
for(var i = 0; i < buttons.length; i++) {
buttons[i].addEventListener('click', function(e) {
e.preventDefault();
if (Squirts.game.running) {
Squirts.game.stop();
}
Squirts.game.world.generate({
count : 160,
radius : 20,
speed : 80
});
Squirts.game.start();
score.style.display = 'block';
startScreen.style.display = 'none';
replayScreen.style.display = 'none';
}, false);
}
// Listen for the `playerDied` event to show the end screen
Squirts.game.world.on('playerDied', function() {
Squirts.game.stop();
replayScreen.style.display = 'block';
score.style.display = 'none';
});
// If any blob gets absorbed, we need to update the scoreboard
Squirts.game.world.on('absorbed', function() {
var blobs = Squirts.game.world.blobs.length - 1;
score.querySelector('span').textContent = blobs;
// If there are no more blobs (besides the player), we need
// to show the end screen
if (blobs <= 0) {
Squirts.game.stop();
replayScreen.style.display = 'block';
score.style.display = 'none';
}
});
// If a player squirts a blob, it creates a new blob and we must
// update the scoreboard
Squirts.game.world.on('squirted', function() {
score.querySelector('span').textContent = Squirts.game.world.blobs.length - 1;
});
// Handle time acceleration keys to slow down or speed up time
// NOTE: this handler will be refactored into a separate Controller
// class that will issue commands to the game and the world.
document.body.addEventListener('keydown', function(ev) {
var key = String.fromCharCode(ev.keyCode);
if (key.toLowerCase() == 'a') {
Squirts.game.world.timeMultiplier = Math.max(0.125, Squirts.game.world.timeMultiplier / 2);
} else if (key.toLowerCase() == 's') {
Squirts.game.world.timeMultiplier = Math.min(8, Squirts.game.world.timeMultiplier * 2);
}
}, false);
// Export the game
this.Squirts = Squirts;
}, false);