diff game/js/game.js @ 1:5492efd60e2c

first prototype + updated art sheet
author ark
date 2026-10-18 14:50 +0200
parents
children 2d46729a022e
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/game/js/game.js	Sun Oct 18 14:50:20 2026 +0200
@@ -0,0 +1,150 @@
+// game state and rules: coins, hearts, building, win and loss
+
+window.MM = window.MM || {};
+
+MM.Game = (function () {
+    "use strict";
+
+    var cfg = MM.config;
+    var dbg = cfg.debug;
+
+    function Game(mapIndex, seed) {
+        var mc = cfg.maps[mapIndex];
+        this.map = new MM.GameMap(mapIndex);
+        this.seed = seed;
+        this.random = MM.util.rng(seed);
+        this.coins = dbg.start_coins >= 0 ? dbg.start_coins : mc.starting_coins;
+        this.hearts = cfg.hearts.per_map;
+        this.monsters = [];
+        this.towers = [];
+        this.projectiles = [];
+        this.flights = [];      // coins flying to the counter
+        this.waves = new MM.Waves(mapIndex, MM.util.clamp(dbg.start_wave - 1, 0, mc.waves.length - 1));
+        this.time = 0;
+        this.paused = false;
+        this.speeds = cfg.controls.speeds.concat(dbg.extra_speeds);
+        this.speedIndex = 0;
+        this.result = null;     // "won" or "lost"
+        // player selection: shop tower type, its snapped preview, a built tower
+        this.shop = null;
+        this.preview = null;
+        this.selected = null;
+    }
+
+    Game.prototype.speed = function () {
+        return this.speeds[this.speedIndex];
+    };
+
+    Game.prototype.canAfford = function (cost) {
+        return dbg.unlimited_coins || this.coins >= cost;
+    };
+
+    Game.prototype.pay = function (cost) {
+        if (!dbg.unlimited_coins) this.coins -= cost;
+    };
+
+    // one simulation step of dt seconds
+    Game.prototype.step = function (dt) {
+        if (this.result) return;
+        var self = this;
+        this.time += dt;
+        this.waves.update(dt, this.monsters.length, function (kind) {
+            self.monsters.push(MM.monster.create(self, kind));
+        });
+        this.monsters.forEach(function (m) {
+            if (m.alive && MM.monster.update(m, dt)) self.escape(m);
+        });
+        this.towers.forEach(function (t) { MM.tower.update(t, self, dt); });
+        this.projectiles.forEach(function (p) { MM.projectile.update(p, self, dt); });
+        this.monsters = this.monsters.filter(function (m) { return m.alive; });
+        this.projectiles = this.projectiles.filter(function (p) { return !p.done; });
+        this.flights.forEach(function (f) { f.t += dt; });
+        this.flights = this.flights.filter(function (f) { return f.t < cfg.coins.flight_time; });
+
+        if (this.hearts <= 0) {
+            this.result = "lost";
+        } else if (this.waves.phase === "done" && this.monsters.length === 0) {
+            this.result = "won";
+        }
+        // the simulation stops here; the coins in flight are counted already
+        if (this.result) this.flights = [];
+    };
+
+    // a monster reached the exit
+    Game.prototype.escape = function (m) {
+        m.alive = false;
+        if (!dbg.no_heart_loss) {
+            this.hearts = Math.max(0, this.hearts - (m.boss ? cfg.hearts.boss : cfg.hearts.monster));
+        }
+    };
+
+    Game.prototype.damage = function (m, amount) {
+        if (!m.alive) return;
+        m.hp -= amount;
+        if (m.hp > 0) return;
+        m.alive = false;
+        // coins count at once; the flying coins only show it (balance.md)
+        this.coins += m.coins;
+        var n = Math.min(m.coins, cfg.coins.max_sprites);
+        for (var i = 0; i < n; i++) {
+            this.flights.push({
+                x: m.x + (this.random() - 0.5) * m.w,
+                y: m.y - m.h / 2 + (this.random() - 0.5) * m.h / 2,
+                sprite: "c" + Math.floor(this.random() * 4),
+                // stagger the coins a little
+                t: -i * 0.05
+            });
+        }
+    };
+
+    Game.prototype.startEarly = function () {
+        this.coins += this.waves.startEarly();
+    };
+
+    Game.prototype.towerAt = function (x, y) {
+        var r = cfg.placement.footprint_radius;
+        for (var i = 0; i < this.towers.length; i++) {
+            var t = this.towers[i];
+            if (Math.hypot(t.x - x, t.y - y) <= r) return t;
+        }
+        return null;
+    };
+
+    Game.prototype.chooseShop = function (type) {
+        this.selected = null;
+        this.preview = null;
+        this.shop = this.shop === type ? null : type;
+    };
+
+    // a tap on the map at map position x, y. hitRadius is how close the tap
+    // must be to the preview to build. returns false when no legal position
+    // was found for a preview.
+    Game.prototype.tapMap = function (x, y, hitRadius) {
+        var t = this.towerAt(x, y);
+        if (t && !(this.preview && Math.hypot(this.preview.x - x, this.preview.y - y) <= hitRadius)) {
+            this.shop = null;
+            this.preview = null;
+            this.selected = t === this.selected ? null : t;
+            return true;
+        }
+        this.selected = null;
+        if (!this.shop) return true;
+        if (this.preview && Math.hypot(this.preview.x - x, this.preview.y - y) <= hitRadius) {
+            return this.build();
+        }
+        this.preview = this.map.snap(x, y, this.towers);
+        return this.preview !== null;
+    };
+
+    Game.prototype.build = function () {
+        var cost = cfg.tower[this.shop].cost;
+        if (!this.canAfford(cost)) return false;
+        this.pay(cost);
+        this.towers.push(MM.tower.create(this.shop, this.preview.x, this.preview.y));
+        this.shop = null;
+        this.preview = null;
+        return true;
+    };
+
+    return Game;
+})();