-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgame.js
133 lines (88 loc) · 2.9 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
var camera,
level,
hero,
RAFID, // Request animation frame ID.
music,
// Those are used in various methods to define the needed animation frame.
currentTime,
isOdd,
gameTimers = new Timers(),
levelCount = Object.keys(Level.LEVELS).length - 1,
delay = 700;
loadImages(SPRITES, function () {
function tickLevel() {
currentTime = Date.now();
isOdd = currentTime % 200 < 100;
camera.clear();
level.update(hero.x);
level.iterateUnits(false, false, function (unit) {
// General logic, like gravitation, applies to all units.
unit.tick();
// Own logic applies only to live units.
!unit.dead && unit.ownTick && unit.ownTick();
camera.draw(unit);
});
level.iterateAttacks();
camera.drawAttacks();
camera.drawGlitches();
level.updateText();
camera.postEffects();
RAFID = window.requestAnimationFrame(tickLevel);
}
// An abridged version of `tickLevel()`.
function tickMenu() {
currentTime = Date.now();
isOdd = currentTime % 200 < 100;
camera.clear();
level.iterateUnits(false, false, function (unit) {
unit.tick();
camera.draw(unit);
});
level.updateText();
camera.postEffects();
RAFID = window.requestAnimationFrame(tickMenu);
}
function nextLevel(lvl) {
// Take all th precautions for there must be the ultimate stop before go to a different level or the menu.
RAFID && window.cancelAnimationFrame(RAFID);
gameTimers.nullifyTimers();
camera.reset();
camera.unfade();
level = new Level(lvl, function () {
if (lvl < levelCount) {
camera.fade();
gameTimers.setTimeout(nextLevel.bind(null, lvl + 1), delay);
} else {
camera.fade();
gameTimers.setTimeout(menu, delay);
}
}, function () {
camera.fade();
gameTimers.setTimeout(menu, delay);
});
hero = new Hero(START_X);
level.addUnit(hero);
camera.focusOn(hero);
window.requestAnimationFrame(tickLevel);
}
function menu() {
// The same precautions here.
RAFID && window.cancelAnimationFrame(RAFID);
gameTimers.nullifyTimers();
camera.reset();
camera.unfade();
level = new Menu(function (lvl) {
camera.fade();
gameTimers.setTimeout(function () {
nextLevel(lvl);
camera.unfade();
}, delay);
});
camera.focusOn(level.units[1]); // There is a hard-coded ID in Menu constructor.
hero = null;
window.requestAnimationFrame(tickMenu);
}
camera = new Camera();
music = new Music();
menu();
});