comparison 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
comparison
equal deleted inserted replaced
3:cc0c32e71090 4:2d46729a022e
47 Game.prototype.step = function (dt) { 47 Game.prototype.step = function (dt) {
48 if (this.result) return; 48 if (this.result) return;
49 var self = this; 49 var self = this;
50 this.time += dt; 50 this.time += dt;
51 this.waves.update(dt, this.monsters.length, function (kind) { 51 this.waves.update(dt, this.monsters.length, function (kind) {
52 self.monsters.push(MM.monster.create(self, kind)); 52 var m = MM.monster.create(self, kind);
53 self.monsters.push(m);
54 MM.audio.play(m.boss ? "boss_enter" : "monster_enter");
53 }); 55 });
54 this.monsters.forEach(function (m) { 56 this.monsters.forEach(function (m) {
55 if (m.alive && MM.monster.update(m, dt)) self.escape(m); 57 if (m.alive && MM.monster.update(m, dt)) self.escape(m);
56 }); 58 });
57 this.towers.forEach(function (t) { MM.tower.update(t, self, dt); }); 59 this.towers.forEach(function (t) { MM.tower.update(t, self, dt); });
58 this.projectiles.forEach(function (p) { MM.projectile.update(p, self, dt); }); 60 this.projectiles.forEach(function (p) { MM.projectile.update(p, self, dt); });
59 this.monsters = this.monsters.filter(function (m) { return m.alive; }); 61 this.monsters = this.monsters.filter(function (m) { return m.alive; });
60 this.projectiles = this.projectiles.filter(function (p) { return !p.done; }); 62 this.projectiles = this.projectiles.filter(function (p) { return !p.done; });
61 this.flights.forEach(function (f) { f.t += dt; }); 63 this.flights.forEach(function (f) { f.t += dt; });
64 var flying = this.flights.length;
62 this.flights = this.flights.filter(function (f) { return f.t < cfg.coins.flight_time; }); 65 this.flights = this.flights.filter(function (f) { return f.t < cfg.coins.flight_time; });
66 if (this.flights.length < flying) MM.audio.play("coins_arrive");
63 67
68 // the end sound waits for the last exit or death sound
64 if (this.hearts <= 0) { 69 if (this.hearts <= 0) {
65 this.result = "lost"; 70 this.result = "lost";
71 MM.audio.play("map_lost", ["monster_exit", "boss_exit"]);
66 } else if (this.waves.phase === "done" && this.monsters.length === 0) { 72 } else if (this.waves.phase === "done" && this.monsters.length === 0) {
67 this.result = "won"; 73 this.result = "won";
74 MM.audio.play("map_won", ["monster_die", "boss_die"]);
68 } 75 }
69 // the simulation stops here; the coins in flight are counted already 76 // the simulation stops here; the coins in flight are counted already
70 if (this.result) this.flights = []; 77 if (this.result) this.flights = [];
71 }; 78 };
72 79
73 // a monster reached the exit 80 // a monster reached the exit
74 Game.prototype.escape = function (m) { 81 Game.prototype.escape = function (m) {
75 m.alive = false; 82 m.alive = false;
83 MM.audio.play(m.boss ? "boss_exit" : "monster_exit");
76 if (!dbg.no_heart_loss) { 84 if (!dbg.no_heart_loss) {
77 this.hearts = Math.max(0, this.hearts - (m.boss ? cfg.hearts.boss : cfg.hearts.monster)); 85 this.hearts = Math.max(0, this.hearts - (m.boss ? cfg.hearts.boss : cfg.hearts.monster));
78 } 86 }
79 }; 87 };
80 88
81 Game.prototype.damage = function (m, amount) { 89 Game.prototype.damage = function (m, amount) {
82 if (!m.alive) return; 90 if (!m.alive) return;
83 m.hp -= amount; 91 m.hp -= amount;
84 if (m.hp > 0) return; 92 if (m.hp > 0) {
93 MM.audio.play("monster_hit");
94 return;
95 }
85 m.alive = false; 96 m.alive = false;
97 MM.audio.play(m.boss ? "boss_die" : "monster_die");
86 // coins count at once; the flying coins only show it (balance.md) 98 // coins count at once; the flying coins only show it (balance.md)
87 this.coins += m.coins; 99 this.coins += m.coins;
88 var n = Math.min(m.coins, cfg.coins.max_sprites); 100 var n = Math.min(m.coins, cfg.coins.max_sprites);
89 for (var i = 0; i < n; i++) { 101 for (var i = 0; i < n; i++) {
90 this.flights.push({ 102 this.flights.push({
96 }); 108 });
97 } 109 }
98 }; 110 };
99 111
100 Game.prototype.startEarly = function () { 112 Game.prototype.startEarly = function () {
101 this.coins += this.waves.startEarly(); 113 var bonus = this.waves.startEarly();
114 this.coins += bonus;
115 if (bonus > 0) MM.audio.play("early_bonus");
102 }; 116 };
103 117
104 Game.prototype.towerAt = function (x, y) { 118 Game.prototype.towerAt = function (x, y) {
105 var r = cfg.placement.footprint_radius; 119 var r = cfg.placement.footprint_radius;
106 for (var i = 0; i < this.towers.length; i++) { 120 for (var i = 0; i < this.towers.length; i++) {
139 Game.prototype.build = function () { 153 Game.prototype.build = function () {
140 var cost = cfg.tower[this.shop].cost; 154 var cost = cfg.tower[this.shop].cost;
141 if (!this.canAfford(cost)) return false; 155 if (!this.canAfford(cost)) return false;
142 this.pay(cost); 156 this.pay(cost);
143 this.towers.push(MM.tower.create(this.shop, this.preview.x, this.preview.y)); 157 this.towers.push(MM.tower.create(this.shop, this.preview.x, this.preview.y));
158 MM.audio.play("tower_built");
144 this.shop = null; 159 this.shop = null;
145 this.preview = null; 160 this.preview = null;
146 return true; 161 return true;
147 }; 162 };
148 163