annotate game/js/game.js @ 26:28e1fc486be5

get_version.sh
author ark
date 2026-10-20 16:06 +0200
parents 3f2811aebb3d
children 75deda08db90
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17
fe680946164b reorganised repo
ark
parents:
diff changeset
1 // game state and rules: coins, hearts, building, upgrading, selling, win
fe680946164b reorganised repo
ark
parents:
diff changeset
2 // and loss
fe680946164b reorganised repo
ark
parents:
diff changeset
3
fe680946164b reorganised repo
ark
parents:
diff changeset
4 window.MM = window.MM || {};
fe680946164b reorganised repo
ark
parents:
diff changeset
5
fe680946164b reorganised repo
ark
parents:
diff changeset
6 MM.Game = (function () {
fe680946164b reorganised repo
ark
parents:
diff changeset
7 "use strict";
fe680946164b reorganised repo
ark
parents:
diff changeset
8
fe680946164b reorganised repo
ark
parents:
diff changeset
9 var cfg = MM.config;
fe680946164b reorganised repo
ark
parents:
diff changeset
10 var dbg = cfg.debug;
fe680946164b reorganised repo
ark
parents:
diff changeset
11
fe680946164b reorganised repo
ark
parents:
diff changeset
12 // firstWave counts from 1
fe680946164b reorganised repo
ark
parents:
diff changeset
13 function Game(mapIndex, seed, firstWave) {
fe680946164b reorganised repo
ark
parents:
diff changeset
14 var mc = cfg.maps[mapIndex];
fe680946164b reorganised repo
ark
parents:
diff changeset
15 this.map = new MM.GameMap(mapIndex);
fe680946164b reorganised repo
ark
parents:
diff changeset
16 this.seed = seed;
fe680946164b reorganised repo
ark
parents:
diff changeset
17 this.random = MM.util.rng(seed);
fe680946164b reorganised repo
ark
parents:
diff changeset
18 this.coins = dbg.start_coins >= 0 ? dbg.start_coins : mc.starting_coins;
fe680946164b reorganised repo
ark
parents:
diff changeset
19 this.hearts = cfg.hearts.per_map;
fe680946164b reorganised repo
ark
parents:
diff changeset
20 this.monsters = [];
fe680946164b reorganised repo
ark
parents:
diff changeset
21 this.towers = [];
fe680946164b reorganised repo
ark
parents:
diff changeset
22 this.projectiles = [];
fe680946164b reorganised repo
ark
parents:
diff changeset
23 this.flights = []; // coins flying to the counter
fe680946164b reorganised repo
ark
parents:
diff changeset
24 this.puffs = []; // death puffs, {x, y, boss, t}
24
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
25 this.freezes = []; // t2 freeze splashes, {x, y, r, t}
17
fe680946164b reorganised repo
ark
parents:
diff changeset
26 this.waves = new MM.Waves(mapIndex, MM.util.clamp(firstWave - 1, 0, mc.waves.length - 1),
fe680946164b reorganised repo
ark
parents:
diff changeset
27 this.random);
fe680946164b reorganised repo
ark
parents:
diff changeset
28 this.time = 0;
fe680946164b reorganised repo
ark
parents:
diff changeset
29 this.paused = false;
fe680946164b reorganised repo
ark
parents:
diff changeset
30 this.speeds = cfg.controls.speeds.concat(dbg.extra_speeds);
fe680946164b reorganised repo
ark
parents:
diff changeset
31 this.speedIndex = 0;
fe680946164b reorganised repo
ark
parents:
diff changeset
32 this.result = null; // "won" or "lost"
fe680946164b reorganised repo
ark
parents:
diff changeset
33 this.splash = false; // splash screen shown over the paused map
24
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
34 this.help = false; // help popup shown over everything
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
35 this.helpResume = false; // the game ran when the help popup opened
17
fe680946164b reorganised repo
ark
parents:
diff changeset
36 // player selection: shop tower type, its snapped preview, a built tower
fe680946164b reorganised repo
ark
parents:
diff changeset
37 this.shop = null;
fe680946164b reorganised repo
ark
parents:
diff changeset
38 this.preview = null;
fe680946164b reorganised repo
ark
parents:
diff changeset
39 this.selected = null;
fe680946164b reorganised repo
ark
parents:
diff changeset
40 this.sellArmed = false; // sell was tapped once and waits for the second tap
fe680946164b reorganised repo
ark
parents:
diff changeset
41 }
fe680946164b reorganised repo
ark
parents:
diff changeset
42
fe680946164b reorganised repo
ark
parents:
diff changeset
43 Game.prototype.speed = function () {
fe680946164b reorganised repo
ark
parents:
diff changeset
44 return this.speeds[this.speedIndex];
fe680946164b reorganised repo
ark
parents:
diff changeset
45 };
fe680946164b reorganised repo
ark
parents:
diff changeset
46
fe680946164b reorganised repo
ark
parents:
diff changeset
47 Game.prototype.lastMap = function () {
fe680946164b reorganised repo
ark
parents:
diff changeset
48 return this.map.index === MM.maps.length - 1;
fe680946164b reorganised repo
ark
parents:
diff changeset
49 };
fe680946164b reorganised repo
ark
parents:
diff changeset
50
fe680946164b reorganised repo
ark
parents:
diff changeset
51 // towers cannot be built, upgraded or sold while paused, so pausing
fe680946164b reorganised repo
ark
parents:
diff changeset
52 // drops the shop choice, its preview and the tower selection
fe680946164b reorganised repo
ark
parents:
diff changeset
53 Game.prototype.togglePause = function () {
fe680946164b reorganised repo
ark
parents:
diff changeset
54 this.paused = !this.paused;
fe680946164b reorganised repo
ark
parents:
diff changeset
55 if (this.paused) {
fe680946164b reorganised repo
ark
parents:
diff changeset
56 this.shop = null;
fe680946164b reorganised repo
ark
parents:
diff changeset
57 this.preview = null;
fe680946164b reorganised repo
ark
parents:
diff changeset
58 this.selected = null;
fe680946164b reorganised repo
ark
parents:
diff changeset
59 this.sellArmed = false;
fe680946164b reorganised repo
ark
parents:
diff changeset
60 }
fe680946164b reorganised repo
ark
parents:
diff changeset
61 };
fe680946164b reorganised repo
ark
parents:
diff changeset
62
24
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
63 // the help popup pauses the game while it is shown. closing it lets a
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
64 // game that was running before go on
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
65 Game.prototype.openHelp = function () {
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
66 this.help = true;
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
67 this.helpResume = !this.paused;
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
68 if (!this.paused) this.togglePause();
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
69 };
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
70
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
71 Game.prototype.closeHelp = function () {
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
72 this.help = false;
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
73 if (this.helpResume) this.togglePause();
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
74 };
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
75
17
fe680946164b reorganised repo
ark
parents:
diff changeset
76 Game.prototype.canAfford = function (cost) {
fe680946164b reorganised repo
ark
parents:
diff changeset
77 return dbg.unlimited_coins || this.coins >= cost;
fe680946164b reorganised repo
ark
parents:
diff changeset
78 };
fe680946164b reorganised repo
ark
parents:
diff changeset
79
fe680946164b reorganised repo
ark
parents:
diff changeset
80 Game.prototype.pay = function (cost) {
fe680946164b reorganised repo
ark
parents:
diff changeset
81 if (!dbg.unlimited_coins) this.coins -= cost;
fe680946164b reorganised repo
ark
parents:
diff changeset
82 };
fe680946164b reorganised repo
ark
parents:
diff changeset
83
fe680946164b reorganised repo
ark
parents:
diff changeset
84 // one simulation step of dt seconds
fe680946164b reorganised repo
ark
parents:
diff changeset
85 Game.prototype.step = function (dt) {
fe680946164b reorganised repo
ark
parents:
diff changeset
86 if (this.result) return;
fe680946164b reorganised repo
ark
parents:
diff changeset
87 var self = this;
fe680946164b reorganised repo
ark
parents:
diff changeset
88 this.time += dt;
23
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
89 this.waves.update(dt, function (kind) {
17
fe680946164b reorganised repo
ark
parents:
diff changeset
90 var m = MM.monster.create(self, kind);
fe680946164b reorganised repo
ark
parents:
diff changeset
91 self.monsters.push(m);
fe680946164b reorganised repo
ark
parents:
diff changeset
92 MM.audio.play(m.boss ? "boss_enter" : "monster_enter");
fe680946164b reorganised repo
ark
parents:
diff changeset
93 });
fe680946164b reorganised repo
ark
parents:
diff changeset
94 this.monsters.forEach(function (m) {
fe680946164b reorganised repo
ark
parents:
diff changeset
95 if (m.alive && MM.monster.update(m, dt)) self.escape(m);
fe680946164b reorganised repo
ark
parents:
diff changeset
96 });
fe680946164b reorganised repo
ark
parents:
diff changeset
97 this.towers.forEach(function (t) { MM.tower.update(t, self, dt); });
fe680946164b reorganised repo
ark
parents:
diff changeset
98 this.projectiles.forEach(function (p) { MM.projectile.update(p, self, dt); });
fe680946164b reorganised repo
ark
parents:
diff changeset
99 this.monsters = this.monsters.filter(function (m) { return m.alive; });
fe680946164b reorganised repo
ark
parents:
diff changeset
100 this.projectiles = this.projectiles.filter(function (p) { return !p.done; });
fe680946164b reorganised repo
ark
parents:
diff changeset
101 this.flights.forEach(function (f) { f.t += dt; });
fe680946164b reorganised repo
ark
parents:
diff changeset
102 this.puffs.forEach(function (p) { p.t += dt; });
fe680946164b reorganised repo
ark
parents:
diff changeset
103 this.puffs = this.puffs.filter(function (p) { return p.t < cfg.animation.death_puff; });
24
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
104 this.freezes.forEach(function (f) { f.t += dt; });
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
105 this.freezes = this.freezes.filter(function (f) { return f.t < cfg.animation.freeze_splash; });
17
fe680946164b reorganised repo
ark
parents:
diff changeset
106 var flying = this.flights.length;
fe680946164b reorganised repo
ark
parents:
diff changeset
107 this.flights = this.flights.filter(function (f) { return f.t < cfg.coins.flight_time; });
fe680946164b reorganised repo
ark
parents:
diff changeset
108 if (this.flights.length < flying) MM.audio.play("coins_arrive");
fe680946164b reorganised repo
ark
parents:
diff changeset
109
fe680946164b reorganised repo
ark
parents:
diff changeset
110 // the end sound waits for the last exit or death sound
fe680946164b reorganised repo
ark
parents:
diff changeset
111 if (this.hearts <= 0) {
fe680946164b reorganised repo
ark
parents:
diff changeset
112 this.result = "lost";
fe680946164b reorganised repo
ark
parents:
diff changeset
113 MM.audio.play("map_lost", ["monster_exit", "boss_exit"]);
fe680946164b reorganised repo
ark
parents:
diff changeset
114 } else if (this.waves.phase === "done" && this.monsters.length === 0) {
fe680946164b reorganised repo
ark
parents:
diff changeset
115 this.result = "won";
fe680946164b reorganised repo
ark
parents:
diff changeset
116 MM.audio.play("map_won", ["monster_die", "boss_die"]);
fe680946164b reorganised repo
ark
parents:
diff changeset
117 }
fe680946164b reorganised repo
ark
parents:
diff changeset
118 // the simulation stops here; the coins in flight are counted already
fe680946164b reorganised repo
ark
parents:
diff changeset
119 if (this.result) {
fe680946164b reorganised repo
ark
parents:
diff changeset
120 this.flights = [];
fe680946164b reorganised repo
ark
parents:
diff changeset
121 this.puffs = [];
24
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
122 this.freezes = [];
17
fe680946164b reorganised repo
ark
parents:
diff changeset
123 }
fe680946164b reorganised repo
ark
parents:
diff changeset
124 };
fe680946164b reorganised repo
ark
parents:
diff changeset
125
fe680946164b reorganised repo
ark
parents:
diff changeset
126 // a monster reached the exit
fe680946164b reorganised repo
ark
parents:
diff changeset
127 Game.prototype.escape = function (m) {
fe680946164b reorganised repo
ark
parents:
diff changeset
128 m.alive = false;
fe680946164b reorganised repo
ark
parents:
diff changeset
129 MM.audio.play(m.boss ? "boss_exit" : "monster_exit");
fe680946164b reorganised repo
ark
parents:
diff changeset
130 if (!dbg.no_heart_loss) {
fe680946164b reorganised repo
ark
parents:
diff changeset
131 this.hearts = Math.max(0, this.hearts - (m.boss ? cfg.hearts.boss : cfg.hearts.monster));
fe680946164b reorganised repo
ark
parents:
diff changeset
132 }
fe680946164b reorganised repo
ark
parents:
diff changeset
133 };
fe680946164b reorganised repo
ark
parents:
diff changeset
134
24
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
135 // a freeze splash of radius r at x, y: slows every monster whose hit
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
136 // area it touches to factor of its speed for time seconds
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
137 Game.prototype.freeze = function (x, y, r, factor, time) {
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
138 this.freezes.push({ x: x, y: y, r: r, t: 0 });
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
139 this.monsters.forEach(function (m) {
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
140 if (m.alive && Math.hypot(m.x - x, m.y - y) <= r + m.hitRadius) {
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
141 MM.monster.slow(m, factor, time);
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
142 }
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
143 });
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
144 };
3f2811aebb3d splash screen buttons, help button, info screen
ark
parents: 23
diff changeset
145
17
fe680946164b reorganised repo
ark
parents:
diff changeset
146 Game.prototype.damage = function (m, amount) {
fe680946164b reorganised repo
ark
parents:
diff changeset
147 if (!m.alive) return;
fe680946164b reorganised repo
ark
parents:
diff changeset
148 m.hp -= amount;
fe680946164b reorganised repo
ark
parents:
diff changeset
149 if (m.hp > 0) {
fe680946164b reorganised repo
ark
parents:
diff changeset
150 MM.audio.play("monster_hit");
fe680946164b reorganised repo
ark
parents:
diff changeset
151 return;
fe680946164b reorganised repo
ark
parents:
diff changeset
152 }
fe680946164b reorganised repo
ark
parents:
diff changeset
153 m.alive = false;
fe680946164b reorganised repo
ark
parents:
diff changeset
154 MM.audio.play(m.boss ? "boss_die" : "monster_die");
fe680946164b reorganised repo
ark
parents:
diff changeset
155 this.puffs.push({ x: m.x, y: m.y, boss: m.boss, t: 0 });
fe680946164b reorganised repo
ark
parents:
diff changeset
156 // coins count at once; the flying coins only show it (balance.md)
fe680946164b reorganised repo
ark
parents:
diff changeset
157 this.coins += m.coins;
fe680946164b reorganised repo
ark
parents:
diff changeset
158 var n = Math.min(m.coins, cfg.coins.max_sprites);
fe680946164b reorganised repo
ark
parents:
diff changeset
159 for (var i = 0; i < n; i++) {
23
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
160 this.flight(m.x + (this.random() - 0.5) * m.w,
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
161 m.y - m.h / 2 + (this.random() - 0.5) * m.h / 2, i);
17
fe680946164b reorganised repo
ark
parents:
diff changeset
162 }
fe680946164b reorganised repo
ark
parents:
diff changeset
163 };
fe680946164b reorganised repo
ark
parents:
diff changeset
164
23
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
165 // the i-th coin of a group flying from (x, y) to the counter
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
166 Game.prototype.flight = function (x, y, i) {
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
167 this.flights.push({
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
168 x: x,
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
169 y: y,
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
170 sprite: "c" + Math.floor(this.random() * 4),
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
171 // stagger the coins a little
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
172 t: -i * 0.05
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
173 });
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
174 };
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
175
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
176 // the bonus coins count at once and fly from the exits, taking turns
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
177 // if there are several
17
fe680946164b reorganised repo
ark
parents:
diff changeset
178 Game.prototype.startEarly = function () {
fe680946164b reorganised repo
ark
parents:
diff changeset
179 var bonus = this.waves.startEarly();
23
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
180 if (bonus <= 0) return;
17
fe680946164b reorganised repo
ark
parents:
diff changeset
181 this.coins += bonus;
23
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
182 MM.audio.play("early_bonus");
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
183 var exits = this.map.data.exits;
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
184 var n = exits.length ? Math.min(bonus, cfg.coins.max_sprites) : 0;
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
185 for (var i = 0; i < n; i++) {
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
186 var e = exits[i % exits.length];
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
187 this.flight(e[0] + (this.random() - 0.5) * e[2], e[1] + (this.random() - 0.5) * e[2], i);
2f555a54c332 layout fixes and balancing
ark
parents: 17
diff changeset
188 }
17
fe680946164b reorganised repo
ark
parents:
diff changeset
189 };
fe680946164b reorganised repo
ark
parents:
diff changeset
190
fe680946164b reorganised repo
ark
parents:
diff changeset
191 Game.prototype.towerAt = function (x, y) {
fe680946164b reorganised repo
ark
parents:
diff changeset
192 var r = cfg.placement.footprint_radius;
fe680946164b reorganised repo
ark
parents:
diff changeset
193 for (var i = 0; i < this.towers.length; i++) {
fe680946164b reorganised repo
ark
parents:
diff changeset
194 var t = this.towers[i];
fe680946164b reorganised repo
ark
parents:
diff changeset
195 if (Math.hypot(t.x - x, t.y - y) <= r) return t;
fe680946164b reorganised repo
ark
parents:
diff changeset
196 }
fe680946164b reorganised repo
ark
parents:
diff changeset
197 return null;
fe680946164b reorganised repo
ark
parents:
diff changeset
198 };
fe680946164b reorganised repo
ark
parents:
diff changeset
199
fe680946164b reorganised repo
ark
parents:
diff changeset
200 Game.prototype.chooseShop = function (type) {
fe680946164b reorganised repo
ark
parents:
diff changeset
201 this.selected = null;
fe680946164b reorganised repo
ark
parents:
diff changeset
202 this.preview = null;
fe680946164b reorganised repo
ark
parents:
diff changeset
203 this.shop = this.shop === type ? null : type;
fe680946164b reorganised repo
ark
parents:
diff changeset
204 };
fe680946164b reorganised repo
ark
parents:
diff changeset
205
fe680946164b reorganised repo
ark
parents:
diff changeset
206 // a press on the map at map position x, y. hitRadius is how close the
fe680946164b reorganised repo
ark
parents:
diff changeset
207 // press must be to the preview to count as a press on it. returns
fe680946164b reorganised repo
ark
parents:
diff changeset
208 // "preview" for a press on the preview, "place" for any other press
fe680946164b reorganised repo
ark
parents:
diff changeset
209 // while a shop tower is chosen, and null when the press selected or
fe680946164b reorganised repo
ark
parents:
diff changeset
210 // deselected a built tower.
fe680946164b reorganised repo
ark
parents:
diff changeset
211 Game.prototype.pressMap = function (x, y, hitRadius) {
fe680946164b reorganised repo
ark
parents:
diff changeset
212 var onPreview = this.preview && Math.hypot(this.preview.x - x, this.preview.y - y) <= hitRadius;
fe680946164b reorganised repo
ark
parents:
diff changeset
213 var t = this.towerAt(x, y);
fe680946164b reorganised repo
ark
parents:
diff changeset
214 this.sellArmed = false;
fe680946164b reorganised repo
ark
parents:
diff changeset
215 if (t && !onPreview) {
fe680946164b reorganised repo
ark
parents:
diff changeset
216 this.shop = null;
fe680946164b reorganised repo
ark
parents:
diff changeset
217 this.preview = null;
fe680946164b reorganised repo
ark
parents:
diff changeset
218 this.selected = t === this.selected ? null : t;
fe680946164b reorganised repo
ark
parents:
diff changeset
219 return null;
fe680946164b reorganised repo
ark
parents:
diff changeset
220 }
fe680946164b reorganised repo
ark
parents:
diff changeset
221 this.selected = null;
fe680946164b reorganised repo
ark
parents:
diff changeset
222 if (!this.shop) return null;
fe680946164b reorganised repo
ark
parents:
diff changeset
223 return onPreview ? "preview" : "place";
fe680946164b reorganised repo
ark
parents:
diff changeset
224 };
fe680946164b reorganised repo
ark
parents:
diff changeset
225
fe680946164b reorganised repo
ark
parents:
diff changeset
226 // move the preview to the nearest legal position to map position x, y.
fe680946164b reorganised repo
ark
parents:
diff changeset
227 // where there is none, the preview stays where it was. returns false
fe680946164b reorganised repo
ark
parents:
diff changeset
228 // when no legal position was found.
fe680946164b reorganised repo
ark
parents:
diff changeset
229 Game.prototype.movePreview = function (x, y) {
fe680946164b reorganised repo
ark
parents:
diff changeset
230 if (!this.shop) return false;
fe680946164b reorganised repo
ark
parents:
diff changeset
231 var pv = this.map.snap(x, y, this.towers);
fe680946164b reorganised repo
ark
parents:
diff changeset
232 if (!pv) return false;
fe680946164b reorganised repo
ark
parents:
diff changeset
233 if (this.preview && this.preview.x === pv.x && this.preview.y === pv.y) return true;
fe680946164b reorganised repo
ark
parents:
diff changeset
234 // t0 draws spikes only towards the path, judged by its top level range
fe680946164b reorganised repo
ark
parents:
diff changeset
235 var def = cfg.tower[this.shop];
fe680946164b reorganised repo
ark
parents:
diff changeset
236 if (def.projectile_speed === 0) {
fe680946164b reorganised repo
ark
parents:
diff changeset
237 pv.sides = this.map.facingSides(pv.x, pv.y, def.range[def.range.length - 1]);
fe680946164b reorganised repo
ark
parents:
diff changeset
238 }
fe680946164b reorganised repo
ark
parents:
diff changeset
239 this.preview = pv;
fe680946164b reorganised repo
ark
parents:
diff changeset
240 return true;
fe680946164b reorganised repo
ark
parents:
diff changeset
241 };
fe680946164b reorganised repo
ark
parents:
diff changeset
242
fe680946164b reorganised repo
ark
parents:
diff changeset
243 // build the shop tower at the preview. returns false when there is no
fe680946164b reorganised repo
ark
parents:
diff changeset
244 // preview or the tower cannot be afforded.
fe680946164b reorganised repo
ark
parents:
diff changeset
245 Game.prototype.build = function () {
fe680946164b reorganised repo
ark
parents:
diff changeset
246 if (!this.shop || !this.preview) return false;
fe680946164b reorganised repo
ark
parents:
diff changeset
247 var cost = cfg.tower[this.shop].cost[0];
fe680946164b reorganised repo
ark
parents:
diff changeset
248 if (!this.canAfford(cost)) return false;
fe680946164b reorganised repo
ark
parents:
diff changeset
249 this.pay(cost);
fe680946164b reorganised repo
ark
parents:
diff changeset
250 var pv = this.preview;
fe680946164b reorganised repo
ark
parents:
diff changeset
251 var t = MM.tower.create(this.shop, pv.x, pv.y, pv.sides);
fe680946164b reorganised repo
ark
parents:
diff changeset
252 t.paid += cost;
fe680946164b reorganised repo
ark
parents:
diff changeset
253 this.towers.push(t);
fe680946164b reorganised repo
ark
parents:
diff changeset
254 MM.audio.play("tower_built");
fe680946164b reorganised repo
ark
parents:
diff changeset
255 this.shop = null;
fe680946164b reorganised repo
ark
parents:
diff changeset
256 this.preview = null;
fe680946164b reorganised repo
ark
parents:
diff changeset
257 return true;
fe680946164b reorganised repo
ark
parents:
diff changeset
258 };
fe680946164b reorganised repo
ark
parents:
diff changeset
259
fe680946164b reorganised repo
ark
parents:
diff changeset
260 // upgrade the selected tower by one level
fe680946164b reorganised repo
ark
parents:
diff changeset
261 Game.prototype.upgrade = function () {
fe680946164b reorganised repo
ark
parents:
diff changeset
262 var t = this.selected;
fe680946164b reorganised repo
ark
parents:
diff changeset
263 var cost = t && MM.tower.upgradeCost(t);
fe680946164b reorganised repo
ark
parents:
diff changeset
264 if (!cost || !this.canAfford(cost)) return;
fe680946164b reorganised repo
ark
parents:
diff changeset
265 this.pay(cost);
fe680946164b reorganised repo
ark
parents:
diff changeset
266 t.level++;
fe680946164b reorganised repo
ark
parents:
diff changeset
267 t.paid += cost;
fe680946164b reorganised repo
ark
parents:
diff changeset
268 MM.audio.play("tower_upgraded");
fe680946164b reorganised repo
ark
parents:
diff changeset
269 };
fe680946164b reorganised repo
ark
parents:
diff changeset
270
fe680946164b reorganised repo
ark
parents:
diff changeset
271 // the first tap arms selling, the second sells
fe680946164b reorganised repo
ark
parents:
diff changeset
272 Game.prototype.sell = function () {
fe680946164b reorganised repo
ark
parents:
diff changeset
273 var t = this.selected;
fe680946164b reorganised repo
ark
parents:
diff changeset
274 if (!t) return;
fe680946164b reorganised repo
ark
parents:
diff changeset
275 if (!this.sellArmed) {
fe680946164b reorganised repo
ark
parents:
diff changeset
276 this.sellArmed = true;
fe680946164b reorganised repo
ark
parents:
diff changeset
277 return;
fe680946164b reorganised repo
ark
parents:
diff changeset
278 }
fe680946164b reorganised repo
ark
parents:
diff changeset
279 this.sellArmed = false;
fe680946164b reorganised repo
ark
parents:
diff changeset
280 this.coins += MM.tower.refund(t);
fe680946164b reorganised repo
ark
parents:
diff changeset
281 this.towers.splice(this.towers.indexOf(t), 1);
fe680946164b reorganised repo
ark
parents:
diff changeset
282 this.selected = null;
fe680946164b reorganised repo
ark
parents:
diff changeset
283 MM.audio.play("tower_sold");
fe680946164b reorganised repo
ark
parents:
diff changeset
284 };
fe680946164b reorganised repo
ark
parents:
diff changeset
285
fe680946164b reorganised repo
ark
parents:
diff changeset
286 return Game;
fe680946164b reorganised repo
ark
parents:
diff changeset
287 })();