comparison game/js/game.js @ 24:3f2811aebb3d 0.9.0

splash screen buttons, help button, info screen
author ark
date 2026-10-20 15:58 +0200
parents 2f555a54c332
children 75deda08db90
comparison
equal deleted inserted replaced
23:2f555a54c332 24:3f2811aebb3d
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.puffs = []; // death puffs, {x, y, boss, t}
25 this.freezes = []; // t2 freeze splashes, {x, y, r, t}
25 this.waves = new MM.Waves(mapIndex, MM.util.clamp(firstWave - 1, 0, mc.waves.length - 1), 26 this.waves = new MM.Waves(mapIndex, MM.util.clamp(firstWave - 1, 0, mc.waves.length - 1),
26 this.random); 27 this.random);
27 this.time = 0; 28 this.time = 0;
28 this.paused = false; 29 this.paused = false;
29 this.speeds = cfg.controls.speeds.concat(dbg.extra_speeds); 30 this.speeds = cfg.controls.speeds.concat(dbg.extra_speeds);
30 this.speedIndex = 0; 31 this.speedIndex = 0;
31 this.result = null; // "won" or "lost" 32 this.result = null; // "won" or "lost"
32 this.splash = false; // splash screen shown over the paused map 33 this.splash = false; // splash screen shown over the paused map
34 this.help = false; // help popup shown over everything
35 this.helpResume = false; // the game ran when the help popup opened
33 // player selection: shop tower type, its snapped preview, a built tower 36 // player selection: shop tower type, its snapped preview, a built tower
34 this.shop = null; 37 this.shop = null;
35 this.preview = null; 38 this.preview = null;
36 this.selected = null; 39 this.selected = null;
37 this.sellArmed = false; // sell was tapped once and waits for the second tap 40 this.sellArmed = false; // sell was tapped once and waits for the second tap
55 this.selected = null; 58 this.selected = null;
56 this.sellArmed = false; 59 this.sellArmed = false;
57 } 60 }
58 }; 61 };
59 62
63 // the help popup pauses the game while it is shown. closing it lets a
64 // game that was running before go on
65 Game.prototype.openHelp = function () {
66 this.help = true;
67 this.helpResume = !this.paused;
68 if (!this.paused) this.togglePause();
69 };
70
71 Game.prototype.closeHelp = function () {
72 this.help = false;
73 if (this.helpResume) this.togglePause();
74 };
75
60 Game.prototype.canAfford = function (cost) { 76 Game.prototype.canAfford = function (cost) {
61 return dbg.unlimited_coins || this.coins >= cost; 77 return dbg.unlimited_coins || this.coins >= cost;
62 }; 78 };
63 79
64 Game.prototype.pay = function (cost) { 80 Game.prototype.pay = function (cost) {
83 this.monsters = this.monsters.filter(function (m) { return m.alive; }); 99 this.monsters = this.monsters.filter(function (m) { return m.alive; });
84 this.projectiles = this.projectiles.filter(function (p) { return !p.done; }); 100 this.projectiles = this.projectiles.filter(function (p) { return !p.done; });
85 this.flights.forEach(function (f) { f.t += dt; }); 101 this.flights.forEach(function (f) { f.t += dt; });
86 this.puffs.forEach(function (p) { p.t += dt; }); 102 this.puffs.forEach(function (p) { p.t += dt; });
87 this.puffs = this.puffs.filter(function (p) { return p.t < cfg.animation.death_puff; }); 103 this.puffs = this.puffs.filter(function (p) { return p.t < cfg.animation.death_puff; });
104 this.freezes.forEach(function (f) { f.t += dt; });
105 this.freezes = this.freezes.filter(function (f) { return f.t < cfg.animation.freeze_splash; });
88 var flying = this.flights.length; 106 var flying = this.flights.length;
89 this.flights = this.flights.filter(function (f) { return f.t < cfg.coins.flight_time; }); 107 this.flights = this.flights.filter(function (f) { return f.t < cfg.coins.flight_time; });
90 if (this.flights.length < flying) MM.audio.play("coins_arrive"); 108 if (this.flights.length < flying) MM.audio.play("coins_arrive");
91 109
92 // the end sound waits for the last exit or death sound 110 // the end sound waits for the last exit or death sound
99 } 117 }
100 // the simulation stops here; the coins in flight are counted already 118 // the simulation stops here; the coins in flight are counted already
101 if (this.result) { 119 if (this.result) {
102 this.flights = []; 120 this.flights = [];
103 this.puffs = []; 121 this.puffs = [];
122 this.freezes = [];
104 } 123 }
105 }; 124 };
106 125
107 // a monster reached the exit 126 // a monster reached the exit
108 Game.prototype.escape = function (m) { 127 Game.prototype.escape = function (m) {
109 m.alive = false; 128 m.alive = false;
110 MM.audio.play(m.boss ? "boss_exit" : "monster_exit"); 129 MM.audio.play(m.boss ? "boss_exit" : "monster_exit");
111 if (!dbg.no_heart_loss) { 130 if (!dbg.no_heart_loss) {
112 this.hearts = Math.max(0, this.hearts - (m.boss ? cfg.hearts.boss : cfg.hearts.monster)); 131 this.hearts = Math.max(0, this.hearts - (m.boss ? cfg.hearts.boss : cfg.hearts.monster));
113 } 132 }
133 };
134
135 // a freeze splash of radius r at x, y: slows every monster whose hit
136 // area it touches to factor of its speed for time seconds
137 Game.prototype.freeze = function (x, y, r, factor, time) {
138 this.freezes.push({ x: x, y: y, r: r, t: 0 });
139 this.monsters.forEach(function (m) {
140 if (m.alive && Math.hypot(m.x - x, m.y - y) <= r + m.hitRadius) {
141 MM.monster.slow(m, factor, time);
142 }
143 });
114 }; 144 };
115 145
116 Game.prototype.damage = function (m, amount) { 146 Game.prototype.damage = function (m, amount) {
117 if (!m.alive) return; 147 if (!m.alive) return;
118 m.hp -= amount; 148 m.hp -= amount;