annotate game/js/game.js @ 6:910b26feca76

full game flow
author ark
date 2026-10-19 01:05 +0200
parents 2d46729a022e
children 2c4fed8baed9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
910b26feca76 full game flow
ark
parents: 4
diff changeset
1 // game state and rules: coins, hearts, building, upgrading, selling, win
910b26feca76 full game flow
ark
parents: 4
diff changeset
2 // and loss
1
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
3
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
4 window.MM = window.MM || {};
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
5
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
6 MM.Game = (function () {
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
7 "use strict";
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
8
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
9 var cfg = MM.config;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
10 var dbg = cfg.debug;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
11
6
910b26feca76 full game flow
ark
parents: 4
diff changeset
12 // firstWave counts from 1
910b26feca76 full game flow
ark
parents: 4
diff changeset
13 function Game(mapIndex, seed, firstWave) {
1
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
14 var mc = cfg.maps[mapIndex];
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
15 this.map = new MM.GameMap(mapIndex);
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
16 this.seed = seed;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
17 this.random = MM.util.rng(seed);
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
18 this.coins = dbg.start_coins >= 0 ? dbg.start_coins : mc.starting_coins;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
19 this.hearts = cfg.hearts.per_map;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
20 this.monsters = [];
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
21 this.towers = [];
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
22 this.projectiles = [];
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
23 this.flights = []; // coins flying to the counter
6
910b26feca76 full game flow
ark
parents: 4
diff changeset
24 this.waves = new MM.Waves(mapIndex, MM.util.clamp(firstWave - 1, 0, mc.waves.length - 1));
1
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
25 this.time = 0;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
26 this.paused = false;
6
910b26feca76 full game flow
ark
parents: 4
diff changeset
27 this.pauses = 0; // number of pauses so far, for the sell refund
1
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
28 this.speeds = cfg.controls.speeds.concat(dbg.extra_speeds);
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
29 this.speedIndex = 0;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
30 this.result = null; // "won" or "lost"
6
910b26feca76 full game flow
ark
parents: 4
diff changeset
31 this.splash = false; // splash screen shown over the paused map
1
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
32 // player selection: shop tower type, its snapped preview, a built tower
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
33 this.shop = null;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
34 this.preview = null;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
35 this.selected = null;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
36 }
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
37
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
38 Game.prototype.speed = function () {
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
39 return this.speeds[this.speedIndex];
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
40 };
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
41
6
910b26feca76 full game flow
ark
parents: 4
diff changeset
42 Game.prototype.lastMap = function () {
910b26feca76 full game flow
ark
parents: 4
diff changeset
43 return this.map.index === MM.maps.length - 1;
910b26feca76 full game flow
ark
parents: 4
diff changeset
44 };
910b26feca76 full game flow
ark
parents: 4
diff changeset
45
910b26feca76 full game flow
ark
parents: 4
diff changeset
46 Game.prototype.togglePause = function () {
910b26feca76 full game flow
ark
parents: 4
diff changeset
47 this.paused = !this.paused;
910b26feca76 full game flow
ark
parents: 4
diff changeset
48 if (this.paused) this.pauses++;
910b26feca76 full game flow
ark
parents: 4
diff changeset
49 };
910b26feca76 full game flow
ark
parents: 4
diff changeset
50
910b26feca76 full game flow
ark
parents: 4
diff changeset
51 // number of the current pause, 0 while the game runs
910b26feca76 full game flow
ark
parents: 4
diff changeset
52 Game.prototype.pauseNumber = function () {
910b26feca76 full game flow
ark
parents: 4
diff changeset
53 return this.paused ? this.pauses : 0;
910b26feca76 full game flow
ark
parents: 4
diff changeset
54 };
910b26feca76 full game flow
ark
parents: 4
diff changeset
55
1
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
56 Game.prototype.canAfford = function (cost) {
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
57 return dbg.unlimited_coins || this.coins >= cost;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
58 };
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
59
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
60 Game.prototype.pay = function (cost) {
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
61 if (!dbg.unlimited_coins) this.coins -= cost;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
62 };
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
63
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
64 // one simulation step of dt seconds
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
65 Game.prototype.step = function (dt) {
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
66 if (this.result) return;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
67 var self = this;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
68 this.time += dt;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
69 this.waves.update(dt, this.monsters.length, function (kind) {
4
2d46729a022e sound processing and in-game sounds
ark
parents: 1
diff changeset
70 var m = MM.monster.create(self, kind);
2d46729a022e sound processing and in-game sounds
ark
parents: 1
diff changeset
71 self.monsters.push(m);
2d46729a022e sound processing and in-game sounds
ark
parents: 1
diff changeset
72 MM.audio.play(m.boss ? "boss_enter" : "monster_enter");
1
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
73 });
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
74 this.monsters.forEach(function (m) {
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
75 if (m.alive && MM.monster.update(m, dt)) self.escape(m);
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
76 });
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
77 this.towers.forEach(function (t) { MM.tower.update(t, self, dt); });
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
78 this.projectiles.forEach(function (p) { MM.projectile.update(p, self, dt); });
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
79 this.monsters = this.monsters.filter(function (m) { return m.alive; });
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
80 this.projectiles = this.projectiles.filter(function (p) { return !p.done; });
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
81 this.flights.forEach(function (f) { f.t += dt; });
4
2d46729a022e sound processing and in-game sounds
ark
parents: 1
diff changeset
82 var flying = this.flights.length;
1
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
83 this.flights = this.flights.filter(function (f) { return f.t < cfg.coins.flight_time; });
4
2d46729a022e sound processing and in-game sounds
ark
parents: 1
diff changeset
84 if (this.flights.length < flying) MM.audio.play("coins_arrive");
1
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
85
4
2d46729a022e sound processing and in-game sounds
ark
parents: 1
diff changeset
86 // the end sound waits for the last exit or death sound
1
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
87 if (this.hearts <= 0) {
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
88 this.result = "lost";
4
2d46729a022e sound processing and in-game sounds
ark
parents: 1
diff changeset
89 MM.audio.play("map_lost", ["monster_exit", "boss_exit"]);
1
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
90 } else if (this.waves.phase === "done" && this.monsters.length === 0) {
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
91 this.result = "won";
4
2d46729a022e sound processing and in-game sounds
ark
parents: 1
diff changeset
92 MM.audio.play("map_won", ["monster_die", "boss_die"]);
1
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
93 }
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
94 // the simulation stops here; the coins in flight are counted already
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
95 if (this.result) this.flights = [];
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
96 };
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
97
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
98 // a monster reached the exit
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
99 Game.prototype.escape = function (m) {
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
100 m.alive = false;
4
2d46729a022e sound processing and in-game sounds
ark
parents: 1
diff changeset
101 MM.audio.play(m.boss ? "boss_exit" : "monster_exit");
1
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
102 if (!dbg.no_heart_loss) {
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
103 this.hearts = Math.max(0, this.hearts - (m.boss ? cfg.hearts.boss : cfg.hearts.monster));
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
104 }
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
105 };
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
106
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
107 Game.prototype.damage = function (m, amount) {
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
108 if (!m.alive) return;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
109 m.hp -= amount;
4
2d46729a022e sound processing and in-game sounds
ark
parents: 1
diff changeset
110 if (m.hp > 0) {
2d46729a022e sound processing and in-game sounds
ark
parents: 1
diff changeset
111 MM.audio.play("monster_hit");
2d46729a022e sound processing and in-game sounds
ark
parents: 1
diff changeset
112 return;
2d46729a022e sound processing and in-game sounds
ark
parents: 1
diff changeset
113 }
1
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
114 m.alive = false;
4
2d46729a022e sound processing and in-game sounds
ark
parents: 1
diff changeset
115 MM.audio.play(m.boss ? "boss_die" : "monster_die");
1
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
116 // coins count at once; the flying coins only show it (balance.md)
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
117 this.coins += m.coins;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
118 var n = Math.min(m.coins, cfg.coins.max_sprites);
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
119 for (var i = 0; i < n; i++) {
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
120 this.flights.push({
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
121 x: m.x + (this.random() - 0.5) * m.w,
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
122 y: m.y - m.h / 2 + (this.random() - 0.5) * m.h / 2,
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
123 sprite: "c" + Math.floor(this.random() * 4),
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
124 // stagger the coins a little
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
125 t: -i * 0.05
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
126 });
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
127 }
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
128 };
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
129
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
130 Game.prototype.startEarly = function () {
4
2d46729a022e sound processing and in-game sounds
ark
parents: 1
diff changeset
131 var bonus = this.waves.startEarly();
2d46729a022e sound processing and in-game sounds
ark
parents: 1
diff changeset
132 this.coins += bonus;
2d46729a022e sound processing and in-game sounds
ark
parents: 1
diff changeset
133 if (bonus > 0) MM.audio.play("early_bonus");
1
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
134 };
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
135
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
136 Game.prototype.towerAt = function (x, y) {
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
137 var r = cfg.placement.footprint_radius;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
138 for (var i = 0; i < this.towers.length; i++) {
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
139 var t = this.towers[i];
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
140 if (Math.hypot(t.x - x, t.y - y) <= r) return t;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
141 }
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
142 return null;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
143 };
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
144
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
145 Game.prototype.chooseShop = function (type) {
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
146 this.selected = null;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
147 this.preview = null;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
148 this.shop = this.shop === type ? null : type;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
149 };
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
150
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
151 // a tap on the map at map position x, y. hitRadius is how close the tap
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
152 // must be to the preview to build. returns false when no legal position
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
153 // was found for a preview.
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
154 Game.prototype.tapMap = function (x, y, hitRadius) {
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
155 var t = this.towerAt(x, y);
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
156 if (t && !(this.preview && Math.hypot(this.preview.x - x, this.preview.y - y) <= hitRadius)) {
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
157 this.shop = null;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
158 this.preview = null;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
159 this.selected = t === this.selected ? null : t;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
160 return true;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
161 }
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
162 this.selected = null;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
163 if (!this.shop) return true;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
164 if (this.preview && Math.hypot(this.preview.x - x, this.preview.y - y) <= hitRadius) {
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
165 return this.build();
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
166 }
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
167 this.preview = this.map.snap(x, y, this.towers);
6
910b26feca76 full game flow
ark
parents: 4
diff changeset
168 if (!this.preview) return false;
910b26feca76 full game flow
ark
parents: 4
diff changeset
169 // t0 draws spikes only towards the path, judged by its top level range
910b26feca76 full game flow
ark
parents: 4
diff changeset
170 var def = cfg.tower[this.shop];
910b26feca76 full game flow
ark
parents: 4
diff changeset
171 if (def.projectile_speed === 0) {
910b26feca76 full game flow
ark
parents: 4
diff changeset
172 this.preview.sides = this.map.facingSides(this.preview.x, this.preview.y,
910b26feca76 full game flow
ark
parents: 4
diff changeset
173 def.range[def.range.length - 1]);
910b26feca76 full game flow
ark
parents: 4
diff changeset
174 }
910b26feca76 full game flow
ark
parents: 4
diff changeset
175 return true;
1
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
176 };
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
177
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
178 Game.prototype.build = function () {
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
179 var cost = cfg.tower[this.shop].cost;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
180 if (!this.canAfford(cost)) return false;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
181 this.pay(cost);
6
910b26feca76 full game flow
ark
parents: 4
diff changeset
182 var pv = this.preview;
910b26feca76 full game flow
ark
parents: 4
diff changeset
183 var t = MM.tower.create(this.shop, pv.x, pv.y, pv.sides);
910b26feca76 full game flow
ark
parents: 4
diff changeset
184 t.paid.push({ cost: cost, pause: this.pauseNumber() });
910b26feca76 full game flow
ark
parents: 4
diff changeset
185 this.towers.push(t);
4
2d46729a022e sound processing and in-game sounds
ark
parents: 1
diff changeset
186 MM.audio.play("tower_built");
1
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
187 this.shop = null;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
188 this.preview = null;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
189 return true;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
190 };
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
191
6
910b26feca76 full game flow
ark
parents: 4
diff changeset
192 // upgrade the selected tower by one level
910b26feca76 full game flow
ark
parents: 4
diff changeset
193 Game.prototype.upgrade = function () {
910b26feca76 full game flow
ark
parents: 4
diff changeset
194 var t = this.selected;
910b26feca76 full game flow
ark
parents: 4
diff changeset
195 var cost = t && MM.tower.upgradeCost(t);
910b26feca76 full game flow
ark
parents: 4
diff changeset
196 if (!cost || !this.canAfford(cost)) return;
910b26feca76 full game flow
ark
parents: 4
diff changeset
197 this.pay(cost);
910b26feca76 full game flow
ark
parents: 4
diff changeset
198 t.level++;
910b26feca76 full game flow
ark
parents: 4
diff changeset
199 t.paid.push({ cost: cost, pause: this.pauseNumber() });
910b26feca76 full game flow
ark
parents: 4
diff changeset
200 MM.audio.play("tower_upgraded");
910b26feca76 full game flow
ark
parents: 4
diff changeset
201 };
910b26feca76 full game flow
ark
parents: 4
diff changeset
202
910b26feca76 full game flow
ark
parents: 4
diff changeset
203 Game.prototype.sell = function () {
910b26feca76 full game flow
ark
parents: 4
diff changeset
204 var t = this.selected;
910b26feca76 full game flow
ark
parents: 4
diff changeset
205 if (!t) return;
910b26feca76 full game flow
ark
parents: 4
diff changeset
206 this.coins += MM.tower.refund(t, this.pauseNumber());
910b26feca76 full game flow
ark
parents: 4
diff changeset
207 this.towers.splice(this.towers.indexOf(t), 1);
910b26feca76 full game flow
ark
parents: 4
diff changeset
208 this.selected = null;
910b26feca76 full game flow
ark
parents: 4
diff changeset
209 MM.audio.play("tower_sold");
910b26feca76 full game flow
ark
parents: 4
diff changeset
210 };
910b26feca76 full game flow
ark
parents: 4
diff changeset
211
1
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
212 return Game;
5492efd60e2c first prototype + updated art sheet
ark
parents:
diff changeset
213 })();