|
6
|
1 // game state and rules: coins, hearts, building, upgrading, selling, win
|
|
|
2 // and loss
|
|
1
|
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
|
|
6
|
12 // firstWave counts from 1
|
|
|
13 function Game(mapIndex, seed, firstWave) {
|
|
1
|
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
|
|
6
|
24 this.waves = new MM.Waves(mapIndex, MM.util.clamp(firstWave - 1, 0, mc.waves.length - 1));
|
|
1
|
25 this.time = 0;
|
|
|
26 this.paused = false;
|
|
6
|
27 this.pauses = 0; // number of pauses so far, for the sell refund
|
|
1
|
28 this.speeds = cfg.controls.speeds.concat(dbg.extra_speeds);
|
|
|
29 this.speedIndex = 0;
|
|
|
30 this.result = null; // "won" or "lost"
|
|
6
|
31 this.splash = false; // splash screen shown over the paused map
|
|
1
|
32 // player selection: shop tower type, its snapped preview, a built tower
|
|
|
33 this.shop = null;
|
|
|
34 this.preview = null;
|
|
|
35 this.selected = null;
|
|
|
36 }
|
|
|
37
|
|
|
38 Game.prototype.speed = function () {
|
|
|
39 return this.speeds[this.speedIndex];
|
|
|
40 };
|
|
|
41
|
|
6
|
42 Game.prototype.lastMap = function () {
|
|
|
43 return this.map.index === MM.maps.length - 1;
|
|
|
44 };
|
|
|
45
|
|
|
46 Game.prototype.togglePause = function () {
|
|
|
47 this.paused = !this.paused;
|
|
|
48 if (this.paused) this.pauses++;
|
|
|
49 };
|
|
|
50
|
|
|
51 // number of the current pause, 0 while the game runs
|
|
|
52 Game.prototype.pauseNumber = function () {
|
|
|
53 return this.paused ? this.pauses : 0;
|
|
|
54 };
|
|
|
55
|
|
1
|
56 Game.prototype.canAfford = function (cost) {
|
|
|
57 return dbg.unlimited_coins || this.coins >= cost;
|
|
|
58 };
|
|
|
59
|
|
|
60 Game.prototype.pay = function (cost) {
|
|
|
61 if (!dbg.unlimited_coins) this.coins -= cost;
|
|
|
62 };
|
|
|
63
|
|
|
64 // one simulation step of dt seconds
|
|
|
65 Game.prototype.step = function (dt) {
|
|
|
66 if (this.result) return;
|
|
|
67 var self = this;
|
|
|
68 this.time += dt;
|
|
|
69 this.waves.update(dt, this.monsters.length, function (kind) {
|
|
4
|
70 var m = MM.monster.create(self, kind);
|
|
|
71 self.monsters.push(m);
|
|
|
72 MM.audio.play(m.boss ? "boss_enter" : "monster_enter");
|
|
1
|
73 });
|
|
|
74 this.monsters.forEach(function (m) {
|
|
|
75 if (m.alive && MM.monster.update(m, dt)) self.escape(m);
|
|
|
76 });
|
|
|
77 this.towers.forEach(function (t) { MM.tower.update(t, self, dt); });
|
|
|
78 this.projectiles.forEach(function (p) { MM.projectile.update(p, self, dt); });
|
|
|
79 this.monsters = this.monsters.filter(function (m) { return m.alive; });
|
|
|
80 this.projectiles = this.projectiles.filter(function (p) { return !p.done; });
|
|
|
81 this.flights.forEach(function (f) { f.t += dt; });
|
|
4
|
82 var flying = this.flights.length;
|
|
1
|
83 this.flights = this.flights.filter(function (f) { return f.t < cfg.coins.flight_time; });
|
|
4
|
84 if (this.flights.length < flying) MM.audio.play("coins_arrive");
|
|
1
|
85
|
|
4
|
86 // the end sound waits for the last exit or death sound
|
|
1
|
87 if (this.hearts <= 0) {
|
|
|
88 this.result = "lost";
|
|
4
|
89 MM.audio.play("map_lost", ["monster_exit", "boss_exit"]);
|
|
1
|
90 } else if (this.waves.phase === "done" && this.monsters.length === 0) {
|
|
|
91 this.result = "won";
|
|
4
|
92 MM.audio.play("map_won", ["monster_die", "boss_die"]);
|
|
1
|
93 }
|
|
|
94 // the simulation stops here; the coins in flight are counted already
|
|
|
95 if (this.result) this.flights = [];
|
|
|
96 };
|
|
|
97
|
|
|
98 // a monster reached the exit
|
|
|
99 Game.prototype.escape = function (m) {
|
|
|
100 m.alive = false;
|
|
4
|
101 MM.audio.play(m.boss ? "boss_exit" : "monster_exit");
|
|
1
|
102 if (!dbg.no_heart_loss) {
|
|
|
103 this.hearts = Math.max(0, this.hearts - (m.boss ? cfg.hearts.boss : cfg.hearts.monster));
|
|
|
104 }
|
|
|
105 };
|
|
|
106
|
|
|
107 Game.prototype.damage = function (m, amount) {
|
|
|
108 if (!m.alive) return;
|
|
|
109 m.hp -= amount;
|
|
4
|
110 if (m.hp > 0) {
|
|
|
111 MM.audio.play("monster_hit");
|
|
|
112 return;
|
|
|
113 }
|
|
1
|
114 m.alive = false;
|
|
4
|
115 MM.audio.play(m.boss ? "boss_die" : "monster_die");
|
|
1
|
116 // coins count at once; the flying coins only show it (balance.md)
|
|
|
117 this.coins += m.coins;
|
|
|
118 var n = Math.min(m.coins, cfg.coins.max_sprites);
|
|
|
119 for (var i = 0; i < n; i++) {
|
|
|
120 this.flights.push({
|
|
|
121 x: m.x + (this.random() - 0.5) * m.w,
|
|
|
122 y: m.y - m.h / 2 + (this.random() - 0.5) * m.h / 2,
|
|
|
123 sprite: "c" + Math.floor(this.random() * 4),
|
|
|
124 // stagger the coins a little
|
|
|
125 t: -i * 0.05
|
|
|
126 });
|
|
|
127 }
|
|
|
128 };
|
|
|
129
|
|
|
130 Game.prototype.startEarly = function () {
|
|
4
|
131 var bonus = this.waves.startEarly();
|
|
|
132 this.coins += bonus;
|
|
|
133 if (bonus > 0) MM.audio.play("early_bonus");
|
|
1
|
134 };
|
|
|
135
|
|
|
136 Game.prototype.towerAt = function (x, y) {
|
|
|
137 var r = cfg.placement.footprint_radius;
|
|
|
138 for (var i = 0; i < this.towers.length; i++) {
|
|
|
139 var t = this.towers[i];
|
|
|
140 if (Math.hypot(t.x - x, t.y - y) <= r) return t;
|
|
|
141 }
|
|
|
142 return null;
|
|
|
143 };
|
|
|
144
|
|
|
145 Game.prototype.chooseShop = function (type) {
|
|
|
146 this.selected = null;
|
|
|
147 this.preview = null;
|
|
|
148 this.shop = this.shop === type ? null : type;
|
|
|
149 };
|
|
|
150
|
|
|
151 // a tap on the map at map position x, y. hitRadius is how close the tap
|
|
|
152 // must be to the preview to build. returns false when no legal position
|
|
|
153 // was found for a preview.
|
|
|
154 Game.prototype.tapMap = function (x, y, hitRadius) {
|
|
|
155 var t = this.towerAt(x, y);
|
|
|
156 if (t && !(this.preview && Math.hypot(this.preview.x - x, this.preview.y - y) <= hitRadius)) {
|
|
|
157 this.shop = null;
|
|
|
158 this.preview = null;
|
|
|
159 this.selected = t === this.selected ? null : t;
|
|
|
160 return true;
|
|
|
161 }
|
|
|
162 this.selected = null;
|
|
|
163 if (!this.shop) return true;
|
|
|
164 if (this.preview && Math.hypot(this.preview.x - x, this.preview.y - y) <= hitRadius) {
|
|
|
165 return this.build();
|
|
|
166 }
|
|
|
167 this.preview = this.map.snap(x, y, this.towers);
|
|
6
|
168 if (!this.preview) return false;
|
|
|
169 // t0 draws spikes only towards the path, judged by its top level range
|
|
|
170 var def = cfg.tower[this.shop];
|
|
|
171 if (def.projectile_speed === 0) {
|
|
|
172 this.preview.sides = this.map.facingSides(this.preview.x, this.preview.y,
|
|
|
173 def.range[def.range.length - 1]);
|
|
|
174 }
|
|
|
175 return true;
|
|
1
|
176 };
|
|
|
177
|
|
|
178 Game.prototype.build = function () {
|
|
|
179 var cost = cfg.tower[this.shop].cost;
|
|
|
180 if (!this.canAfford(cost)) return false;
|
|
|
181 this.pay(cost);
|
|
6
|
182 var pv = this.preview;
|
|
|
183 var t = MM.tower.create(this.shop, pv.x, pv.y, pv.sides);
|
|
|
184 t.paid.push({ cost: cost, pause: this.pauseNumber() });
|
|
|
185 this.towers.push(t);
|
|
4
|
186 MM.audio.play("tower_built");
|
|
1
|
187 this.shop = null;
|
|
|
188 this.preview = null;
|
|
|
189 return true;
|
|
|
190 };
|
|
|
191
|
|
6
|
192 // upgrade the selected tower by one level
|
|
|
193 Game.prototype.upgrade = function () {
|
|
|
194 var t = this.selected;
|
|
|
195 var cost = t && MM.tower.upgradeCost(t);
|
|
|
196 if (!cost || !this.canAfford(cost)) return;
|
|
|
197 this.pay(cost);
|
|
|
198 t.level++;
|
|
|
199 t.paid.push({ cost: cost, pause: this.pauseNumber() });
|
|
|
200 MM.audio.play("tower_upgraded");
|
|
|
201 };
|
|
|
202
|
|
|
203 Game.prototype.sell = function () {
|
|
|
204 var t = this.selected;
|
|
|
205 if (!t) return;
|
|
|
206 this.coins += MM.tower.refund(t, this.pauseNumber());
|
|
|
207 this.towers.splice(this.towers.indexOf(t), 1);
|
|
|
208 this.selected = null;
|
|
|
209 MM.audio.play("tower_sold");
|
|
|
210 };
|
|
|
211
|
|
1
|
212 return Game;
|
|
|
213 })();
|