xonecas.ioDon't click! Sean Caetano Martin Sign in
tiny-adventure / web / js / props.js 169 lines7.2 kB Raw
1import * as THREE from '../vendor/three.module.js';
2import { T } from './theme.js';
3import { labelTexture } from './text.js';
4import { rarityColor } from './theme.js';
5
6const matCache = {};
7export const mat = (color, extra = {}) => {
8  const k = color + JSON.stringify(extra);
9  return (matCache[k] ||= new THREE.MeshStandardMaterial({ color, flatShading: true, roughness: 0.85, ...extra }));
10};
11
12const G = {
13  box: new THREE.BoxGeometry(1, 1, 1),
14  cone: new THREE.ConeGeometry(0.5, 1, 6),
15  cyl: new THREE.CylinderGeometry(0.5, 0.5, 1, 8),
16  sph: new THREE.IcosahedronGeometry(0.5, 1),
17};
18
19export function part(parent, geo, color, [x, y, z], [sx, sy, sz], extra) {
20  const m = new THREE.Mesh(G[geo], typeof color === 'string' ? mat(color, extra) : color);
21  m.position.set(x, y, z);
22  m.scale.set(sx, sy, sz);
23  m.castShadow = true;
24  m.receiveShadow = true;
25  parent.add(m);
26  return m;
27}
28
29function banner(g, color, y) {
30  part(g, 'cyl', '#4a3320', [0.45, y / 2, 0.45], [0.05, y, 0.05]);
31  const pivot = new THREE.Group();
32  pivot.position.set(0.45, y - 0.18, 0.45);
33  pivot.userData.wave = true;
34  g.add(pivot);
35  const f = part(pivot, 'box', color, [0.17, 0, 0], [0.35, 0.28, 0.03]);
36  f.userData.flag = true;
37}
38
39const POIS = {
40  mine(g) {
41    part(g, 'cone', '#6e6258', [0, 0.45, 0], [1.7, 0.9, 1.5]);
42    part(g, 'box', '#120d0a', [0, 0.22, 0.55], [0.5, 0.44, 0.2]);
43    part(g, 'box', '#6a4a2a', [-0.3, 0.26, 0.58], [0.08, 0.52, 0.1]);
44    part(g, 'box', '#6a4a2a', [0.3, 0.26, 0.58], [0.08, 0.52, 0.1]);
45    part(g, 'box', '#6a4a2a', [0, 0.52, 0.58], [0.7, 0.08, 0.1]);
46  },
47  beach(g) {
48    part(g, 'cyl', '#8a6a3e', [0, 0.5, 0], [0.12, 1, 0.12]).rotation.z = 0.2;
49    for (let i = 0; i < 5; i++) {
50      const l = part(g, 'box', '#3f9a4a', [Math.cos(i * 1.26) * 0.28 + 0.1, 1.0, Math.sin(i * 1.26) * 0.28], [0.6, 0.04, 0.16]);
51      l.rotation.y = -i * 1.26;
52      l.rotation.z = -0.4;
53    }
54    part(g, 'sph', '#e8dcc0', [-0.45, 0.12, 0.4], [0.35, 0.2, 0.3]);
55  },
56  ruins(g) {
57    [[-0.45, 0.4], [0.35, 0.7], [0.0, 0.3], [0.45, -0.4]].forEach(([x, h], i) => part(g, 'cyl', '#a29d8f', [x, h / 2, (i - 1.5) * 0.3], [0.22, h, 0.22]));
58    part(g, 'box', '#8e897c', [-0.1, 0.1, 0.4], [0.9, 0.2, 0.3]);
59  },
60  crypt(g) {
61    part(g, 'box', '#7f8a84', [0, 0.3, 0], [1.1, 0.6, 0.9]);
62    part(g, 'cone', '#5d6862', [0, 0.75, 0], [1.3, 0.35, 1.1]).rotation.y = Math.PI / 4;
63    part(g, 'box', '#d8d4c4', [0.45, 0.45, 0.55], [0.08, 0.5, 0.08]);
64    part(g, 'box', '#d8d4c4', [0.45, 0.55, 0.55], [0.3, 0.08, 0.08]);
65  },
66  village(g) {
67    [[-0.4, -0.2], [0.35, 0.15], [-0.05, 0.5]].forEach(([x, z], i) => {
68      part(g, 'box', '#d8c49a', [x, 0.2, z], [0.5, 0.4, 0.45]);
69      part(g, 'cone', i % 2 ? '#a8472f' : '#8a4a2a', [x, 0.55, z], [0.75, 0.4, 0.75]).rotation.y = Math.PI / 4;
70    });
71  },
72  tower(g) {
73    part(g, 'cyl', '#a9a597', [0, 0.7, 0], [0.7, 1.4, 0.7]);
74    part(g, 'cyl', '#8e8a7c', [0, 1.45, 0], [0.9, 0.18, 0.9]);
75    part(g, 'cone', '#5b3a7a', [0, 1.8, 0], [0.8, 0.7, 0.8]);
76  },
77  camp(g) {
78    part(g, 'cone', '#a8472f', [-0.3, 0.35, 0], [0.8, 0.7, 0.8]);
79    part(g, 'cone', '#c9a24a', [0.4, 0.28, 0.3], [0.6, 0.56, 0.6]);
80    part(g, 'cone', '#ff8a24', [0.2, 0.12, -0.45], [0.2, 0.24, 0.2], { emissive: '#ff6a10', emissiveIntensity: 1.2 });
81  },
82};
83
84export function buildPOI(poi, color, big) {
85  const g = new THREE.Group();
86  (POIS[poi.kind] || POIS.camp)(g);
87  banner(g, poi.cleared ? '#6e6e6e' : color, big ? 1.6 : 1.2);
88  if (big) g.scale.setScalar(1.35);
89  g.userData.poi = poi.id;
90  return g;
91}
92
93export function setCleared(g, cleared, color) {
94  g.traverse((o) => {
95    if (o.userData.flag) o.material = mat(cleared ? '#6e6e6e' : color);
96  });
97}
98
99const SPAWNS = {
100  shrine: (g) => { part(g, 'box', '#bdb7a6', [0, 0.15, 0], [0.7, 0.3, 0.7]); part(g, 'box', '#d4cebb', [0, 0.55, 0], [0.3, 0.5, 0.3]); part(g, 'cone', '#ffd27a', [0, 0.95, 0], [0.25, 0.3, 0.25], { emissive: '#ff9a20' }); },
101  'burned inn': (g) => { part(g, 'box', '#2b2420', [0, 0.25, 0], [0.9, 0.5, 0.7]); part(g, 'box', '#1a1512', [0.2, 0.7, 0], [0.12, 0.5, 0.5]); },
102  crossroads: (g) => { part(g, 'cyl', '#6a4a2a', [0, 0.5, 0], [0.08, 1, 0.08]); part(g, 'box', '#8a6a3e', [0.2, 0.8, 0], [0.5, 0.15, 0.04]); part(g, 'box', '#8a6a3e', [-0.2, 0.6, 0], [0.5, 0.15, 0.04]); },
103  watchpost: (g) => { part(g, 'box', '#6a4a2a', [0, 0.6, 0], [0.6, 0.08, 0.6]); [[-1, -1], [1, -1], [-1, 1], [1, 1]].forEach(([a, b]) => part(g, 'cyl', '#5a3d22', [a * 0.25, 0.3, b * 0.25], [0.07, 0.6, 0.07])); part(g, 'cone', '#8a4a2a', [0, 0.95, 0], [0.8, 0.4, 0.8]); },
104  wagon: (g) => { part(g, 'box', '#8a6a3e', [0, 0.3, 0], [0.9, 0.3, 0.5]); part(g, 'cyl', '#3a2a1a', [-0.3, 0.2, 0.3], [0.35, 0.06, 0.35]).rotation.x = Math.PI / 2; part(g, 'cyl', '#3a2a1a', [0.3, 0.2, 0.3], [0.35, 0.06, 0.35]).rotation.x = Math.PI / 2; part(g, 'cone', '#d8c49a', [0, 0.65, 0], [0.8, 0.4, 0.45]); },
105};
106
107export function buildSpawn(sp) {
108  const g = new THREE.Group();
109  (SPAWNS[sp.kind] || SPAWNS.crossroads)(g);
110  const ring = new THREE.Mesh(new THREE.TorusGeometry(0.55, 0.04, 6, 24), mat('#ffd27a', { emissive: '#ffb030', emissiveIntensity: 0.8 }));
111  ring.rotation.x = Math.PI / 2;
112  ring.position.y = 0.05;
113  g.add(ring);
114  return g;
115}
116
117export function buildLoot(item) {
118  const g = new THREE.Group();
119  part(g, 'box', '#7a4f26', [0, 0.14, 0], [0.42, 0.28, 0.3]);
120  part(g, 'cyl', '#7a4f26', [0, 0.28, 0], [0.3, 0.42, 0.3]).rotation.z = Math.PI / 2;
121  part(g, 'box', '#d9b23a', [0, 0.17, 0.16], [0.08, 0.1, 0.03]);
122  const c = rarityColor(item.rarity);
123  const gem = part(g, 'sph', c, [0, 0.7, 0], [0.18, 0.24, 0.18], { emissive: c, emissiveIntensity: 1 });
124  g.userData.gem = gem;
125  return g;
126}
127
128const LANDMARKS = {
129  lookout(g) {
130    [[-0.2, 0], [0.2, 0.05], [0, 0.22]].forEach(([x, z], i) => part(g, 'sph', '#8e897c', [x, 0.12 + i * 0.02, z], [0.32, 0.24, 0.3]));
131    part(g, 'box', '#a29d8f', [0, 0.45, 0.08], [0.22, 0.4, 0.22]);
132    part(g, 'cyl', '#4a3320', [0, 0.95, 0.08], [0.05, 0.7, 0.05]);
133    part(g, 'box', '#c9a24a', [0.15, 1.18, 0.08], [0.28, 0.18, 0.02]).userData.flag = true;
134  },
135  shrine(g) {
136    part(g, 'cyl', '#8e897c', [0, 0.06, 0], [0.8, 0.12, 0.8]);
137    part(g, 'box', '#bdb7a6', [0, 0.5, 0], [0.18, 0.8, 0.12]);
138    part(g, 'box', '#bdb7a6', [0, 0.72, 0], [0.5, 0.1, 0.12]);
139    const glow = part(g, 'sph', '#ffe6a0', [0, 1.05, 0], [0.18, 0.18, 0.18], { emissive: '#ffc040', emissiveIntensity: 1.4 });
140    glow.userData.flag = true;
141  },
142  cache(g) {
143    part(g, 'box', '#8e897c', [-0.2, 0.15, 0], [0.4, 0.3, 0.5]);
144    part(g, 'box', '#a29d8f', [0.25, 0.1, 0.1], [0.3, 0.2, 0.3]).rotation.y = 0.5;
145    part(g, 'box', '#d8cfb0', [-0.2, 0.34, 0.05], [0.26, 0.06, 0.2]).rotation.y = 0.3;
146    part(g, 'box', '#c9a24a', [0.25, 0.24, 0.1], [0.18, 0.08, 0.14]).userData.flag = true;
147  },
148};
149
150export function buildLandmark(lm) {
151  const g = new THREE.Group();
152  LANDMARKS[lm.kind](g);
153  setUsed(g, lm.used);
154  return g;
155}
156
157export function setUsed(g, used) {
158  g.traverse((o) => {
159    if (o.userData.flag) o.material = used ? mat('#6e6e6e') : o.userData.paint || (o.userData.paint = o.material);
160  });
161}
162
163export function label(text) {
164  const t = labelTexture(text, T.fg, 40);
165  const s = new THREE.Sprite(new THREE.SpriteMaterial({ map: t.texture, transparent: true, depthTest: false }));
166  s.scale.set(0.5 * t.aspect, 0.5, 1);
167  s.renderOrder = 10;
168  return s;
169}