wobblyhead hg > minor_monsters
diff game/js/wave.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/wave.js Tue Oct 20 00:12:36 2026 +0200 @@ -0,0 +1,94 @@ +// wave timing: breaks with countdown, spawning, early start bonus + +window.MM = window.MM || {}; + +MM.Waves = (function () { + "use strict"; + + var cfg = MM.config; + + // phases: "break" counts down to the next wave, "spawning" releases + // the wave's monsters, "clearing" waits until the wave's monsters are + // gone, "done" follows the last wave. random gives numbers in [0, 1). + function Waves(mapIndex, firstWave, random) { + var mc = cfg.maps[mapIndex]; + this.list = mc.waves; + this.rate = mc.early_bonus; + this.interval = mc.spawn_interval !== undefined ? mc.spawn_interval : cfg.waves.spawn_interval; + this.random = random; + this.next = firstWave; + this.phase = "break"; + this.left = cfg.waves.first_break; + this.pending = []; // monster kinds of the wave not spawned yet + this.wait = 0; // seconds until the next spawn + } + + // take the next monster out of pending: a boss if one is left, + // otherwise a random one, each monster left equally likely + Waves.prototype.take = function () { + var i; + for (i = 0; i < this.pending.length; i++) { + if (cfg.monster[this.pending[i]].boss) break; + } + if (i === this.pending.length) i = Math.floor(this.random() * this.pending.length); + return this.pending.splice(i, 1)[0]; + }; + + // number of the wave shown in the top bar, from 1 + Waves.prototype.shown = function () { + return Math.min(this.next + (this.phase === "break" ? 1 : 0), this.list.length); + }; + + Waves.prototype.bonus = function () { + return this.phase === "break" ? Math.floor(this.left * this.rate) : 0; + }; + + Waves.prototype.start = function () { + var counts = this.list[this.next].monsters; + this.pending = []; + for (var kind in counts) { + for (var i = 0; i < counts[kind]; i++) this.pending.push(kind); + } + this.wait = 0; + this.phase = "spawning"; + this.next++; + MM.audio.play("wave_start"); + }; + + // start the next wave during a break. returns the bonus coins. + Waves.prototype.startEarly = function () { + if (this.phase !== "break") return 0; + var b = this.bonus(); + this.start(); + return b; + }; + + // advance by dt. spawn(kind) creates a monster; alive is the number of + // monsters on the map. + Waves.prototype.update = function (dt, alive, spawn) { + if (this.phase === "break") { + this.left -= dt; + if (this.left <= 0) this.start(); + return; + } + if (this.phase === "spawning") { + this.wait -= dt; + while (this.pending.length && this.wait <= 0) { + spawn(this.take()); + this.wait += this.interval; + } + if (!this.pending.length) this.phase = "clearing"; + return; + } + if (this.phase === "clearing" && alive === 0) { + if (this.next >= this.list.length) { + this.phase = "done"; + } else { + this.phase = "break"; + this.left = cfg.waves.break; + } + } + }; + + return Waves; +})();
