wobblyhead hg > minor_monsters
view game/js/game.js @ 5:67bf3c505145
adjusted map0 mask
| author | ark |
|---|---|
| date | 2026-10-18 22:50 +0200 |
| parents | 2d46729a022e |
| children | 910b26feca76 |
line wrap: on
line source
// game state and rules: coins, hearts, building, win and loss window.MM = window.MM || {}; MM.Game = (function () { "use strict"; var cfg = MM.config; var dbg = cfg.debug; function Game(mapIndex, seed) { 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.waves = new MM.Waves(mapIndex, MM.util.clamp(dbg.start_wave - 1, 0, mc.waves.length - 1)); this.time = 0; this.paused = false; this.speeds = cfg.controls.speeds.concat(dbg.extra_speeds); this.speedIndex = 0; this.result = null; // "won" or "lost" // player selection: shop tower type, its snapped preview, a built tower this.shop = null; this.preview = null; this.selected = null; } Game.prototype.speed = function () { return this.speeds[this.speedIndex]; }; 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; }); 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 = []; }; // 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"); // 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 tap on the map at map position x, y. hitRadius is how close the tap // must be to the preview to build. returns false when no legal position // was found for a preview. Game.prototype.tapMap = function (x, y, hitRadius) { var t = this.towerAt(x, y); if (t && !(this.preview && Math.hypot(this.preview.x - x, this.preview.y - y) <= hitRadius)) { this.shop = null; this.preview = null; this.selected = t === this.selected ? null : t; return true; } this.selected = null; if (!this.shop) return true; if (this.preview && Math.hypot(this.preview.x - x, this.preview.y - y) <= hitRadius) { return this.build(); } this.preview = this.map.snap(x, y, this.towers); return this.preview !== null; }; 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)); MM.audio.play("tower_built"); this.shop = null; this.preview = null; return true; }; return Game; })();
