comparison game/js/monster.js @ 6:910b26feca76

full game flow
author ark
date 2026-10-19 01:05 +0200
parents 5492efd60e2c
children 22b604680c5a
comparison
equal deleted inserted replaced
5:67bf3c505145 6:910b26feca76
1 // monsters: movement along a route, hops, hp 1 // monsters: movement along a route, hops, slow, hp
2 2
3 window.MM = window.MM || {}; 3 window.MM = window.MM || {};
4 4
5 MM.monster = (function () { 5 MM.monster = (function () {
6 "use strict"; 6 "use strict";
32 w: size[0], 32 w: size[0],
33 h: size[1], 33 h: size[1],
34 hitRadius: cfg.animation.hit_radius * Math.min(size[0], size[1]), 34 hitRadius: cfg.animation.hit_radius * Math.min(size[0], size[1]),
35 // start at a random point of the hop cycle, so a group does not hop in step 35 // start at a random point of the hop cycle, so a group does not hop in step
36 phase: game.random(), 36 phase: game.random(),
37 slows: [], // running slows, {factor, left}
37 alive: true 38 alive: true
38 }; 39 };
39 m.route.point(0, m); 40 m.route.point(0, m);
40 return m; 41 return m;
41 } 42 }
42 43
44 // share of its speed a monster keeps: the strongest running slow
45 // applies, slows do not add up. 1 when not slowed.
46 function slowFactor(m) {
47 var k = 1;
48 for (var i = 0; i < m.slows.length; i++) k = Math.min(k, m.slows[i].factor);
49 return k;
50 }
51
52 // slow to factor of the speed for time seconds. a new hit of the same
53 // strength restarts its time.
54 function slow(m, factor, time) {
55 for (var i = 0; i < m.slows.length; i++) {
56 if (m.slows[i].factor === factor) {
57 m.slows[i].left = time;
58 return;
59 }
60 }
61 m.slows.push({ factor: factor, left: time });
62 }
63
43 // advance by dt seconds. returns true when the monster reached the exit. 64 // advance by dt seconds. returns true when the monster reached the exit.
44 function update(m, dt) { 65 function update(m, dt) {
45 m.s += m.speed * dt; 66 for (var i = m.slows.length - 1; i >= 0; i--) {
46 m.phase = (m.phase + dt / cfg.animation.hop_time) % 1; 67 m.slows[i].left -= dt;
68 if (m.slows[i].left <= 0) m.slows.splice(i, 1);
69 }
70 // a slowed monster also hops slower
71 var k = slowFactor(m);
72 m.s += m.speed * k * dt;
73 m.phase = (m.phase + k * dt / cfg.animation.hop_time) % 1;
47 m.route.point(m.s, m); 74 m.route.point(m.s, m);
48 return m.s >= m.route.length; 75 return m.s >= m.route.length;
49 } 76 }
50 77
51 function remaining(m) { 78 function remaining(m) {
68 // slow breathing on top of the hops 95 // slow breathing on top of the hops
69 var bob = a.idle_bob * Math.sin(time * 2 * Math.PI / 1.3 + m.phase * 6.28); 96 var bob = a.idle_bob * Math.sin(time * 2 * Math.PI / 1.3 + m.phase * 6.28);
70 return { lift: lift, sx: sx * (1 - bob / 2), sy: sy * (1 + bob) }; 97 return { lift: lift, sx: sx * (1 - bob / 2), sy: sy * (1 + bob) };
71 } 98 }
72 99
73 return { create: create, update: update, remaining: remaining, pose: pose }; 100 return { create: create, update: update, remaining: remaining, pose: pose,
101 slow: slow, slowFactor: slowFactor };
74 })(); 102 })();