wobblyhead hg > minor_monsters
diff game/js/game.js @ 6:910b26feca76
full game flow
| author | ark |
|---|---|
| date | 2026-10-19 01:05 +0200 |
| parents | 2d46729a022e |
| children | 2c4fed8baed9 |
line wrap: on
line diff
--- a/game/js/game.js Sun Oct 18 22:50:24 2026 +0200 +++ b/game/js/game.js Mon Oct 19 01:05:29 2026 +0200 @@ -1,4 +1,5 @@ -// game state and rules: coins, hearts, building, win and loss +// game state and rules: coins, hearts, building, upgrading, selling, win +// and loss window.MM = window.MM || {}; @@ -8,7 +9,8 @@ var cfg = MM.config; var dbg = cfg.debug; - function Game(mapIndex, seed) { + // firstWave counts from 1 + function Game(mapIndex, seed, firstWave) { var mc = cfg.maps[mapIndex]; this.map = new MM.GameMap(mapIndex); this.seed = seed; @@ -19,12 +21,14 @@ this.towers = []; this.projectiles = []; this.flights = []; // coins flying to the counter - this.waves = new MM.Waves(mapIndex, MM.util.clamp(dbg.start_wave - 1, 0, mc.waves.length - 1)); + this.waves = new MM.Waves(mapIndex, MM.util.clamp(firstWave - 1, 0, mc.waves.length - 1)); this.time = 0; this.paused = false; + this.pauses = 0; // number of pauses so far, for the sell refund this.speeds = cfg.controls.speeds.concat(dbg.extra_speeds); this.speedIndex = 0; this.result = null; // "won" or "lost" + this.splash = false; // splash screen shown over the paused map // player selection: shop tower type, its snapped preview, a built tower this.shop = null; this.preview = null; @@ -35,6 +39,20 @@ return this.speeds[this.speedIndex]; }; + Game.prototype.lastMap = function () { + return this.map.index === MM.maps.length - 1; + }; + + Game.prototype.togglePause = function () { + this.paused = !this.paused; + if (this.paused) this.pauses++; + }; + + // number of the current pause, 0 while the game runs + Game.prototype.pauseNumber = function () { + return this.paused ? this.pauses : 0; + }; + Game.prototype.canAfford = function (cost) { return dbg.unlimited_coins || this.coins >= cost; }; @@ -147,19 +165,49 @@ return this.build(); } this.preview = this.map.snap(x, y, this.towers); - return this.preview !== null; + if (!this.preview) return false; + // t0 draws spikes only towards the path, judged by its top level range + var def = cfg.tower[this.shop]; + if (def.projectile_speed === 0) { + this.preview.sides = this.map.facingSides(this.preview.x, this.preview.y, + def.range[def.range.length - 1]); + } + return true; }; Game.prototype.build = function () { var cost = cfg.tower[this.shop].cost; if (!this.canAfford(cost)) return false; this.pay(cost); - this.towers.push(MM.tower.create(this.shop, this.preview.x, this.preview.y)); + var pv = this.preview; + var t = MM.tower.create(this.shop, pv.x, pv.y, pv.sides); + t.paid.push({ cost: cost, pause: this.pauseNumber() }); + this.towers.push(t); MM.audio.play("tower_built"); this.shop = null; this.preview = null; return true; }; + // upgrade the selected tower by one level + Game.prototype.upgrade = function () { + var t = this.selected; + var cost = t && MM.tower.upgradeCost(t); + if (!cost || !this.canAfford(cost)) return; + this.pay(cost); + t.level++; + t.paid.push({ cost: cost, pause: this.pauseNumber() }); + MM.audio.play("tower_upgraded"); + }; + + Game.prototype.sell = function () { + var t = this.selected; + if (!t) return; + this.coins += MM.tower.refund(t, this.pauseNumber()); + this.towers.splice(this.towers.indexOf(t), 1); + this.selected = null; + MM.audio.play("tower_sold"); + }; + return Game; })();
