view game/js/wave.js @ 11:a77857594150

adjusted tower building, added preview dragging
author ark
date 2026-10-19 17:56 +0200
parents 2d46729a022e
children 7fda634d15ad
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, "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++;
        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.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;
})();