|
1
|
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
|
|
|
13 function Waves(mapIndex, firstWave) {
|
|
|
14 this.list = cfg.maps[mapIndex].waves;
|
|
|
15 this.rate = cfg.maps[mapIndex].early_bonus;
|
|
|
16 this.next = firstWave;
|
|
|
17 this.phase = "break";
|
|
|
18 this.left = cfg.waves.first_break;
|
|
|
19 this.queue = [];
|
|
|
20 this.clock = 0;
|
|
|
21 }
|
|
|
22
|
|
|
23 // spawn times of a wave: a group starts after the previous group has
|
|
|
24 // finished spawning, plus its delay
|
|
|
25 function schedule(wave) {
|
|
|
26 var out = [], t = 0;
|
|
|
27 wave.groups.forEach(function (g, gi) {
|
|
|
28 var iv = g.interval !== undefined ? g.interval : cfg.waves.spawn_interval;
|
|
|
29 if (gi > 0) t += g.delay || 0;
|
|
|
30 for (var i = 0; i < g.count; i++) {
|
|
|
31 if (i > 0) t += iv;
|
|
|
32 out.push({ time: t, monster: g.monster });
|
|
|
33 }
|
|
|
34 });
|
|
|
35 return out;
|
|
|
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 this.queue = schedule(this.list[this.next]);
|
|
|
49 this.clock = 0;
|
|
|
50 this.phase = "spawning";
|
|
|
51 this.next++;
|
|
|
52 };
|
|
|
53
|
|
|
54 // start the next wave during a break. returns the bonus coins.
|
|
|
55 Waves.prototype.startEarly = function () {
|
|
|
56 if (this.phase !== "break") return 0;
|
|
|
57 var b = this.bonus();
|
|
|
58 this.start();
|
|
|
59 return b;
|
|
|
60 };
|
|
|
61
|
|
|
62 // advance by dt. spawn(kind) creates a monster; alive is the number of
|
|
|
63 // monsters on the map.
|
|
|
64 Waves.prototype.update = function (dt, alive, spawn) {
|
|
|
65 if (this.phase === "break") {
|
|
|
66 this.left -= dt;
|
|
|
67 if (this.left <= 0) this.start();
|
|
|
68 return;
|
|
|
69 }
|
|
|
70 if (this.phase === "spawning") {
|
|
|
71 this.clock += dt;
|
|
|
72 while (this.queue.length && this.queue[0].time <= this.clock) {
|
|
|
73 spawn(this.queue.shift().monster);
|
|
|
74 }
|
|
|
75 if (!this.queue.length) this.phase = "clearing";
|
|
|
76 return;
|
|
|
77 }
|
|
|
78 if (this.phase === "clearing" && alive === 0) {
|
|
|
79 if (this.next >= this.list.length) {
|
|
|
80 this.phase = "done";
|
|
|
81 } else {
|
|
|
82 this.phase = "break";
|
|
|
83 this.left = cfg.waves.break;
|
|
|
84 }
|
|
|
85 }
|
|
|
86 };
|
|
|
87
|
|
|
88 return Waves;
|
|
|
89 })();
|