|
1
|
1 // monsters: movement along a route, hops, hp
|
|
|
2
|
|
|
3 window.MM = window.MM || {};
|
|
|
4
|
|
|
5 MM.monster = (function () {
|
|
|
6 "use strict";
|
|
|
7
|
|
|
8 var cfg = MM.config;
|
|
|
9 // share of the hop cycle spent in the air; the rest is the landing
|
|
|
10 var AIR = 0.8;
|
|
|
11
|
|
|
12 // a new monster of the given kind at the start of a route chosen by
|
|
|
13 // the map's route weights
|
|
|
14 function create(game, kind) {
|
|
|
15 var def = cfg.monster[kind];
|
|
|
16 var map = game.map;
|
|
|
17 var r = MM.util.pick(map.routeWeights(def.speed), game.random);
|
|
|
18 var size = MM.assets.size(kind, cfg.scale.sprite);
|
|
|
19 var m = {
|
|
|
20 kind: kind,
|
|
|
21 boss: !!def.boss,
|
|
|
22 route: map.routes[r],
|
|
|
23 routeIndex: r,
|
|
|
24 s: 0,
|
|
|
25 x: 0,
|
|
|
26 y: 0,
|
|
|
27 seg: 1,
|
|
|
28 hp: def.hp,
|
|
|
29 maxHp: def.hp,
|
|
|
30 speed: def.speed,
|
|
|
31 coins: def.coins,
|
|
|
32 w: size[0],
|
|
|
33 h: 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
|
|
|
36 phase: game.random(),
|
|
|
37 alive: true
|
|
|
38 };
|
|
|
39 m.route.point(0, m);
|
|
|
40 return m;
|
|
|
41 }
|
|
|
42
|
|
|
43 // advance by dt seconds. returns true when the monster reached the exit.
|
|
|
44 function update(m, dt) {
|
|
|
45 m.s += m.speed * dt;
|
|
|
46 m.phase = (m.phase + dt / cfg.animation.hop_time) % 1;
|
|
|
47 m.route.point(m.s, m);
|
|
|
48 return m.s >= m.route.length;
|
|
|
49 }
|
|
|
50
|
|
|
51 function remaining(m) {
|
|
|
52 return m.route.length - m.s;
|
|
|
53 }
|
|
|
54
|
|
|
55 // hop height and stretch for drawing: {lift, sx, sy}
|
|
|
56 function pose(m, time) {
|
|
|
57 var a = cfg.animation, p = m.phase, lift = 0, sx = 1, sy = 1;
|
|
|
58 if (p < AIR) {
|
|
|
59 var k = Math.sin(Math.PI * p / AIR);
|
|
|
60 lift = a.hop_height * k;
|
|
|
61 sx = 1 - a.stretch * k;
|
|
|
62 sy = 1 + a.stretch * k;
|
|
|
63 } else {
|
|
|
64 var q = Math.sin(Math.PI * (p - AIR) / (1 - AIR));
|
|
|
65 sx = 1 + a.squash * q;
|
|
|
66 sy = 1 - a.squash * q;
|
|
|
67 }
|
|
|
68 // 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);
|
|
|
70 return { lift: lift, sx: sx * (1 - bob / 2), sy: sy * (1 + bob) };
|
|
|
71 }
|
|
|
72
|
|
|
73 return { create: create, update: update, remaining: remaining, pose: pose };
|
|
|
74 })();
|