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