xonecas.ioDon't click! Sean Caetano Martin Sign in
tiny-adventure / web / js / route.js 44 lines1.4 kB Raw
1import * as THREE from '../vendor/three.module.js';
2import { label } from './props.js';
3import { T } from './theme.js';
4
5const MAX = 128;
6
7export function createRoute(scene) {
8  const dots = new THREE.InstancedMesh(new THREE.CylinderGeometry(0.11, 0.11, 0.03, 12), new THREE.MeshBasicMaterial({ color: T.accent, transparent: true, opacity: 0.9, depthWrite: false, toneMapped: false }), MAX);
9  dots.frustumCulled = false;
10  dots.count = 0;
11  scene.add(dots);
12  const m = new THREE.Matrix4();
13  let tag = null;
14
15  function clear() {
16    dots.count = 0;
17    if (tag) {
18      scene.remove(tag);
19      tag.material.dispose();
20      tag = null;
21    }
22  }
23
24  function show(route, worldPos) {
25    clear();
26    const path = route.path.slice(0, MAX);
27    if (!path.length) return;
28    path.forEach(([x, y], i) => {
29      const p = worldPos(x, y);
30      dots.setMatrixAt(i, m.makeTranslation(p.x, p.y + 0.07, p.z));
31    });
32    dots.count = path.length;
33    dots.instanceMatrix.needsUpdate = true;
34    const n = route.path.length;
35    const notes = [`${n} ${n === 1 ? 'hex' : 'hexes'}`, route.hazard && `hazards cost ${route.hazard} hp`, route.dusk && 'night falls on the way', route.blocked && 'stops at a fight'].filter(Boolean);
36    tag = label(notes.join(', '));
37    tag.scale.multiplyScalar(0.7);
38    const end = worldPos(...path.at(-1));
39    tag.position.set(end.x, end.y + 0.75, end.z);
40    scene.add(tag);
41  }
42
43  return { show, clear };
44}