|
17
|
1 // game state and rules: coins, hearts, building, upgrading, selling, win
|
|
|
2 // and loss
|
|
|
3
|
|
|
4 window.MM = window.MM || {};
|
|
|
5
|
|
|
6 MM.Game = (function () {
|
|
|
7 "use strict";
|
|
|
8
|
|
|
9 var cfg = MM.config;
|
|
|
10 var dbg = cfg.debug;
|
|
|
11
|
|
|
12 // firstWave counts from 1
|
|
|
13 function Game(mapIndex, seed, firstWave) {
|
|
|
14 var mc = cfg.maps[mapIndex];
|
|
|
15 this.map = new MM.GameMap(mapIndex);
|
|
|
16 this.seed = seed;
|
|
|
17 this.random = MM.util.rng(seed);
|
|
|
18 this.coins = dbg.start_coins >= 0 ? dbg.start_coins : mc.starting_coins;
|
|
|
19 this.hearts = cfg.hearts.per_map;
|
|
|
20 this.monsters = [];
|
|
|
21 this.towers = [];
|
|
|
22 this.projectiles = [];
|
|
|
23 this.flights = []; // coins flying to the counter
|
|
|
24 this.puffs = []; // death puffs, {x, y, boss, t}
|
|
24
|
25 this.freezes = []; // t2 freeze splashes, {x, y, r, t}
|
|
17
|
26 this.waves = new MM.Waves(mapIndex, MM.util.clamp(firstWave - 1, 0, mc.waves.length - 1),
|
|
|
27 this.random);
|
|
|
28 this.time = 0;
|
|
|
29 this.paused = false;
|
|
|
30 this.speeds = cfg.controls.speeds.concat(dbg.extra_speeds);
|
|
|
31 this.speedIndex = 0;
|
|
|
32 this.result = null; // "won" or "lost"
|
|
|
33 this.splash = false; // splash screen shown over the paused map
|
|
24
|
34 this.help = false; // help popup shown over everything
|
|
|
35 this.helpResume = false; // the game ran when the help popup opened
|
|
17
|
36 // player selection: shop tower type, its snapped preview, a built tower
|
|
|
37 this.shop = null;
|
|
|
38 this.preview = null;
|
|
|
39 this.selected = null;
|
|
|
40 this.sellArmed = false; // sell was tapped once and waits for the second tap
|
|
|
41 }
|
|
|
42
|
|
|
43 Game.prototype.speed = function () {
|
|
|
44 return this.speeds[this.speedIndex];
|
|
|
45 };
|
|
|
46
|
|
|
47 Game.prototype.lastMap = function () {
|
|
|
48 return this.map.index === MM.maps.length - 1;
|
|
|
49 };
|
|
|
50
|
|
|
51 // towers cannot be built, upgraded or sold while paused, so pausing
|
|
|
52 // drops the shop choice, its preview and the tower selection
|
|
|
53 Game.prototype.togglePause = function () {
|
|
|
54 this.paused = !this.paused;
|
|
|
55 if (this.paused) {
|
|
|
56 this.shop = null;
|
|
|
57 this.preview = null;
|
|
|
58 this.selected = null;
|
|
|
59 this.sellArmed = false;
|
|
|
60 }
|
|
|
61 };
|
|
|
62
|
|
24
|
63 // the help popup pauses the game while it is shown. closing it lets a
|
|
|
64 // game that was running before go on
|
|
|
65 Game.prototype.openHelp = function () {
|
|
|
66 this.help = true;
|
|
|
67 this.helpResume = !this.paused;
|
|
|
68 if (!this.paused) this.togglePause();
|
|
|
69 };
|
|
|
70
|
|
|
71 Game.prototype.closeHelp = function () {
|
|
|
72 this.help = false;
|
|
|
73 if (this.helpResume) this.togglePause();
|
|
|
74 };
|
|
|
75
|
|
17
|
76 Game.prototype.canAfford = function (cost) {
|
|
|
77 return dbg.unlimited_coins || this.coins >= cost;
|
|
|
78 };
|
|
|
79
|
|
|
80 Game.prototype.pay = function (cost) {
|
|
|
81 if (!dbg.unlimited_coins) this.coins -= cost;
|
|
|
82 };
|
|
|
83
|
|
|
84 // one simulation step of dt seconds
|
|
|
85 Game.prototype.step = function (dt) {
|
|
|
86 if (this.result) return;
|
|
|
87 var self = this;
|
|
|
88 this.time += dt;
|
|
23
|
89 this.waves.update(dt, function (kind) {
|
|
17
|
90 var m = MM.monster.create(self, kind);
|
|
|
91 self.monsters.push(m);
|
|
|
92 MM.audio.play(m.boss ? "boss_enter" : "monster_enter");
|
|
|
93 });
|
|
|
94 this.monsters.forEach(function (m) {
|
|
|
95 if (m.alive && MM.monster.update(m, dt)) self.escape(m);
|
|
|
96 });
|
|
|
97 this.towers.forEach(function (t) { MM.tower.update(t, self, dt); });
|
|
|
98 this.projectiles.forEach(function (p) { MM.projectile.update(p, self, dt); });
|
|
|
99 this.monsters = this.monsters.filter(function (m) { return m.alive; });
|
|
|
100 this.projectiles = this.projectiles.filter(function (p) { return !p.done; });
|
|
|
101 this.flights.forEach(function (f) { f.t += dt; });
|
|
|
102 this.puffs.forEach(function (p) { p.t += dt; });
|
|
|
103 this.puffs = this.puffs.filter(function (p) { return p.t < cfg.animation.death_puff; });
|
|
24
|
104 this.freezes.forEach(function (f) { f.t += dt; });
|
|
|
105 this.freezes = this.freezes.filter(function (f) { return f.t < cfg.animation.freeze_splash; });
|
|
17
|
106 var flying = this.flights.length;
|
|
|
107 this.flights = this.flights.filter(function (f) { return f.t < cfg.coins.flight_time; });
|
|
|
108 if (this.flights.length < flying) MM.audio.play("coins_arrive");
|
|
|
109
|
|
|
110 // the end sound waits for the last exit or death sound
|
|
|
111 if (this.hearts <= 0) {
|
|
|
112 this.result = "lost";
|
|
|
113 MM.audio.play("map_lost", ["monster_exit", "boss_exit"]);
|
|
|
114 } else if (this.waves.phase === "done" && this.monsters.length === 0) {
|
|
|
115 this.result = "won";
|
|
|
116 MM.audio.play("map_won", ["monster_die", "boss_die"]);
|
|
|
117 }
|
|
|
118 // the simulation stops here; the coins in flight are counted already
|
|
|
119 if (this.result) {
|
|
|
120 this.flights = [];
|
|
|
121 this.puffs = [];
|
|
24
|
122 this.freezes = [];
|
|
17
|
123 }
|
|
|
124 };
|
|
|
125
|
|
|
126 // a monster reached the exit
|
|
|
127 Game.prototype.escape = function (m) {
|
|
|
128 m.alive = false;
|
|
|
129 MM.audio.play(m.boss ? "boss_exit" : "monster_exit");
|
|
|
130 if (!dbg.no_heart_loss) {
|
|
|
131 this.hearts = Math.max(0, this.hearts - (m.boss ? cfg.hearts.boss : cfg.hearts.monster));
|
|
|
132 }
|
|
|
133 };
|
|
|
134
|
|
24
|
135 // a freeze splash of radius r at x, y: slows every monster whose hit
|
|
|
136 // area it touches to factor of its speed for time seconds
|
|
|
137 Game.prototype.freeze = function (x, y, r, factor, time) {
|
|
|
138 this.freezes.push({ x: x, y: y, r: r, t: 0 });
|
|
|
139 this.monsters.forEach(function (m) {
|
|
|
140 if (m.alive && Math.hypot(m.x - x, m.y - y) <= r + m.hitRadius) {
|
|
|
141 MM.monster.slow(m, factor, time);
|
|
|
142 }
|
|
|
143 });
|
|
|
144 };
|
|
|
145
|
|
17
|
146 Game.prototype.damage = function (m, amount) {
|
|
|
147 if (!m.alive) return;
|
|
|
148 m.hp -= amount;
|
|
|
149 if (m.hp > 0) {
|
|
|
150 MM.audio.play("monster_hit");
|
|
|
151 return;
|
|
|
152 }
|
|
|
153 m.alive = false;
|
|
|
154 MM.audio.play(m.boss ? "boss_die" : "monster_die");
|
|
|
155 this.puffs.push({ x: m.x, y: m.y, boss: m.boss, t: 0 });
|
|
|
156 // coins count at once; the flying coins only show it (balance.md)
|
|
|
157 this.coins += m.coins;
|
|
|
158 var n = Math.min(m.coins, cfg.coins.max_sprites);
|
|
|
159 for (var i = 0; i < n; i++) {
|
|
23
|
160 this.flight(m.x + (this.random() - 0.5) * m.w,
|
|
|
161 m.y - m.h / 2 + (this.random() - 0.5) * m.h / 2, i);
|
|
17
|
162 }
|
|
|
163 };
|
|
|
164
|
|
23
|
165 // the i-th coin of a group flying from (x, y) to the counter
|
|
|
166 Game.prototype.flight = function (x, y, i) {
|
|
|
167 this.flights.push({
|
|
|
168 x: x,
|
|
|
169 y: y,
|
|
|
170 sprite: "c" + Math.floor(this.random() * 4),
|
|
|
171 // stagger the coins a little
|
|
|
172 t: -i * 0.05
|
|
|
173 });
|
|
|
174 };
|
|
|
175
|
|
|
176 // the bonus coins count at once and fly from the exits, taking turns
|
|
|
177 // if there are several
|
|
17
|
178 Game.prototype.startEarly = function () {
|
|
|
179 var bonus = this.waves.startEarly();
|
|
23
|
180 if (bonus <= 0) return;
|
|
17
|
181 this.coins += bonus;
|
|
23
|
182 MM.audio.play("early_bonus");
|
|
|
183 var exits = this.map.data.exits;
|
|
|
184 var n = exits.length ? Math.min(bonus, cfg.coins.max_sprites) : 0;
|
|
|
185 for (var i = 0; i < n; i++) {
|
|
|
186 var e = exits[i % exits.length];
|
|
|
187 this.flight(e[0] + (this.random() - 0.5) * e[2], e[1] + (this.random() - 0.5) * e[2], i);
|
|
|
188 }
|
|
17
|
189 };
|
|
|
190
|
|
|
191 Game.prototype.towerAt = function (x, y) {
|
|
|
192 var r = cfg.placement.footprint_radius;
|
|
|
193 for (var i = 0; i < this.towers.length; i++) {
|
|
|
194 var t = this.towers[i];
|
|
|
195 if (Math.hypot(t.x - x, t.y - y) <= r) return t;
|
|
|
196 }
|
|
|
197 return null;
|
|
|
198 };
|
|
|
199
|
|
|
200 Game.prototype.chooseShop = function (type) {
|
|
|
201 this.selected = null;
|
|
|
202 this.preview = null;
|
|
|
203 this.shop = this.shop === type ? null : type;
|
|
|
204 };
|
|
|
205
|
|
|
206 // a press on the map at map position x, y. hitRadius is how close the
|
|
|
207 // press must be to the preview to count as a press on it. returns
|
|
|
208 // "preview" for a press on the preview, "place" for any other press
|
|
|
209 // while a shop tower is chosen, and null when the press selected or
|
|
|
210 // deselected a built tower.
|
|
|
211 Game.prototype.pressMap = function (x, y, hitRadius) {
|
|
|
212 var onPreview = this.preview && Math.hypot(this.preview.x - x, this.preview.y - y) <= hitRadius;
|
|
|
213 var t = this.towerAt(x, y);
|
|
|
214 this.sellArmed = false;
|
|
|
215 if (t && !onPreview) {
|
|
|
216 this.shop = null;
|
|
|
217 this.preview = null;
|
|
|
218 this.selected = t === this.selected ? null : t;
|
|
|
219 return null;
|
|
|
220 }
|
|
|
221 this.selected = null;
|
|
|
222 if (!this.shop) return null;
|
|
|
223 return onPreview ? "preview" : "place";
|
|
|
224 };
|
|
|
225
|
|
|
226 // move the preview to the nearest legal position to map position x, y.
|
|
|
227 // where there is none, the preview stays where it was. returns false
|
|
|
228 // when no legal position was found.
|
|
|
229 Game.prototype.movePreview = function (x, y) {
|
|
|
230 if (!this.shop) return false;
|
|
|
231 var pv = this.map.snap(x, y, this.towers);
|
|
|
232 if (!pv) return false;
|
|
|
233 if (this.preview && this.preview.x === pv.x && this.preview.y === pv.y) return true;
|
|
|
234 // t0 draws spikes only towards the path, judged by its top level range
|
|
|
235 var def = cfg.tower[this.shop];
|
|
|
236 if (def.projectile_speed === 0) {
|
|
|
237 pv.sides = this.map.facingSides(pv.x, pv.y, def.range[def.range.length - 1]);
|
|
|
238 }
|
|
|
239 this.preview = pv;
|
|
|
240 return true;
|
|
|
241 };
|
|
|
242
|
|
|
243 // build the shop tower at the preview. returns false when there is no
|
|
|
244 // preview or the tower cannot be afforded.
|
|
|
245 Game.prototype.build = function () {
|
|
|
246 if (!this.shop || !this.preview) return false;
|
|
|
247 var cost = cfg.tower[this.shop].cost[0];
|
|
|
248 if (!this.canAfford(cost)) return false;
|
|
|
249 this.pay(cost);
|
|
|
250 var pv = this.preview;
|
|
|
251 var t = MM.tower.create(this.shop, pv.x, pv.y, pv.sides);
|
|
|
252 t.paid += cost;
|
|
|
253 this.towers.push(t);
|
|
|
254 MM.audio.play("tower_built");
|
|
|
255 this.shop = null;
|
|
|
256 this.preview = null;
|
|
|
257 return true;
|
|
|
258 };
|
|
|
259
|
|
|
260 // upgrade the selected tower by one level
|
|
|
261 Game.prototype.upgrade = function () {
|
|
|
262 var t = this.selected;
|
|
|
263 var cost = t && MM.tower.upgradeCost(t);
|
|
|
264 if (!cost || !this.canAfford(cost)) return;
|
|
|
265 this.pay(cost);
|
|
|
266 t.level++;
|
|
|
267 t.paid += cost;
|
|
|
268 MM.audio.play("tower_upgraded");
|
|
|
269 };
|
|
|
270
|
|
|
271 // the first tap arms selling, the second sells
|
|
|
272 Game.prototype.sell = function () {
|
|
|
273 var t = this.selected;
|
|
|
274 if (!t) return;
|
|
|
275 if (!this.sellArmed) {
|
|
|
276 this.sellArmed = true;
|
|
|
277 return;
|
|
|
278 }
|
|
|
279 this.sellArmed = false;
|
|
|
280 this.coins += MM.tower.refund(t);
|
|
|
281 this.towers.splice(this.towers.indexOf(t), 1);
|
|
|
282 this.selected = null;
|
|
|
283 MM.audio.play("tower_sold");
|
|
|
284 };
|
|
|
285
|
|
|
286 return Game;
|
|
|
287 })();
|