xonecas.ioDon't click! Sean Caetano Martin Sign in
tiny-adventure / web / js / party.js 121 lines3.2 kB Raw
1import * as THREE from '../vendor/three.module.js';
2import { createMini, dim } from './minis.js';
3
4const MARCH = [[-0.27, 0.27], [0.27, 0.27], [-0.27, -0.27], [0.27, -0.27]];
5const RING = [[-0.32, 0.32], [0.32, 0.32], [-0.32, -0.32], [0.32, -0.32]];
6const SPEED = 4;
7const PACE = 3.6;
8const TURN = 8;
9const TOPPLE = 1.35;
10
11export function createParty(scene) {
12  const root = new THREE.Group();
13  scene.add(root);
14  const figures = new Map();
15  const pos = new THREE.Vector3();
16  let route = [];
17  let resolve = null;
18  let heading = 0;
19  let goal = 0;
20  let ring = false;
21  let spread = 1;
22
23  function setUnits(units) {
24    for (const f of figures.values()) root.remove(f);
25    figures.clear();
26    units.forEach((u, i) => {
27      const f = createMini(u);
28      f.userData.slot = i % MARCH.length;
29      figures.set(u.id, f);
30      root.add(f);
31    });
32    sync(units);
33    layout();
34  }
35
36  function topple(f, on) {
37    const [side] = MARCH[f.userData.slot];
38    f.userData.body.rotation.z = on ? (side < 0 ? TOPPLE : -TOPPLE) : 0;
39  }
40
41  function sync(units) {
42    for (const u of units) {
43      const f = figures.get(u.id);
44      if (!f) continue;
45      const down = u.stats.hp <= 0;
46      if (f.userData.down !== down) dim(f, down);
47      f.userData.down = down;
48      topple(f, down);
49    }
50  }
51
52  function layout() {
53    const sx = Math.cos(heading);
54    const sz = -Math.sin(heading);
55    const fx = Math.sin(heading);
56    const fz = Math.cos(heading);
57    for (const f of figures.values()) {
58      const [side, fwd] = (ring ? RING : MARCH)[f.userData.slot].map((k) => k * spread);
59      f.position.set(pos.x + sx * side + fx * fwd, pos.y, pos.z + sz * side + fz * fwd);
60      f.rotation.y = ring ? Math.atan2(pos.x - f.position.x, pos.z - f.position.z) : heading;
61    }
62  }
63
64  function place(p) {
65    pos.copy(p);
66    route = [];
67    layout();
68  }
69
70  function walk(points) {
71    route = points.slice();
72    return new Promise((r) => {
73      if (!route.length) return r();
74      resolve = r;
75    });
76  }
77
78  function face(angle, snap = false) {
79    goal = angle;
80    if (snap) heading = angle;
81  }
82
83  function widen(k) {
84    spread = k;
85    layout();
86  }
87
88  function gather(on) {
89    ring = on;
90    layout();
91  }
92
93  function update(dt) {
94    let step = SPEED * dt;
95    if (route.length && pos.distanceTo(route[0]) > 1e-3) goal = Math.atan2(route[0].x - pos.x, route[0].z - pos.z);
96    const turn = Math.atan2(Math.sin(goal - heading), Math.cos(goal - heading));
97    heading += turn * Math.min(1, dt * TURN);
98    while (route.length && step > 0) {
99      const d = pos.distanceTo(route[0]);
100      if (d <= step) {
101        step -= d;
102        pos.copy(route.shift());
103      } else {
104        pos.lerp(route[0], step / d);
105        step = 0;
106      }
107      if (!route.length && resolve) {
108        const r = resolve;
109        resolve = null;
110        r();
111      }
112    }
113    layout();
114    const moving = route.length > 0;
115    for (const f of figures.values()) {
116      if (!f.userData.down) f.userData.animate(f.userData.mode || (moving ? 'walk' : 'idle'), dt, moving ? PACE : 0);
117    }
118  }
119
120  return { root, figures, pos, setUnits, sync, place, walk, face, widen, gather, topple, update, moving: () => route.length > 0, heading: () => heading };
121}