wobblyhead hg > minor_monsters
diff game/js/game.js @ 17:fe680946164b
reorganised repo
| author | ark |
|---|---|
| date | 2026-10-20 00:12 +0200 |
| parents | |
| children | 2f555a54c332 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/game/js/game.js Tue Oct 20 00:12:36 2026 +0200 @@ -0,0 +1,242 @@ +// game state and rules: coins, hearts, building, upgrading, selling, win +// and loss + +window.MM = window.MM || {}; + +MM.Game = (function () { + "use strict"; + + var cfg = MM.config; + var dbg = cfg.debug; + + // firstWave counts from 1 + function Game(mapIndex, seed, firstWave) { + var mc = cfg.maps[mapIndex]; + this.map = new MM.GameMap(mapIndex); + this.seed = seed; + this.random = MM.util.rng(seed); + this.coins = dbg.start_coins >= 0 ? dbg.start_coins : mc.starting_coins; + this.hearts = cfg.hearts.per_map; + this.monsters = []; + this.towers = []; + this.projectiles = []; + this.flights = []; // coins flying to the counter + this.puffs = []; // death puffs, {x, y, boss, t} + this.waves = new MM.Waves(mapIndex, MM.util.clamp(firstWave - 1, 0, mc.waves.length - 1), + this.random); + this.time = 0; + this.paused = false; + 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; + this.selected = null; + this.sellArmed = false; // sell was tapped once and waits for the second tap + } + + Game.prototype.speed = function () { + return this.speeds[this.speedIndex]; + }; + + Game.prototype.lastMap = function () { + return this.map.index === MM.maps.length - 1; + }; + + // towers cannot be built, upgraded or sold while paused, so pausing + // drops the shop choice, its preview and the tower selection + Game.prototype.togglePause = function () { + this.paused = !this.paused; + if (this.paused) { + this.shop = null; + this.preview = null; + this.selected = null; + this.sellArmed = false; + } + }; + + Game.prototype.canAfford = function (cost) { + return dbg.unlimited_coins || this.coins >= cost; + }; + + Game.prototype.pay = function (cost) { + if (!dbg.unlimited_coins) this.coins -= cost; + }; + + // one simulation step of dt seconds + Game.prototype.step = function (dt) { + if (this.result) return; + var self = this; + this.time += dt; + this.waves.update(dt, this.monsters.length, function (kind) { + var m = MM.monster.create(self, kind); + self.monsters.push(m); + MM.audio.play(m.boss ? "boss_enter" : "monster_enter"); + }); + this.monsters.forEach(function (m) { + if (m.alive && MM.monster.update(m, dt)) self.escape(m); + }); + this.towers.forEach(function (t) { MM.tower.update(t, self, dt); }); + this.projectiles.forEach(function (p) { MM.projectile.update(p, self, dt); }); + this.monsters = this.monsters.filter(function (m) { return m.alive; }); + this.projectiles = this.projectiles.filter(function (p) { return !p.done; }); + this.flights.forEach(function (f) { f.t += dt; }); + this.puffs.forEach(function (p) { p.t += dt; }); + this.puffs = this.puffs.filter(function (p) { return p.t < cfg.animation.death_puff; }); + var flying = this.flights.length; + this.flights = this.flights.filter(function (f) { return f.t < cfg.coins.flight_time; }); + if (this.flights.length < flying) MM.audio.play("coins_arrive"); + + // the end sound waits for the last exit or death sound + if (this.hearts <= 0) { + this.result = "lost"; + MM.audio.play("map_lost", ["monster_exit", "boss_exit"]); + } else if (this.waves.phase === "done" && this.monsters.length === 0) { + this.result = "won"; + MM.audio.play("map_won", ["monster_die", "boss_die"]); + } + // the simulation stops here; the coins in flight are counted already + if (this.result) { + this.flights = []; + this.puffs = []; + } + }; + + // a monster reached the exit + Game.prototype.escape = function (m) { + m.alive = false; + MM.audio.play(m.boss ? "boss_exit" : "monster_exit"); + if (!dbg.no_heart_loss) { + this.hearts = Math.max(0, this.hearts - (m.boss ? cfg.hearts.boss : cfg.hearts.monster)); + } + }; + + Game.prototype.damage = function (m, amount) { + if (!m.alive) return; + m.hp -= amount; + if (m.hp > 0) { + MM.audio.play("monster_hit"); + return; + } + m.alive = false; + MM.audio.play(m.boss ? "boss_die" : "monster_die"); + this.puffs.push({ x: m.x, y: m.y, boss: m.boss, t: 0 }); + // coins count at once; the flying coins only show it (balance.md) + this.coins += m.coins; + var n = Math.min(m.coins, cfg.coins.max_sprites); + for (var i = 0; i < n; i++) { + this.flights.push({ + x: m.x + (this.random() - 0.5) * m.w, + y: m.y - m.h / 2 + (this.random() - 0.5) * m.h / 2, + sprite: "c" + Math.floor(this.random() * 4), + // stagger the coins a little + t: -i * 0.05 + }); + } + }; + + Game.prototype.startEarly = function () { + var bonus = this.waves.startEarly(); + this.coins += bonus; + if (bonus > 0) MM.audio.play("early_bonus"); + }; + + Game.prototype.towerAt = function (x, y) { + var r = cfg.placement.footprint_radius; + for (var i = 0; i < this.towers.length; i++) { + var t = this.towers[i]; + if (Math.hypot(t.x - x, t.y - y) <= r) return t; + } + return null; + }; + + Game.prototype.chooseShop = function (type) { + this.selected = null; + this.preview = null; + this.shop = this.shop === type ? null : type; + }; + + // a press on the map at map position x, y. hitRadius is how close the + // press must be to the preview to count as a press on it. returns + // "preview" for a press on the preview, "place" for any other press + // while a shop tower is chosen, and null when the press selected or + // deselected a built tower. + Game.prototype.pressMap = function (x, y, hitRadius) { + var onPreview = this.preview && Math.hypot(this.preview.x - x, this.preview.y - y) <= hitRadius; + var t = this.towerAt(x, y); + this.sellArmed = false; + if (t && !onPreview) { + this.shop = null; + this.preview = null; + this.selected = t === this.selected ? null : t; + return null; + } + this.selected = null; + if (!this.shop) return null; + return onPreview ? "preview" : "place"; + }; + + // move the preview to the nearest legal position to map position x, y. + // where there is none, the preview stays where it was. returns false + // when no legal position was found. + Game.prototype.movePreview = function (x, y) { + if (!this.shop) return false; + var pv = this.map.snap(x, y, this.towers); + if (!pv) return false; + if (this.preview && this.preview.x === pv.x && this.preview.y === pv.y) return true; + // t0 draws spikes only towards the path, judged by its top level range + var def = cfg.tower[this.shop]; + if (def.projectile_speed === 0) { + pv.sides = this.map.facingSides(pv.x, pv.y, def.range[def.range.length - 1]); + } + this.preview = pv; + return true; + }; + + // build the shop tower at the preview. returns false when there is no + // preview or the tower cannot be afforded. + Game.prototype.build = function () { + if (!this.shop || !this.preview) return false; + var cost = cfg.tower[this.shop].cost[0]; + if (!this.canAfford(cost)) return false; + this.pay(cost); + var pv = this.preview; + var t = MM.tower.create(this.shop, pv.x, pv.y, pv.sides); + t.paid += cost; + 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 += cost; + MM.audio.play("tower_upgraded"); + }; + + // the first tap arms selling, the second sells + Game.prototype.sell = function () { + var t = this.selected; + if (!t) return; + if (!this.sellArmed) { + this.sellArmed = true; + return; + } + this.sellArmed = false; + this.coins += MM.tower.refund(t); + this.towers.splice(this.towers.indexOf(t), 1); + this.selected = null; + MM.audio.play("tower_sold"); + }; + + return Game; +})();
