|
1
|
1 // game state and rules: coins, hearts, building, win and loss
|
|
|
2
|
|
|
3 window.MM = window.MM || {};
|
|
|
4
|
|
|
5 MM.Game = (function () {
|
|
|
6 "use strict";
|
|
|
7
|
|
|
8 var cfg = MM.config;
|
|
|
9 var dbg = cfg.debug;
|
|
|
10
|
|
|
11 function Game(mapIndex, seed) {
|
|
|
12 var mc = cfg.maps[mapIndex];
|
|
|
13 this.map = new MM.GameMap(mapIndex);
|
|
|
14 this.seed = seed;
|
|
|
15 this.random = MM.util.rng(seed);
|
|
|
16 this.coins = dbg.start_coins >= 0 ? dbg.start_coins : mc.starting_coins;
|
|
|
17 this.hearts = cfg.hearts.per_map;
|
|
|
18 this.monsters = [];
|
|
|
19 this.towers = [];
|
|
|
20 this.projectiles = [];
|
|
|
21 this.flights = []; // coins flying to the counter
|
|
|
22 this.waves = new MM.Waves(mapIndex, MM.util.clamp(dbg.start_wave - 1, 0, mc.waves.length - 1));
|
|
|
23 this.time = 0;
|
|
|
24 this.paused = false;
|
|
|
25 this.speeds = cfg.controls.speeds.concat(dbg.extra_speeds);
|
|
|
26 this.speedIndex = 0;
|
|
|
27 this.result = null; // "won" or "lost"
|
|
|
28 // player selection: shop tower type, its snapped preview, a built tower
|
|
|
29 this.shop = null;
|
|
|
30 this.preview = null;
|
|
|
31 this.selected = null;
|
|
|
32 }
|
|
|
33
|
|
|
34 Game.prototype.speed = function () {
|
|
|
35 return this.speeds[this.speedIndex];
|
|
|
36 };
|
|
|
37
|
|
|
38 Game.prototype.canAfford = function (cost) {
|
|
|
39 return dbg.unlimited_coins || this.coins >= cost;
|
|
|
40 };
|
|
|
41
|
|
|
42 Game.prototype.pay = function (cost) {
|
|
|
43 if (!dbg.unlimited_coins) this.coins -= cost;
|
|
|
44 };
|
|
|
45
|
|
|
46 // one simulation step of dt seconds
|
|
|
47 Game.prototype.step = function (dt) {
|
|
|
48 if (this.result) return;
|
|
|
49 var self = this;
|
|
|
50 this.time += dt;
|
|
|
51 this.waves.update(dt, this.monsters.length, function (kind) {
|
|
4
|
52 var m = MM.monster.create(self, kind);
|
|
|
53 self.monsters.push(m);
|
|
|
54 MM.audio.play(m.boss ? "boss_enter" : "monster_enter");
|
|
1
|
55 });
|
|
|
56 this.monsters.forEach(function (m) {
|
|
|
57 if (m.alive && MM.monster.update(m, dt)) self.escape(m);
|
|
|
58 });
|
|
|
59 this.towers.forEach(function (t) { MM.tower.update(t, self, dt); });
|
|
|
60 this.projectiles.forEach(function (p) { MM.projectile.update(p, self, dt); });
|
|
|
61 this.monsters = this.monsters.filter(function (m) { return m.alive; });
|
|
|
62 this.projectiles = this.projectiles.filter(function (p) { return !p.done; });
|
|
|
63 this.flights.forEach(function (f) { f.t += dt; });
|
|
4
|
64 var flying = this.flights.length;
|
|
1
|
65 this.flights = this.flights.filter(function (f) { return f.t < cfg.coins.flight_time; });
|
|
4
|
66 if (this.flights.length < flying) MM.audio.play("coins_arrive");
|
|
1
|
67
|
|
4
|
68 // the end sound waits for the last exit or death sound
|
|
1
|
69 if (this.hearts <= 0) {
|
|
|
70 this.result = "lost";
|
|
4
|
71 MM.audio.play("map_lost", ["monster_exit", "boss_exit"]);
|
|
1
|
72 } else if (this.waves.phase === "done" && this.monsters.length === 0) {
|
|
|
73 this.result = "won";
|
|
4
|
74 MM.audio.play("map_won", ["monster_die", "boss_die"]);
|
|
1
|
75 }
|
|
|
76 // the simulation stops here; the coins in flight are counted already
|
|
|
77 if (this.result) this.flights = [];
|
|
|
78 };
|
|
|
79
|
|
|
80 // a monster reached the exit
|
|
|
81 Game.prototype.escape = function (m) {
|
|
|
82 m.alive = false;
|
|
4
|
83 MM.audio.play(m.boss ? "boss_exit" : "monster_exit");
|
|
1
|
84 if (!dbg.no_heart_loss) {
|
|
|
85 this.hearts = Math.max(0, this.hearts - (m.boss ? cfg.hearts.boss : cfg.hearts.monster));
|
|
|
86 }
|
|
|
87 };
|
|
|
88
|
|
|
89 Game.prototype.damage = function (m, amount) {
|
|
|
90 if (!m.alive) return;
|
|
|
91 m.hp -= amount;
|
|
4
|
92 if (m.hp > 0) {
|
|
|
93 MM.audio.play("monster_hit");
|
|
|
94 return;
|
|
|
95 }
|
|
1
|
96 m.alive = false;
|
|
4
|
97 MM.audio.play(m.boss ? "boss_die" : "monster_die");
|
|
1
|
98 // coins count at once; the flying coins only show it (balance.md)
|
|
|
99 this.coins += m.coins;
|
|
|
100 var n = Math.min(m.coins, cfg.coins.max_sprites);
|
|
|
101 for (var i = 0; i < n; i++) {
|
|
|
102 this.flights.push({
|
|
|
103 x: m.x + (this.random() - 0.5) * m.w,
|
|
|
104 y: m.y - m.h / 2 + (this.random() - 0.5) * m.h / 2,
|
|
|
105 sprite: "c" + Math.floor(this.random() * 4),
|
|
|
106 // stagger the coins a little
|
|
|
107 t: -i * 0.05
|
|
|
108 });
|
|
|
109 }
|
|
|
110 };
|
|
|
111
|
|
|
112 Game.prototype.startEarly = function () {
|
|
4
|
113 var bonus = this.waves.startEarly();
|
|
|
114 this.coins += bonus;
|
|
|
115 if (bonus > 0) MM.audio.play("early_bonus");
|
|
1
|
116 };
|
|
|
117
|
|
|
118 Game.prototype.towerAt = function (x, y) {
|
|
|
119 var r = cfg.placement.footprint_radius;
|
|
|
120 for (var i = 0; i < this.towers.length; i++) {
|
|
|
121 var t = this.towers[i];
|
|
|
122 if (Math.hypot(t.x - x, t.y - y) <= r) return t;
|
|
|
123 }
|
|
|
124 return null;
|
|
|
125 };
|
|
|
126
|
|
|
127 Game.prototype.chooseShop = function (type) {
|
|
|
128 this.selected = null;
|
|
|
129 this.preview = null;
|
|
|
130 this.shop = this.shop === type ? null : type;
|
|
|
131 };
|
|
|
132
|
|
|
133 // a tap on the map at map position x, y. hitRadius is how close the tap
|
|
|
134 // must be to the preview to build. returns false when no legal position
|
|
|
135 // was found for a preview.
|
|
|
136 Game.prototype.tapMap = function (x, y, hitRadius) {
|
|
|
137 var t = this.towerAt(x, y);
|
|
|
138 if (t && !(this.preview && Math.hypot(this.preview.x - x, this.preview.y - y) <= hitRadius)) {
|
|
|
139 this.shop = null;
|
|
|
140 this.preview = null;
|
|
|
141 this.selected = t === this.selected ? null : t;
|
|
|
142 return true;
|
|
|
143 }
|
|
|
144 this.selected = null;
|
|
|
145 if (!this.shop) return true;
|
|
|
146 if (this.preview && Math.hypot(this.preview.x - x, this.preview.y - y) <= hitRadius) {
|
|
|
147 return this.build();
|
|
|
148 }
|
|
|
149 this.preview = this.map.snap(x, y, this.towers);
|
|
|
150 return this.preview !== null;
|
|
|
151 };
|
|
|
152
|
|
|
153 Game.prototype.build = function () {
|
|
|
154 var cost = cfg.tower[this.shop].cost;
|
|
|
155 if (!this.canAfford(cost)) return false;
|
|
|
156 this.pay(cost);
|
|
|
157 this.towers.push(MM.tower.create(this.shop, this.preview.x, this.preview.y));
|
|
4
|
158 MM.audio.play("tower_built");
|
|
1
|
159 this.shop = null;
|
|
|
160 this.preview = null;
|
|
|
161 return true;
|
|
|
162 };
|
|
|
163
|
|
|
164 return Game;
|
|
|
165 })();
|