view game/js/wave.js @ 30:a51e44094df2 0.9.2

art source
author ark
date 2026-10-20 17:58 +0200
parents 2f555a54c332
children
line wrap: on
line source

// 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, "done" follows the last wave. a break starts when
    // the last monster of a wave has spawned, so monsters may still be on
    // the map. 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.
    Waves.prototype.update = function (dt, 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) return;
            if (this.next >= this.list.length) {
                this.phase = "done";
            } else {
                this.phase = "break";
                this.left = cfg.waves.break;
            }
        }
    };

    return Waves;
})();