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