wobblyhead hg > minor_monsters
comparison game/js/game.js @ 8:2c4fed8baed9
art update, persistent sound setting, tower selling as double action
| author | ark |
|---|---|
| date | 2026-10-19 13:37 +0200 |
| parents | 910b26feca76 |
| children | a77857594150 |
comparison
equal
deleted
inserted
replaced
| 7:4fd2da999ba4 | 8:2c4fed8baed9 |
|---|---|
| 19 this.hearts = cfg.hearts.per_map; | 19 this.hearts = cfg.hearts.per_map; |
| 20 this.monsters = []; | 20 this.monsters = []; |
| 21 this.towers = []; | 21 this.towers = []; |
| 22 this.projectiles = []; | 22 this.projectiles = []; |
| 23 this.flights = []; // coins flying to the counter | 23 this.flights = []; // coins flying to the counter |
| 24 this.puffs = []; // death puffs, {x, y, boss, t} | |
| 24 this.waves = new MM.Waves(mapIndex, MM.util.clamp(firstWave - 1, 0, mc.waves.length - 1)); | 25 this.waves = new MM.Waves(mapIndex, MM.util.clamp(firstWave - 1, 0, mc.waves.length - 1)); |
| 25 this.time = 0; | 26 this.time = 0; |
| 26 this.paused = false; | 27 this.paused = false; |
| 27 this.pauses = 0; // number of pauses so far, for the sell refund | 28 this.pauses = 0; // number of pauses so far, for the sell refund |
| 28 this.speeds = cfg.controls.speeds.concat(dbg.extra_speeds); | 29 this.speeds = cfg.controls.speeds.concat(dbg.extra_speeds); |
| 31 this.splash = false; // splash screen shown over the paused map | 32 this.splash = false; // splash screen shown over the paused map |
| 32 // player selection: shop tower type, its snapped preview, a built tower | 33 // player selection: shop tower type, its snapped preview, a built tower |
| 33 this.shop = null; | 34 this.shop = null; |
| 34 this.preview = null; | 35 this.preview = null; |
| 35 this.selected = null; | 36 this.selected = null; |
| 37 this.sellArmed = false; // sell was tapped once and waits for the second tap | |
| 36 } | 38 } |
| 37 | 39 |
| 38 Game.prototype.speed = function () { | 40 Game.prototype.speed = function () { |
| 39 return this.speeds[this.speedIndex]; | 41 return this.speeds[this.speedIndex]; |
| 40 }; | 42 }; |
| 77 this.towers.forEach(function (t) { MM.tower.update(t, self, dt); }); | 79 this.towers.forEach(function (t) { MM.tower.update(t, self, dt); }); |
| 78 this.projectiles.forEach(function (p) { MM.projectile.update(p, self, dt); }); | 80 this.projectiles.forEach(function (p) { MM.projectile.update(p, self, dt); }); |
| 79 this.monsters = this.monsters.filter(function (m) { return m.alive; }); | 81 this.monsters = this.monsters.filter(function (m) { return m.alive; }); |
| 80 this.projectiles = this.projectiles.filter(function (p) { return !p.done; }); | 82 this.projectiles = this.projectiles.filter(function (p) { return !p.done; }); |
| 81 this.flights.forEach(function (f) { f.t += dt; }); | 83 this.flights.forEach(function (f) { f.t += dt; }); |
| 84 this.puffs.forEach(function (p) { p.t += dt; }); | |
| 85 this.puffs = this.puffs.filter(function (p) { return p.t < cfg.animation.death_puff; }); | |
| 82 var flying = this.flights.length; | 86 var flying = this.flights.length; |
| 83 this.flights = this.flights.filter(function (f) { return f.t < cfg.coins.flight_time; }); | 87 this.flights = this.flights.filter(function (f) { return f.t < cfg.coins.flight_time; }); |
| 84 if (this.flights.length < flying) MM.audio.play("coins_arrive"); | 88 if (this.flights.length < flying) MM.audio.play("coins_arrive"); |
| 85 | 89 |
| 86 // the end sound waits for the last exit or death sound | 90 // the end sound waits for the last exit or death sound |
| 90 } else if (this.waves.phase === "done" && this.monsters.length === 0) { | 94 } else if (this.waves.phase === "done" && this.monsters.length === 0) { |
| 91 this.result = "won"; | 95 this.result = "won"; |
| 92 MM.audio.play("map_won", ["monster_die", "boss_die"]); | 96 MM.audio.play("map_won", ["monster_die", "boss_die"]); |
| 93 } | 97 } |
| 94 // the simulation stops here; the coins in flight are counted already | 98 // the simulation stops here; the coins in flight are counted already |
| 95 if (this.result) this.flights = []; | 99 if (this.result) { |
| 100 this.flights = []; | |
| 101 this.puffs = []; | |
| 102 } | |
| 96 }; | 103 }; |
| 97 | 104 |
| 98 // a monster reached the exit | 105 // a monster reached the exit |
| 99 Game.prototype.escape = function (m) { | 106 Game.prototype.escape = function (m) { |
| 100 m.alive = false; | 107 m.alive = false; |
| 111 MM.audio.play("monster_hit"); | 118 MM.audio.play("monster_hit"); |
| 112 return; | 119 return; |
| 113 } | 120 } |
| 114 m.alive = false; | 121 m.alive = false; |
| 115 MM.audio.play(m.boss ? "boss_die" : "monster_die"); | 122 MM.audio.play(m.boss ? "boss_die" : "monster_die"); |
| 123 this.puffs.push({ x: m.x, y: m.y, boss: m.boss, t: 0 }); | |
| 116 // coins count at once; the flying coins only show it (balance.md) | 124 // coins count at once; the flying coins only show it (balance.md) |
| 117 this.coins += m.coins; | 125 this.coins += m.coins; |
| 118 var n = Math.min(m.coins, cfg.coins.max_sprites); | 126 var n = Math.min(m.coins, cfg.coins.max_sprites); |
| 119 for (var i = 0; i < n; i++) { | 127 for (var i = 0; i < n; i++) { |
| 120 this.flights.push({ | 128 this.flights.push({ |
| 151 // a tap on the map at map position x, y. hitRadius is how close the tap | 159 // a tap on the map at map position x, y. hitRadius is how close the tap |
| 152 // must be to the preview to build. returns false when no legal position | 160 // must be to the preview to build. returns false when no legal position |
| 153 // was found for a preview. | 161 // was found for a preview. |
| 154 Game.prototype.tapMap = function (x, y, hitRadius) { | 162 Game.prototype.tapMap = function (x, y, hitRadius) { |
| 155 var t = this.towerAt(x, y); | 163 var t = this.towerAt(x, y); |
| 164 this.sellArmed = false; | |
| 156 if (t && !(this.preview && Math.hypot(this.preview.x - x, this.preview.y - y) <= hitRadius)) { | 165 if (t && !(this.preview && Math.hypot(this.preview.x - x, this.preview.y - y) <= hitRadius)) { |
| 157 this.shop = null; | 166 this.shop = null; |
| 158 this.preview = null; | 167 this.preview = null; |
| 159 this.selected = t === this.selected ? null : t; | 168 this.selected = t === this.selected ? null : t; |
| 160 return true; | 169 return true; |
| 198 t.level++; | 207 t.level++; |
| 199 t.paid.push({ cost: cost, pause: this.pauseNumber() }); | 208 t.paid.push({ cost: cost, pause: this.pauseNumber() }); |
| 200 MM.audio.play("tower_upgraded"); | 209 MM.audio.play("tower_upgraded"); |
| 201 }; | 210 }; |
| 202 | 211 |
| 212 // the first tap arms selling, the second sells | |
| 203 Game.prototype.sell = function () { | 213 Game.prototype.sell = function () { |
| 204 var t = this.selected; | 214 var t = this.selected; |
| 205 if (!t) return; | 215 if (!t) return; |
| 216 if (!this.sellArmed) { | |
| 217 this.sellArmed = true; | |
| 218 return; | |
| 219 } | |
| 220 this.sellArmed = false; | |
| 206 this.coins += MM.tower.refund(t, this.pauseNumber()); | 221 this.coins += MM.tower.refund(t, this.pauseNumber()); |
| 207 this.towers.splice(this.towers.indexOf(t), 1); | 222 this.towers.splice(this.towers.indexOf(t), 1); |
| 208 this.selected = null; | 223 this.selected = null; |
| 209 MM.audio.play("tower_sold"); | 224 MM.audio.play("tower_sold"); |
| 210 }; | 225 }; |
