wobblyhead hg > minor_monsters
comparison game/js/wave.js @ 17:fe680946164b
reorganised repo
| author | ark |
|---|---|
| date | 2026-10-20 00:12 +0200 |
| parents | |
| children | 2f555a54c332 |
comparison
equal
deleted
inserted
replaced
| 16:30bd10284fd8 | 17:fe680946164b |
|---|---|
| 1 // wave timing: breaks with countdown, spawning, early start bonus | |
| 2 | |
| 3 window.MM = window.MM || {}; | |
| 4 | |
| 5 MM.Waves = (function () { | |
| 6 "use strict"; | |
| 7 | |
| 8 var cfg = MM.config; | |
| 9 | |
| 10 // phases: "break" counts down to the next wave, "spawning" releases | |
| 11 // the wave's monsters, "clearing" waits until the wave's monsters are | |
| 12 // gone, "done" follows the last wave. random gives numbers in [0, 1). | |
| 13 function Waves(mapIndex, firstWave, random) { | |
| 14 var mc = cfg.maps[mapIndex]; | |
| 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; | |
| 19 this.next = firstWave; | |
| 20 this.phase = "break"; | |
| 21 this.left = cfg.waves.first_break; | |
| 22 this.pending = []; // monster kinds of the wave not spawned yet | |
| 23 this.wait = 0; // seconds until the next spawn | |
| 24 } | |
| 25 | |
| 26 // take the next monster out of pending: a boss if one is left, | |
| 27 // otherwise a random one, each monster left equally likely | |
| 28 Waves.prototype.take = function () { | |
| 29 var i; | |
| 30 for (i = 0; i < this.pending.length; i++) { | |
| 31 if (cfg.monster[this.pending[i]].boss) break; | |
| 32 } | |
| 33 if (i === this.pending.length) i = Math.floor(this.random() * this.pending.length); | |
| 34 return this.pending.splice(i, 1)[0]; | |
| 35 }; | |
| 36 | |
| 37 // number of the wave shown in the top bar, from 1 | |
| 38 Waves.prototype.shown = function () { | |
| 39 return Math.min(this.next + (this.phase === "break" ? 1 : 0), this.list.length); | |
| 40 }; | |
| 41 | |
| 42 Waves.prototype.bonus = function () { | |
| 43 return this.phase === "break" ? Math.floor(this.left * this.rate) : 0; | |
| 44 }; | |
| 45 | |
| 46 Waves.prototype.start = function () { | |
| 47 var counts = this.list[this.next].monsters; | |
| 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; | |
| 53 this.phase = "spawning"; | |
| 54 this.next++; | |
| 55 MM.audio.play("wave_start"); | |
| 56 }; | |
| 57 | |
| 58 // start the next wave during a break. returns the bonus coins. | |
| 59 Waves.prototype.startEarly = function () { | |
| 60 if (this.phase !== "break") return 0; | |
| 61 var b = this.bonus(); | |
| 62 this.start(); | |
| 63 return b; | |
| 64 }; | |
| 65 | |
| 66 // advance by dt. spawn(kind) creates a monster; alive is the number of | |
| 67 // monsters on the map. | |
| 68 Waves.prototype.update = function (dt, alive, spawn) { | |
| 69 if (this.phase === "break") { | |
| 70 this.left -= dt; | |
| 71 if (this.left <= 0) this.start(); | |
| 72 return; | |
| 73 } | |
| 74 if (this.phase === "spawning") { | |
| 75 this.wait -= dt; | |
| 76 while (this.pending.length && this.wait <= 0) { | |
| 77 spawn(this.take()); | |
| 78 this.wait += this.interval; | |
| 79 } | |
| 80 if (!this.pending.length) this.phase = "clearing"; | |
| 81 return; | |
| 82 } | |
| 83 if (this.phase === "clearing" && alive === 0) { | |
| 84 if (this.next >= this.list.length) { | |
| 85 this.phase = "done"; | |
| 86 } else { | |
| 87 this.phase = "break"; | |
| 88 this.left = cfg.waves.break; | |
| 89 } | |
| 90 } | |
| 91 }; | |
| 92 | |
| 93 return Waves; | |
| 94 })(); |
