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