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

first prototype + updated art sheet
author ark
date 2026-10-18 14:50 +0200
parents
children 910b26feca76
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/game/js/monster.js	Sun Oct 18 14:50:20 2026 +0200
@@ -0,0 +1,74 @@
+// monsters: movement along a route, hops, hp
+
+window.MM = window.MM || {};
+
+MM.monster = (function () {
+    "use strict";
+
+    var cfg = MM.config;
+    // share of the hop cycle spent in the air; the rest is the landing
+    var AIR = 0.8;
+
+    // a new monster of the given kind at the start of a route chosen by
+    // the map's route weights
+    function create(game, kind) {
+        var def = cfg.monster[kind];
+        var map = game.map;
+        var r = MM.util.pick(map.routeWeights(def.speed), game.random);
+        var size = MM.assets.size(kind, cfg.scale.sprite);
+        var m = {
+            kind: kind,
+            boss: !!def.boss,
+            route: map.routes[r],
+            routeIndex: r,
+            s: 0,
+            x: 0,
+            y: 0,
+            seg: 1,
+            hp: def.hp,
+            maxHp: def.hp,
+            speed: def.speed,
+            coins: def.coins,
+            w: size[0],
+            h: size[1],
+            hitRadius: cfg.animation.hit_radius * Math.min(size[0], size[1]),
+            // start at a random point of the hop cycle, so a group does not hop in step
+            phase: game.random(),
+            alive: true
+        };
+        m.route.point(0, m);
+        return m;
+    }
+
+    // advance by dt seconds. returns true when the monster reached the exit.
+    function update(m, dt) {
+        m.s += m.speed * dt;
+        m.phase = (m.phase + dt / cfg.animation.hop_time) % 1;
+        m.route.point(m.s, m);
+        return m.s >= m.route.length;
+    }
+
+    function remaining(m) {
+        return m.route.length - m.s;
+    }
+
+    // hop height and stretch for drawing: {lift, sx, sy}
+    function pose(m, time) {
+        var a = cfg.animation, p = m.phase, lift = 0, sx = 1, sy = 1;
+        if (p < AIR) {
+            var k = Math.sin(Math.PI * p / AIR);
+            lift = a.hop_height * k;
+            sx = 1 - a.stretch * k;
+            sy = 1 + a.stretch * k;
+        } else {
+            var q = Math.sin(Math.PI * (p - AIR) / (1 - AIR));
+            sx = 1 + a.squash * q;
+            sy = 1 - a.squash * q;
+        }
+        // slow breathing on top of the hops
+        var bob = a.idle_bob * Math.sin(time * 2 * Math.PI / 1.3 + m.phase * 6.28);
+        return { lift: lift, sx: sx * (1 - bob / 2), sy: sy * (1 + bob) };
+    }
+
+    return { create: create, update: update, remaining: remaining, pose: pose };
+})();