xonecas.ioDon't click! Sean Caetano Martin Sign in
tiny-adventure / web / js / minis.js 61 lines2.1 kB Raw
1import * as THREE from '../vendor/three.module.js';
2import { createFigure } from './figures.js';
3import { createBeast } from './beasts.js';
4import { part } from './props.js';
5import { classColor, factionColor } from './paint.js';
6
7const HERO = 0.85;
8const FOE = 0.9;
9const BOSS = 1.45;
10
11function seedOf(text) {
12  let h = 2166136261;
13  for (const c of String(text)) h = Math.imul(h ^ c.charCodeAt(0), 16777619);
14  return h >>> 0;
15}
16
17export function createMini(unit, { enemy = false, boss = false } = {}) {
18  const accent = enemy ? factionColor(unit.faction || unit.class) : classColor(unit.class);
19  const seed = seedOf(unit.id + unit.name);
20  const model = unit.race === 'beast' ? createBeast(unit.class, seed) : createFigure(unit.race, unit.class, seed, accent);
21  const g = new THREE.Group();
22  part(g, 'cyl', '#2b2621', [0, 0.03, 0], [0.62, 0.06, 0.62]);
23  part(g, 'cyl', enemy ? '#7a2c24' : accent, [0, 0.035, 0], [0.66, 0.03, 0.66]);
24  const body = new THREE.Group();
25  body.position.y = 0.06;
26  body.add(model.object);
27  g.add(body);
28  const size = boss ? BOSS : enemy ? FOE : HERO;
29  g.scale.setScalar(size);
30  Object.assign(g.userData, { body, size, animate: model.animate, swing: model.swing, model, mode: null });
31  return g;
32}
33
34const ASH = new THREE.Color('#5a5650');
35
36export function dim(fig, on) {
37  fig.traverse((o) => {
38    if (!o.isMesh || !o.material.color) return;
39    o.userData.base ||= o.material;
40    if (on && !o.userData.ash) {
41      o.userData.ash = o.userData.base.clone();
42      o.userData.ash.color.lerp(ASH, 0.75);
43    }
44    o.userData.paint = on ? o.userData.ash : o.userData.base;
45    o.material = o.userData.paint;
46  });
47}
48
49export function flash(fig, color = '#ff3020', ms = 160) {
50  const meshes = [];
51  fig.traverse((o) => o.isMesh && o.material.emissive && meshes.push(o));
52  meshes.forEach((o) => {
53    o.userData.paint ||= o.material;
54    const c = o.userData.paint.clone();
55    c.emissive = new THREE.Color(color);
56    c.emissiveIntensity = 1.2;
57    o.material = c;
58  });
59  clearTimeout(fig.userData.flash);
60  fig.userData.flash = setTimeout(() => meshes.forEach((o) => (o.material = o.userData.paint)), ms);
61}