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