wobblyhead hg > minor_monsters
annotate game/js/game.js @ 12:dc314bfbb1cf
minor map adjustment
| author | ark |
|---|---|
| date | 2026-10-19 20:41 +0200 |
| parents | a77857594150 |
| children | 8df95050fc8b |
| 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 | |
| 11 | 159 // a press on the map at map position x, y. hitRadius is how close the |
| 160 // press must be to the preview to count as a press on it. returns | |
| 161 // "preview" for a press on the preview, "place" for any other press | |
| 162 // while a shop tower is chosen, and null when the press selected or | |
| 163 // deselected a built tower. | |
| 164 Game.prototype.pressMap = function (x, y, hitRadius) { | |
| 165 var onPreview = this.preview && Math.hypot(this.preview.x - x, this.preview.y - y) <= hitRadius; | |
| 1 | 166 var t = this.towerAt(x, y); |
|
8
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
167 this.sellArmed = false; |
| 11 | 168 if (t && !onPreview) { |
| 1 | 169 this.shop = null; |
| 170 this.preview = null; | |
| 171 this.selected = t === this.selected ? null : t; | |
| 11 | 172 return null; |
| 1 | 173 } |
| 174 this.selected = null; | |
| 11 | 175 if (!this.shop) return null; |
| 176 return onPreview ? "preview" : "place"; | |
| 177 }; | |
| 178 | |
| 179 // move the preview to the nearest legal position to map position x, y. | |
| 180 // where there is none, the preview stays where it was. returns false | |
| 181 // when no legal position was found. | |
| 182 Game.prototype.movePreview = function (x, y) { | |
| 183 if (!this.shop) return false; | |
| 184 var pv = this.map.snap(x, y, this.towers); | |
| 185 if (!pv) return false; | |
| 186 if (this.preview && this.preview.x === pv.x && this.preview.y === pv.y) return true; | |
| 6 | 187 // t0 draws spikes only towards the path, judged by its top level range |
| 188 var def = cfg.tower[this.shop]; | |
| 189 if (def.projectile_speed === 0) { | |
| 11 | 190 pv.sides = this.map.facingSides(pv.x, pv.y, def.range[def.range.length - 1]); |
| 6 | 191 } |
| 11 | 192 this.preview = pv; |
| 6 | 193 return true; |
| 1 | 194 }; |
| 195 | |
| 11 | 196 // build the shop tower at the preview. returns false when there is no |
| 197 // preview or the tower cannot be afforded. | |
| 1 | 198 Game.prototype.build = function () { |
| 11 | 199 if (!this.shop || !this.preview) return false; |
| 1 | 200 var cost = cfg.tower[this.shop].cost; |
| 201 if (!this.canAfford(cost)) return false; | |
| 202 this.pay(cost); | |
| 6 | 203 var pv = this.preview; |
| 204 var t = MM.tower.create(this.shop, pv.x, pv.y, pv.sides); | |
| 205 t.paid.push({ cost: cost, pause: this.pauseNumber() }); | |
| 206 this.towers.push(t); | |
| 4 | 207 MM.audio.play("tower_built"); |
| 1 | 208 this.shop = null; |
| 209 this.preview = null; | |
| 210 return true; | |
| 211 }; | |
| 212 | |
| 6 | 213 // upgrade the selected tower by one level |
| 214 Game.prototype.upgrade = function () { | |
| 215 var t = this.selected; | |
| 216 var cost = t && MM.tower.upgradeCost(t); | |
| 217 if (!cost || !this.canAfford(cost)) return; | |
| 218 this.pay(cost); | |
| 219 t.level++; | |
| 220 t.paid.push({ cost: cost, pause: this.pauseNumber() }); | |
| 221 MM.audio.play("tower_upgraded"); | |
| 222 }; | |
| 223 | |
|
8
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
224 // the first tap arms selling, the second sells |
| 6 | 225 Game.prototype.sell = function () { |
| 226 var t = this.selected; | |
| 227 if (!t) return; | |
|
8
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
228 if (!this.sellArmed) { |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
229 this.sellArmed = true; |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
230 return; |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
231 } |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
232 this.sellArmed = false; |
| 6 | 233 this.coins += MM.tower.refund(t, this.pauseNumber()); |
| 234 this.towers.splice(this.towers.indexOf(t), 1); | |
| 235 this.selected = null; | |
| 236 MM.audio.play("tower_sold"); | |
| 237 }; | |
| 238 | |
| 1 | 239 return Game; |
| 240 })(); |
