wobblyhead hg > minor_monsters
view game/js/monster.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 | 22b604680c5a |
line wrap: on
line source
// monsters: movement along a route, hops, slow, 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(), slows: [], // running slows, {factor, left} alive: true }; m.route.point(0, m); return m; } // share of its speed a monster keeps: the strongest running slow // applies, slows do not add up. 1 when not slowed. function slowFactor(m) { var k = 1; for (var i = 0; i < m.slows.length; i++) k = Math.min(k, m.slows[i].factor); return k; } // slow to factor of the speed for time seconds. a new hit of the same // strength restarts its time. function slow(m, factor, time) { for (var i = 0; i < m.slows.length; i++) { if (m.slows[i].factor === factor) { m.slows[i].left = time; return; } } m.slows.push({ factor: factor, left: time }); } // advance by dt seconds. returns true when the monster reached the exit. function update(m, dt) { for (var i = m.slows.length - 1; i >= 0; i--) { m.slows[i].left -= dt; if (m.slows[i].left <= 0) m.slows.splice(i, 1); } // a slowed monster also hops slower var k = slowFactor(m); m.s += m.speed * k * dt; m.phase = (m.phase + k * 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, slow: slow, slowFactor: slowFactor }; })();
