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