wobblyhead hg > minor_monsters
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 13:8df95050fc8b | 14:7fda634d15ad |
|---|---|
| 7 | 7 |
| 8 var cfg = MM.config; | 8 var cfg = MM.config; |
| 9 | 9 |
| 10 // phases: "break" counts down to the next wave, "spawning" releases | 10 // phases: "break" counts down to the next wave, "spawning" releases |
| 11 // the wave's monsters, "clearing" waits until the wave's monsters are | 11 // the wave's monsters, "clearing" waits until the wave's monsters are |
| 12 // gone, "done" follows the last wave | 12 // gone, "done" follows the last wave. random gives numbers in [0, 1). |
| 13 function Waves(mapIndex, firstWave) { | 13 function Waves(mapIndex, firstWave, random) { |
| 14 this.list = cfg.maps[mapIndex].waves; | 14 var mc = cfg.maps[mapIndex]; |
| 15 this.rate = cfg.maps[mapIndex].early_bonus; | 15 this.list = mc.waves; |
| 16 this.rate = mc.early_bonus; | |
| 17 this.interval = mc.spawn_interval !== undefined ? mc.spawn_interval : cfg.waves.spawn_interval; | |
| 18 this.random = random; | |
| 16 this.next = firstWave; | 19 this.next = firstWave; |
| 17 this.phase = "break"; | 20 this.phase = "break"; |
| 18 this.left = cfg.waves.first_break; | 21 this.left = cfg.waves.first_break; |
| 19 this.queue = []; | 22 this.pending = []; // monster kinds of the wave not spawned yet |
| 20 this.clock = 0; | 23 this.wait = 0; // seconds until the next spawn |
| 21 } | 24 } |
| 22 | 25 |
| 23 // spawn times of a wave: a group starts after the previous group has | 26 // take the next monster out of pending: a boss if one is left, |
| 24 // finished spawning, plus its delay | 27 // otherwise a random one, each monster left equally likely |
| 25 function schedule(wave) { | 28 Waves.prototype.take = function () { |
| 26 var out = [], t = 0; | 29 var i; |
| 27 wave.groups.forEach(function (g, gi) { | 30 for (i = 0; i < this.pending.length; i++) { |
| 28 var iv = g.interval !== undefined ? g.interval : cfg.waves.spawn_interval; | 31 if (cfg.monster[this.pending[i]].boss) break; |
| 29 if (gi > 0) t += g.delay || 0; | 32 } |
| 30 for (var i = 0; i < g.count; i++) { | 33 if (i === this.pending.length) i = Math.floor(this.random() * this.pending.length); |
| 31 if (i > 0) t += iv; | 34 return this.pending.splice(i, 1)[0]; |
| 32 out.push({ time: t, monster: g.monster }); | 35 }; |
| 33 } | |
| 34 }); | |
| 35 return out; | |
| 36 } | |
| 37 | 36 |
| 38 // number of the wave shown in the top bar, from 1 | 37 // number of the wave shown in the top bar, from 1 |
| 39 Waves.prototype.shown = function () { | 38 Waves.prototype.shown = function () { |
| 40 return Math.min(this.next + (this.phase === "break" ? 1 : 0), this.list.length); | 39 return Math.min(this.next + (this.phase === "break" ? 1 : 0), this.list.length); |
| 41 }; | 40 }; |
| 43 Waves.prototype.bonus = function () { | 42 Waves.prototype.bonus = function () { |
| 44 return this.phase === "break" ? Math.floor(this.left * this.rate) : 0; | 43 return this.phase === "break" ? Math.floor(this.left * this.rate) : 0; |
| 45 }; | 44 }; |
| 46 | 45 |
| 47 Waves.prototype.start = function () { | 46 Waves.prototype.start = function () { |
| 48 this.queue = schedule(this.list[this.next]); | 47 var counts = this.list[this.next].monsters; |
| 49 this.clock = 0; | 48 this.pending = []; |
| 49 for (var kind in counts) { | |
| 50 for (var i = 0; i < counts[kind]; i++) this.pending.push(kind); | |
| 51 } | |
| 52 this.wait = 0; | |
| 50 this.phase = "spawning"; | 53 this.phase = "spawning"; |
| 51 this.next++; | 54 this.next++; |
| 52 MM.audio.play("wave_start"); | 55 MM.audio.play("wave_start"); |
| 53 }; | 56 }; |
| 54 | 57 |
| 67 this.left -= dt; | 70 this.left -= dt; |
| 68 if (this.left <= 0) this.start(); | 71 if (this.left <= 0) this.start(); |
| 69 return; | 72 return; |
| 70 } | 73 } |
| 71 if (this.phase === "spawning") { | 74 if (this.phase === "spawning") { |
| 72 this.clock += dt; | 75 this.wait -= dt; |
| 73 while (this.queue.length && this.queue[0].time <= this.clock) { | 76 while (this.pending.length && this.wait <= 0) { |
| 74 spawn(this.queue.shift().monster); | 77 spawn(this.take()); |
| 78 this.wait += this.interval; | |
| 75 } | 79 } |
| 76 if (!this.queue.length) this.phase = "clearing"; | 80 if (!this.pending.length) this.phase = "clearing"; |
| 77 return; | 81 return; |
| 78 } | 82 } |
| 79 if (this.phase === "clearing" && alive === 0) { | 83 if (this.phase === "clearing" && alive === 0) { |
| 80 if (this.next >= this.list.length) { | 84 if (this.next >= this.list.length) { |
| 81 this.phase = "done"; | 85 this.phase = "done"; |
