wobblyhead hg > minor_monsters
comparison game/js/game.js @ 6:910b26feca76
full game flow
| author | ark |
|---|---|
| date | 2026-10-19 01:05 +0200 |
| parents | 2d46729a022e |
| children | 2c4fed8baed9 |
comparison
equal
deleted
inserted
replaced
| 5:67bf3c505145 | 6:910b26feca76 |
|---|---|
| 1 // game state and rules: coins, hearts, building, win and loss | 1 // game state and rules: coins, hearts, building, upgrading, selling, win |
| 2 // and loss | |
| 2 | 3 |
| 3 window.MM = window.MM || {}; | 4 window.MM = window.MM || {}; |
| 4 | 5 |
| 5 MM.Game = (function () { | 6 MM.Game = (function () { |
| 6 "use strict"; | 7 "use strict"; |
| 7 | 8 |
| 8 var cfg = MM.config; | 9 var cfg = MM.config; |
| 9 var dbg = cfg.debug; | 10 var dbg = cfg.debug; |
| 10 | 11 |
| 11 function Game(mapIndex, seed) { | 12 // firstWave counts from 1 |
| 13 function Game(mapIndex, seed, firstWave) { | |
| 12 var mc = cfg.maps[mapIndex]; | 14 var mc = cfg.maps[mapIndex]; |
| 13 this.map = new MM.GameMap(mapIndex); | 15 this.map = new MM.GameMap(mapIndex); |
| 14 this.seed = seed; | 16 this.seed = seed; |
| 15 this.random = MM.util.rng(seed); | 17 this.random = MM.util.rng(seed); |
| 16 this.coins = dbg.start_coins >= 0 ? dbg.start_coins : mc.starting_coins; | 18 this.coins = dbg.start_coins >= 0 ? dbg.start_coins : mc.starting_coins; |
| 17 this.hearts = cfg.hearts.per_map; | 19 this.hearts = cfg.hearts.per_map; |
| 18 this.monsters = []; | 20 this.monsters = []; |
| 19 this.towers = []; | 21 this.towers = []; |
| 20 this.projectiles = []; | 22 this.projectiles = []; |
| 21 this.flights = []; // coins flying to the counter | 23 this.flights = []; // coins flying to the counter |
| 22 this.waves = new MM.Waves(mapIndex, MM.util.clamp(dbg.start_wave - 1, 0, mc.waves.length - 1)); | 24 this.waves = new MM.Waves(mapIndex, MM.util.clamp(firstWave - 1, 0, mc.waves.length - 1)); |
| 23 this.time = 0; | 25 this.time = 0; |
| 24 this.paused = false; | 26 this.paused = false; |
| 27 this.pauses = 0; // number of pauses so far, for the sell refund | |
| 25 this.speeds = cfg.controls.speeds.concat(dbg.extra_speeds); | 28 this.speeds = cfg.controls.speeds.concat(dbg.extra_speeds); |
| 26 this.speedIndex = 0; | 29 this.speedIndex = 0; |
| 27 this.result = null; // "won" or "lost" | 30 this.result = null; // "won" or "lost" |
| 31 this.splash = false; // splash screen shown over the paused map | |
| 28 // player selection: shop tower type, its snapped preview, a built tower | 32 // player selection: shop tower type, its snapped preview, a built tower |
| 29 this.shop = null; | 33 this.shop = null; |
| 30 this.preview = null; | 34 this.preview = null; |
| 31 this.selected = null; | 35 this.selected = null; |
| 32 } | 36 } |
| 33 | 37 |
| 34 Game.prototype.speed = function () { | 38 Game.prototype.speed = function () { |
| 35 return this.speeds[this.speedIndex]; | 39 return this.speeds[this.speedIndex]; |
| 40 }; | |
| 41 | |
| 42 Game.prototype.lastMap = function () { | |
| 43 return this.map.index === MM.maps.length - 1; | |
| 44 }; | |
| 45 | |
| 46 Game.prototype.togglePause = function () { | |
| 47 this.paused = !this.paused; | |
| 48 if (this.paused) this.pauses++; | |
| 49 }; | |
| 50 | |
| 51 // number of the current pause, 0 while the game runs | |
| 52 Game.prototype.pauseNumber = function () { | |
| 53 return this.paused ? this.pauses : 0; | |
| 36 }; | 54 }; |
| 37 | 55 |
| 38 Game.prototype.canAfford = function (cost) { | 56 Game.prototype.canAfford = function (cost) { |
| 39 return dbg.unlimited_coins || this.coins >= cost; | 57 return dbg.unlimited_coins || this.coins >= cost; |
| 40 }; | 58 }; |
| 145 if (!this.shop) return true; | 163 if (!this.shop) return true; |
| 146 if (this.preview && Math.hypot(this.preview.x - x, this.preview.y - y) <= hitRadius) { | 164 if (this.preview && Math.hypot(this.preview.x - x, this.preview.y - y) <= hitRadius) { |
| 147 return this.build(); | 165 return this.build(); |
| 148 } | 166 } |
| 149 this.preview = this.map.snap(x, y, this.towers); | 167 this.preview = this.map.snap(x, y, this.towers); |
| 150 return this.preview !== null; | 168 if (!this.preview) return false; |
| 169 // t0 draws spikes only towards the path, judged by its top level range | |
| 170 var def = cfg.tower[this.shop]; | |
| 171 if (def.projectile_speed === 0) { | |
| 172 this.preview.sides = this.map.facingSides(this.preview.x, this.preview.y, | |
| 173 def.range[def.range.length - 1]); | |
| 174 } | |
| 175 return true; | |
| 151 }; | 176 }; |
| 152 | 177 |
| 153 Game.prototype.build = function () { | 178 Game.prototype.build = function () { |
| 154 var cost = cfg.tower[this.shop].cost; | 179 var cost = cfg.tower[this.shop].cost; |
| 155 if (!this.canAfford(cost)) return false; | 180 if (!this.canAfford(cost)) return false; |
| 156 this.pay(cost); | 181 this.pay(cost); |
| 157 this.towers.push(MM.tower.create(this.shop, this.preview.x, this.preview.y)); | 182 var pv = this.preview; |
| 183 var t = MM.tower.create(this.shop, pv.x, pv.y, pv.sides); | |
| 184 t.paid.push({ cost: cost, pause: this.pauseNumber() }); | |
| 185 this.towers.push(t); | |
| 158 MM.audio.play("tower_built"); | 186 MM.audio.play("tower_built"); |
| 159 this.shop = null; | 187 this.shop = null; |
| 160 this.preview = null; | 188 this.preview = null; |
| 161 return true; | 189 return true; |
| 162 }; | 190 }; |
| 163 | 191 |
| 192 // upgrade the selected tower by one level | |
| 193 Game.prototype.upgrade = function () { | |
| 194 var t = this.selected; | |
| 195 var cost = t && MM.tower.upgradeCost(t); | |
| 196 if (!cost || !this.canAfford(cost)) return; | |
| 197 this.pay(cost); | |
| 198 t.level++; | |
| 199 t.paid.push({ cost: cost, pause: this.pauseNumber() }); | |
| 200 MM.audio.play("tower_upgraded"); | |
| 201 }; | |
| 202 | |
| 203 Game.prototype.sell = function () { | |
| 204 var t = this.selected; | |
| 205 if (!t) return; | |
| 206 this.coins += MM.tower.refund(t, this.pauseNumber()); | |
| 207 this.towers.splice(this.towers.indexOf(t), 1); | |
| 208 this.selected = null; | |
| 209 MM.audio.play("tower_sold"); | |
| 210 }; | |
| 211 | |
| 164 return Game; | 212 return Game; |
| 165 })(); | 213 })(); |
