xonecas.ioDon't click! Sean Caetano Martin Sign in
tiny-adventure / web / js / beasts.js 113 lines8.6 kB Raw
1import * as THREE from '../vendor/three.module.js';
2import { box, mesh, spike } from './figures.js';
3
4const BONE = '#e8e0c8', EYE = '#ffd84a', HORN = '#e8e0c8', DARK = '#1a1410', BEAK = '#d8b040', FANG = '#f0ead6';
5const BLEND_RATE = 8;
6
7const pivot = (x, y, z, ...children) => {
8  const group = new THREE.Group().add(...children);
9  group.position.set(x, y, z);
10  return group;
11};
12const limb = (x, y, z, w, l, color) => pivot(x, y, z, mesh(box(w, l, w), color, 0, -l / 2));
13const eyes = (x, y, z, r, color = EYE) => [-1, 1].map((s) => mesh(box(r, r, 0.01), color, s * x, y, z));
14const blob = (r, sx, sy, sz, color, x = 0, y = 0, z = 0) => {
15  const m = mesh(new THREE.IcosahedronGeometry(r, 0), color, x, y, z);
16  m.scale.set(sx, sy, sz);
17  return m;
18};
19const pairs = (make) => [-1, 1].flatMap(make);
20const legs4 = (x, z, y, c, l = 0.24, w = 0.07) => [[x, z], [-x, z], [x, -z], [-x, -z]].map(([a, b]) => limb(a, y, b, w, l, c));
21
22const BEASTS = {
23  owlbear: (c) => ({
24    body: [blob(0.3, 1.1, 0.95, 1.5, c, 0, 0.5), blob(0.22, 1.1, 0.8, 0.8, '#8a6a42', 0, 0.5, 0.2), blob(0.2, 1, 1.1, 1.1, c, 0, 0.78, 0.0)],
25    limbs: [[0.2, 0.2], [-0.2, 0.2], [0.2, -0.2], [-0.2, -0.2]].map(([x, z]) => pivot(x, 0.34, z, blob(0.1, 1.2, 2.2, 1.2, c, 0, -0.17), ...[-1, 0, 1].map((i) => mesh(spike(0.02, 0.09, 3), BONE, i * 0.04, -0.34, 0.08, Math.PI)))),
26    head: pivot(0, 0.72, 0.42, blob(0.2, 1.1, 1, 1, '#8a6a42', 0, 0.05, 0.06), mesh(spike(0.07, 0.18), BEAK, 0, 0.0, 0.26, Math.PI / 2), ...eyes(0.09, 0.1, 0.22, 0.07, '#fff0a0'), ...eyes(0.09, 0.1, 0.235, 0.03, DARK), ...pairs((s) => mesh(spike(0.045, 0.14), c, s * 0.13, 0.2, 0.02, 0, -s * 0.4))),
27  }),
28  troll: (c) => ({
29    body: [blob(0.28, 1.2, 1.2, 1, c, 0, 0.75), blob(0.3, 1.1, 0.9, 1, '#7f8f6a', 0, 0.58, 0.12), mesh(box(0.4, 0.14, 0.3), '#4a3a2a', 0, 0.45), ...pairs((s) => blob(0.12, 1.2, 0.8, 1.1, '#7f8f6a', s * 0.34, 1.05, 0)), mesh(box(0.46, 0.1, 0.34), '#8a6a42', 0, 0.42, 0), ...[0, 1, 2, 3, 4].map((i) => mesh(spike(0.025, 0.1, 3), '#4a5a3a', (i - 2) * 0.08, 1.12, -0.18, -1.2))],
30    limbs: [limb(0.13, 0.5, 0, 0.17, 0.5, c), limb(-0.13, 0.5, 0, 0.17, 0.5, c), pivot(-0.38, 0.98, 0.05, blob(0.14, 1, 2.6, 1, c, 0, -0.3), blob(0.14, 1.2, 1, 1.2, c, 0, -0.62)),
31      pivot(0.38, 0.98, 0.05, blob(0.14, 1, 2.6, 1, c, 0, -0.3), blob(0.14, 1.2, 1, 1.2, c, 0, -0.62), mesh(new THREE.CylinderGeometry(0.06, 0.11, 0.7, 6), '#6a4a2a', 0, -0.62, 0.2, 1.5))],
32    head: pivot(0, 1.0, 0.16, blob(0.16, 1.2, 1, 1.05, c, 0, 0.1, 0.04), mesh(spike(0.045, 0.14), c, 0, 0.08, 0.2, Math.PI / 2 + 0.4), ...eyes(0.06, 0.15, 0.17, 0.045, '#ffe060'), ...pairs((s) => [mesh(spike(0.025, 0.12), FANG, s * 0.07, 0.0, 0.15), mesh(spike(0.04, 0.1), c, s * 0.2, 0.12, 0, 0, -s * 1.2)]).flat()),
33  }),
34  spider: (c) => ({
35    body: [mesh(new THREE.IcosahedronGeometry(0.2, 0), c, 0, 0.24, -0.2), mesh(box(0.1, 0.02, 0.14), '#a02a20', 0, 0.43, -0.22)],
36    limbs: [0.12, 0.04, -0.04, -0.12].flatMap((z) => pairs((s) => pivot(s * 0.1, 0.2, z + 0.06, mesh(box(0.3, 0.03, 0.03), c, s * 0.13, 0.05, 0, 0, s * 0.5), mesh(box(0.03, 0.34, 0.03), c, s * 0.31, -0.04, 0, 0, s * 0.3)))).map((leg, i) => ((leg.rotation.y = (i & 1 ? 1 : -1) * ((i >> 1) * 0.35 - 0.5)), leg)),
37    head: pivot(0, 0.2, 0.06, mesh(new THREE.IcosahedronGeometry(0.12, 0), c, 0, 0.02, 0.04), ...eyes(0.04, 0.07, 0.15, 0.03, '#d02020'), ...pairs((s) => mesh(spike(0.015, 0.08), FANG, s * 0.03, -0.04, 0.14, Math.PI))),
38  }),
39  dragon: (c, belly) => ({
40    body: [blob(0.3, 1.3, 1.15, 3, c, 0, 0.6), blob(0.27, 1.2, 0.5, 2.7, belly, 0, 0.45), mesh(spike(0.16, 0.7).rotateX(-Math.PI / 2), c, 0, 0.6, -0.42), blob(0.13, 1.1, 1.1, 3.4, c, 0, 0.92, 0.5).rotateX(-0.7)],
41    limbs: [[0.2, 0.3], [-0.2, 0.3], [0.2, -0.3], [-0.2, -0.3]].map(([x, z]) => pivot(x, 0.48, z, blob(0.1, 1.2, 2.6, 1.2, c, 0, -0.24))),
42    head: pivot(0, 1.08, 0.72, blob(0.13, 1.05, 0.95, 2.2, c, 0, 0, 0.12), blob(0.1, 1.1, 0.5, 2.1, belly, 0, -0.08, 0.12), ...eyes(0.08, 0.05, 0.31, 0.04), ...pairs((s) => mesh(spike(0.035, 0.24), HORN, s * 0.08, 0.08, -0.04, -1.1))),
43    wings: pairs((s) => pivot(s * 0.2, 0.78, 0.1, mesh(box(0.45, 0.02, 0.6), c, s * 0.24, 0, -0.15), mesh(box(0.45, 0.02, 0.3), c, s * 0.66, 0, 0), mesh(box(0.9, 0.04, 0.04), HORN, s * 0.45, 0.02, 0.15))),
44  }),
45  wolf: (c) => ({
46    body: [blob(0.2, 1, 0.9, 1.9, c, 0, 0.36), blob(0.16, 1.1, 1, 1.1, '#cfc6b0', 0, 0.36, 0.22), mesh(spike(0.06, 0.34, 4).rotateX(-2.2), c, 0, 0.42, -0.32)],
47    limbs: legs4(0.1, 0.2, 0.28, c, 0.26, 0.06),
48    head: pivot(0, 0.48, 0.38, blob(0.12, 1, 0.95, 1.1, c, 0, 0, 0), mesh(box(0.1, 0.08, 0.16), c, 0, -0.03, 0.14), mesh(box(0.04, 0.03, 0.02), DARK, 0, 0.0, 0.23), ...eyes(0.05, 0.04, 0.1, 0.03, '#f0d040'), ...pairs((s) => mesh(spike(0.035, 0.1, 3), c, s * 0.06, 0.08, -0.02))),
49  }),
50  boar: (c) => ({
51    body: [blob(0.26, 1.15, 0.95, 1.5, c, 0, 0.36), ...[0, 1, 2, 3].map((i) => mesh(spike(0.03, 0.1, 3), '#2a1e16', 0, 0.56, 0.15 - i * 0.1, -0.4))],
52    limbs: legs4(0.13, 0.18, 0.2, '#3a2a20', 0.18, 0.07),
53    head: pivot(0, 0.38, 0.36, blob(0.14, 1, 0.95, 1.2, c, 0, 0, 0.02), mesh(box(0.1, 0.08, 0.06), '#c8908a', 0, -0.04, 0.17), ...pairs((s) => mesh(spike(0.02, 0.12, 3), FANG, s * 0.07, -0.06, 0.12, -0.5)), ...eyes(0.06, 0.04, 0.13, 0.025, DARK)),
54  }),
55  bat: (c) => ({
56    body: [blob(0.12, 1, 1.2, 1, c, 0, 0.62)],
57    limbs: [],
58    head: pivot(0, 0.76, 0.04, blob(0.08, 1, 1, 1, c), ...pairs((s) => mesh(spike(0.035, 0.1, 3), c, s * 0.05, 0.05, 0)), ...eyes(0.03, 0.01, 0.07, 0.025, '#e02020')),
59    wings: pairs((s) => pivot(s * 0.08, 0.66, 0, mesh(box(0.32, 0.015, 0.22), '#8a3a44', s * 0.16, 0, 0), mesh(spike(0.08, 0.18, 3).rotateZ(s * Math.PI / 2), '#8a3a44', s * 0.32, 0, -0.06))),
60    hover: 0.25,
61  }),
62  crow: (c) => ({
63    body: [blob(0.13, 1, 1, 1.5, c, 0, 0.3), mesh(box(0.12, 0.02, 0.18), c, 0, 0.32, -0.24, -0.3)],
64    limbs: pairs((s) => limb(s * 0.05, 0.18, 0, 0.02, 0.18, BEAK)),
65    head: pivot(0, 0.42, 0.14, blob(0.08, 1, 1, 1, c), mesh(spike(0.03, 0.1, 4), BEAK, 0, -0.01, 0.07, Math.PI / 2), ...eyes(0.04, 0.02, 0.06, 0.02, '#f0e0a0')),
66    wings: pairs((s) => pivot(s * 0.1, 0.34, 0, mesh(box(0.24, 0.015, 0.2), c, s * 0.12, 0, -0.04))),
67  }),
68  mimic: (c) => ({
69    body: [mesh(box(0.5, 0.28, 0.36), c, 0, 0.2, 0), mesh(box(0.52, 0.04, 0.38), '#d9b23a', 0, 0.33, 0), mesh(box(0.36, 0.03, 0.22), '#a0242a', 0, 0.35, 0.02), ...[-2, -1, 0, 1, 2].map((i) => mesh(spike(0.025, 0.07, 3), FANG, i * 0.09, 0.38, 0.15))],
70    limbs: legs4(0.2, 0.12, 0.08, c, 0.08, 0.05),
71    head: pivot(0, 0.34, -0.18, pivot(0, 0, 0, mesh(box(0.5, 0.14, 0.36), c, 0, 0.07, 0.18), mesh(box(0.08, 0.1, 0.03), '#d9b23a', 0, 0.06, 0.37), ...[-2, -1, 0, 1, 2].map((i) => mesh(spike(0.025, 0.07, 3), FANG, i * 0.09, -0.02, 0.33, Math.PI)), ...eyes(0.1, 0.1, 0.365, 0.05, '#ffe040'))),
72  }),
73};
74
75const BEAST_LOOK = {
76  owlbear: { color: '#6a4a2e', scale: 1.4 },
77  troll: { color: '#6a9a4a', scale: 1.5 },
78  spider: { color: '#2a2420', scale: 1.4 },
79  dragon: { color: '#a8302a', belly: '#d8a040', scale: 1.2 },
80  wolf: { color: '#6e6a64', scale: 1.4 },
81  boar: { color: '#5a3e2a', scale: 1.4 },
82  bat: { color: '#6a4a58', scale: 1.6 },
83  crow: { color: '#24242c', scale: 1.8 },
84  mimic: { color: '#7a4f26', scale: 1.6 },
85};
86
87export function createBeast(kind, seed) {
88  const look = BEAST_LOOK[kind] || BEAST_LOOK.wolf;
89  const beast = (BEASTS[kind] || BEASTS.wolf)(look.color, look.belly);
90  const body = new THREE.Group().add(...beast.body, ...beast.limbs, beast.head, ...(beast.wings ?? []));
91  const object = new THREE.Group().add(body);
92  object.scale.setScalar(look.scale);
93  const weights = { walk: 0, fight: 0 };
94  let clock = seed % 10;
95  let stride = 0;
96  let strike = 0;
97
98  function animate(mode, dt, pace = 0) {
99    clock += dt;
100    stride += dt * pace * 2 * Math.PI;
101    for (const m in weights) weights[m] += ((m === mode ? 1 : 0) - weights[m]) * Math.min(1, dt * BLEND_RATE);
102    const { walk, fight } = weights;
103    strike = Math.max(0, Math.sin(clock * 5));
104    beast.limbs.forEach((l, i) => (l.rotation.x = 0.5 * walk * Math.sin(stride + Math.PI * ((i & 1) ^ ((i >> 1) & 1)))));
105    beast.head.rotation.x = kind === 'mimic' ? -0.35 - 0.2 * Math.max(0, Math.sin(clock * 2)) - 0.5 * fight * strike : 0.06 * Math.sin(clock * 2) + 0.5 * fight * strike - 0.2 * fight;
106    body.position.y = (beast.hover ?? 0) * (0.5 + 0.5 * Math.sin(clock * 3)) * 0.2 + 0.01 * Math.sin(clock * 2) + 0.02 * walk * Math.abs(Math.sin(stride));
107    beast.wings?.forEach((w, i) => (w.rotation.z = (i ? 1 : -1) * (0.35 + (0.15 + 0.5 * fight + (beast.hover ? 0.5 : 0)) * Math.sin(clock * (1.5 + 6 * fight + (beast.hover ? 10 : 0))))));
108  }
109
110  const swing = () => (clock = 0);
111
112  return { object, head: beast.head, animate, swing, get strike() { return strike * weights.fight; }, radius: 0.35 * look.scale };
113}