wobblyhead hg > minor_monsters
diff game/js/wave.js @ 1:5492efd60e2c
first prototype + updated art sheet
| author | ark |
|---|---|
| date | 2026-10-18 14:50 +0200 |
| parents | |
| children | 2d46729a022e |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/game/js/wave.js Sun Oct 18 14:50:20 2026 +0200 @@ -0,0 +1,89 @@ +// 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 + function Waves(mapIndex, firstWave) { + this.list = cfg.maps[mapIndex].waves; + this.rate = cfg.maps[mapIndex].early_bonus; + this.next = firstWave; + this.phase = "break"; + this.left = cfg.waves.first_break; + this.queue = []; + this.clock = 0; + } + + // spawn times of a wave: a group starts after the previous group has + // finished spawning, plus its delay + function schedule(wave) { + var out = [], t = 0; + wave.groups.forEach(function (g, gi) { + var iv = g.interval !== undefined ? g.interval : cfg.waves.spawn_interval; + if (gi > 0) t += g.delay || 0; + for (var i = 0; i < g.count; i++) { + if (i > 0) t += iv; + out.push({ time: t, monster: g.monster }); + } + }); + return out; + } + + // 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 () { + this.queue = schedule(this.list[this.next]); + this.clock = 0; + this.phase = "spawning"; + this.next++; + }; + + // 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.clock += dt; + while (this.queue.length && this.queue[0].time <= this.clock) { + spawn(this.queue.shift().monster); + } + if (!this.queue.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; +})();
