wobblyhead hg > minor_monsters
comparison game/js/game.js @ 17:fe680946164b
reorganised repo
| author | ark |
|---|---|
| date | 2026-10-20 00:12 +0200 |
| parents | |
| children | 2f555a54c332 |
comparison
equal
deleted
inserted
replaced
| 16:30bd10284fd8 | 17:fe680946164b |
|---|---|
| 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; | |
| 73 this.waves.update(dt, this.monsters.length, function (kind) { | |
| 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++) { | |
| 130 this.flights.push({ | |
| 131 x: m.x + (this.random() - 0.5) * m.w, | |
| 132 y: m.y - m.h / 2 + (this.random() - 0.5) * m.h / 2, | |
| 133 sprite: "c" + Math.floor(this.random() * 4), | |
| 134 // stagger the coins a little | |
| 135 t: -i * 0.05 | |
| 136 }); | |
| 137 } | |
| 138 }; | |
| 139 | |
| 140 Game.prototype.startEarly = function () { | |
| 141 var bonus = this.waves.startEarly(); | |
| 142 this.coins += bonus; | |
| 143 if (bonus > 0) MM.audio.play("early_bonus"); | |
| 144 }; | |
| 145 | |
| 146 Game.prototype.towerAt = function (x, y) { | |
| 147 var r = cfg.placement.footprint_radius; | |
| 148 for (var i = 0; i < this.towers.length; i++) { | |
| 149 var t = this.towers[i]; | |
| 150 if (Math.hypot(t.x - x, t.y - y) <= r) return t; | |
| 151 } | |
| 152 return null; | |
| 153 }; | |
| 154 | |
| 155 Game.prototype.chooseShop = function (type) { | |
| 156 this.selected = null; | |
| 157 this.preview = null; | |
| 158 this.shop = this.shop === type ? null : type; | |
| 159 }; | |
| 160 | |
| 161 // a press on the map at map position x, y. hitRadius is how close the | |
| 162 // press must be to the preview to count as a press on it. returns | |
| 163 // "preview" for a press on the preview, "place" for any other press | |
| 164 // while a shop tower is chosen, and null when the press selected or | |
| 165 // deselected a built tower. | |
| 166 Game.prototype.pressMap = function (x, y, hitRadius) { | |
| 167 var onPreview = this.preview && Math.hypot(this.preview.x - x, this.preview.y - y) <= hitRadius; | |
| 168 var t = this.towerAt(x, y); | |
| 169 this.sellArmed = false; | |
| 170 if (t && !onPreview) { | |
| 171 this.shop = null; | |
| 172 this.preview = null; | |
| 173 this.selected = t === this.selected ? null : t; | |
| 174 return null; | |
| 175 } | |
| 176 this.selected = null; | |
| 177 if (!this.shop) return null; | |
| 178 return onPreview ? "preview" : "place"; | |
| 179 }; | |
| 180 | |
| 181 // move the preview to the nearest legal position to map position x, y. | |
| 182 // where there is none, the preview stays where it was. returns false | |
| 183 // when no legal position was found. | |
| 184 Game.prototype.movePreview = function (x, y) { | |
| 185 if (!this.shop) return false; | |
| 186 var pv = this.map.snap(x, y, this.towers); | |
| 187 if (!pv) return false; | |
| 188 if (this.preview && this.preview.x === pv.x && this.preview.y === pv.y) return true; | |
| 189 // t0 draws spikes only towards the path, judged by its top level range | |
| 190 var def = cfg.tower[this.shop]; | |
| 191 if (def.projectile_speed === 0) { | |
| 192 pv.sides = this.map.facingSides(pv.x, pv.y, def.range[def.range.length - 1]); | |
| 193 } | |
| 194 this.preview = pv; | |
| 195 return true; | |
| 196 }; | |
| 197 | |
| 198 // build the shop tower at the preview. returns false when there is no | |
| 199 // preview or the tower cannot be afforded. | |
| 200 Game.prototype.build = function () { | |
| 201 if (!this.shop || !this.preview) return false; | |
| 202 var cost = cfg.tower[this.shop].cost[0]; | |
| 203 if (!this.canAfford(cost)) return false; | |
| 204 this.pay(cost); | |
| 205 var pv = this.preview; | |
| 206 var t = MM.tower.create(this.shop, pv.x, pv.y, pv.sides); | |
| 207 t.paid += cost; | |
| 208 this.towers.push(t); | |
| 209 MM.audio.play("tower_built"); | |
| 210 this.shop = null; | |
| 211 this.preview = null; | |
| 212 return true; | |
| 213 }; | |
| 214 | |
| 215 // upgrade the selected tower by one level | |
| 216 Game.prototype.upgrade = function () { | |
| 217 var t = this.selected; | |
| 218 var cost = t && MM.tower.upgradeCost(t); | |
| 219 if (!cost || !this.canAfford(cost)) return; | |
| 220 this.pay(cost); | |
| 221 t.level++; | |
| 222 t.paid += cost; | |
| 223 MM.audio.play("tower_upgraded"); | |
| 224 }; | |
| 225 | |
| 226 // the first tap arms selling, the second sells | |
| 227 Game.prototype.sell = function () { | |
| 228 var t = this.selected; | |
| 229 if (!t) return; | |
| 230 if (!this.sellArmed) { | |
| 231 this.sellArmed = true; | |
| 232 return; | |
| 233 } | |
| 234 this.sellArmed = false; | |
| 235 this.coins += MM.tower.refund(t); | |
| 236 this.towers.splice(this.towers.indexOf(t), 1); | |
| 237 this.selected = null; | |
| 238 MM.audio.play("tower_sold"); | |
| 239 }; | |
| 240 | |
| 241 return Game; | |
| 242 })(); |
