|
17
|
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
|
|
23
|
11 // the wave's monsters, "done" follows the last wave. a break starts when
|
|
|
12 // the last monster of a wave has spawned, so monsters may still be on
|
|
|
13 // the map. random gives numbers in [0, 1).
|
|
17
|
14 function Waves(mapIndex, firstWave, random) {
|
|
|
15 var mc = cfg.maps[mapIndex];
|
|
|
16 this.list = mc.waves;
|
|
|
17 this.rate = mc.early_bonus;
|
|
|
18 this.interval = mc.spawn_interval !== undefined ? mc.spawn_interval : cfg.waves.spawn_interval;
|
|
|
19 this.random = random;
|
|
|
20 this.next = firstWave;
|
|
|
21 this.phase = "break";
|
|
|
22 this.left = cfg.waves.first_break;
|
|
|
23 this.pending = []; // monster kinds of the wave not spawned yet
|
|
|
24 this.wait = 0; // seconds until the next spawn
|
|
|
25 }
|
|
|
26
|
|
|
27 // take the next monster out of pending: a boss if one is left,
|
|
|
28 // otherwise a random one, each monster left equally likely
|
|
|
29 Waves.prototype.take = function () {
|
|
|
30 var i;
|
|
|
31 for (i = 0; i < this.pending.length; i++) {
|
|
|
32 if (cfg.monster[this.pending[i]].boss) break;
|
|
|
33 }
|
|
|
34 if (i === this.pending.length) i = Math.floor(this.random() * this.pending.length);
|
|
|
35 return this.pending.splice(i, 1)[0];
|
|
|
36 };
|
|
|
37
|
|
|
38 // number of the wave shown in the top bar, from 1
|
|
|
39 Waves.prototype.shown = function () {
|
|
|
40 return Math.min(this.next + (this.phase === "break" ? 1 : 0), this.list.length);
|
|
|
41 };
|
|
|
42
|
|
|
43 Waves.prototype.bonus = function () {
|
|
|
44 return this.phase === "break" ? Math.floor(this.left * this.rate) : 0;
|
|
|
45 };
|
|
|
46
|
|
|
47 Waves.prototype.start = function () {
|
|
|
48 var counts = this.list[this.next].monsters;
|
|
|
49 this.pending = [];
|
|
|
50 for (var kind in counts) {
|
|
|
51 for (var i = 0; i < counts[kind]; i++) this.pending.push(kind);
|
|
|
52 }
|
|
|
53 this.wait = 0;
|
|
|
54 this.phase = "spawning";
|
|
|
55 this.next++;
|
|
|
56 MM.audio.play("wave_start");
|
|
|
57 };
|
|
|
58
|
|
|
59 // start the next wave during a break. returns the bonus coins.
|
|
|
60 Waves.prototype.startEarly = function () {
|
|
|
61 if (this.phase !== "break") return 0;
|
|
|
62 var b = this.bonus();
|
|
|
63 this.start();
|
|
|
64 return b;
|
|
|
65 };
|
|
|
66
|
|
23
|
67 // advance by dt. spawn(kind) creates a monster.
|
|
|
68 Waves.prototype.update = function (dt, spawn) {
|
|
17
|
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 }
|
|
23
|
80 if (this.pending.length) return;
|
|
17
|
81 if (this.next >= this.list.length) {
|
|
|
82 this.phase = "done";
|
|
|
83 } else {
|
|
|
84 this.phase = "break";
|
|
|
85 this.left = cfg.waves.break;
|
|
|
86 }
|
|
|
87 }
|
|
|
88 };
|
|
|
89
|
|
|
90 return Waves;
|
|
|
91 })();
|