annotate game/js/game.js @ 4:2d46729a022e

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