diff game/js/wave.js @ 14:7fda634d15ad

adjustments to wave logic, tower pivot points
author ark
date 2026-10-19 22:50 +0200
parents 2d46729a022e
children
line wrap: on
line diff
--- a/game/js/wave.js	Mon Oct 19 21:59:53 2026 +0200
+++ b/game/js/wave.js	Mon Oct 19 22:50:52 2026 +0200
@@ -9,31 +9,30 @@
 
     // 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;
+    // 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.queue = [];
-        this.clock = 0;
+        this.pending = [];      // monster kinds of the wave not spawned yet
+        this.wait = 0;          // seconds until the next spawn
     }
 
-    // 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;
-    }
+    // 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 () {
@@ -45,8 +44,12 @@
     };
 
     Waves.prototype.start = function () {
-        this.queue = schedule(this.list[this.next]);
-        this.clock = 0;
+        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");
@@ -69,11 +72,12 @@
             return;
         }
         if (this.phase === "spawning") {
-            this.clock += dt;
-            while (this.queue.length && this.queue[0].time <= this.clock) {
-                spawn(this.queue.shift().monster);
+            this.wait -= dt;
+            while (this.pending.length && this.wait <= 0) {
+                spawn(this.take());
+                this.wait += this.interval;
             }
-            if (!this.queue.length) this.phase = "clearing";
+            if (!this.pending.length) this.phase = "clearing";
             return;
         }
         if (this.phase === "clearing" && alive === 0) {