1import * as THREE from '../vendor/three.module.js'; 2import { factionColor } from './paint.js'; 3import { createBoard } from './board.js'; 4import { worldToHex, hexDistance } from './hex.js'; 5import { mat, buildPOI, setCleared, buildSpawn, buildLoot, buildLandmark, setUsed, label } from './props.js'; 6import { createMini } from './minis.js'; 7import { FACTIONS } from './looks.js'; 8import { T } from './theme.js'; 9import { createLife } from './life.js'; 10 11const REVEAL = 6; 12const LOOKOUT = 13; 13const BAND_SPEED = 3; 14 15export function createWorld(scene, state) { 16 const { map, campaign } = state; 17 const group = new THREE.Group(); 18 scene.add(group); 19 const board = createBoard(map); 20 group.add(board.group); 21 const worldPos = (x, y) => board.pos(x, y).setY(Math.max(0, board.pos(x, y).y)); 22 23 const paintOf = Object.fromEntries(campaign.factions.map((f) => [f.id, factionColor(f.id)])); 24 const lastPoi = campaign.main.steps.at(-1)?.poi; 25 const things = []; 26 const poiObjs = {}; 27 const lootObjs = {}; 28 const add = (obj, x, y, lab) => { 29 const p = worldPos(x, y); 30 obj.position.copy(p); 31 obj.visible = false; 32 group.add(obj); 33 let l = null; 34 if (lab) { 35 l = label(lab); 36 l.position.set(p.x, p.y + 1.9, p.z); 37 l.visible = false; 38 group.add(l); 39 } 40 obj.userData.tile = { x, y }; 41 const t = { x, y, obj, label: l, hide: () => false }; 42 things.push(t); 43 return t; 44 }; 45 for (const poi of map.pois) { 46 const c = paintOf[poi.faction] || '#7e3b36'; 47 const t = add(buildPOI(poi, c, poi.id === lastPoi), poi.x, poi.y, `${poi.name} Lv ${poi.level}`); 48 poiObjs[poi.id] = { t, c, poi }; 49 } 50 const chimneys = []; 51 for (const sp of map.spawns) { 52 add(buildSpawn(sp), sp.x, sp.y, sp.kind[0].toUpperCase() + sp.kind.slice(1)); 53 if (sp.kind !== 'crossroads') chimneys.push(sp); 54 } 55 for (const l of map.loot) { 56 const t = add(buildLoot(l.item), l.x, l.y); 57 t.hide = () => lootObjs[l.id].collected; 58 lootObjs[l.id] = { t, collected: l.collected }; 59 } 60 61 const markObjs = {}; 62 for (const lm of map.landmarks || []) { 63 markObjs[lm.id] = { t: add(buildLandmark(lm), lm.x, lm.y), lm, used: lm.used }; 64 } 65 const bandObjs = {}; 66 let here = [state.party.x, state.party.y]; 67 let staged = false; 68 let standing = [-1, -1]; 69 const ARENA = 3; 70 const factionOf = (id) => campaign.factions.find((f) => f.id === id); 71 for (const b of map.bands || []) { 72 if (b.kind === 'mimic') continue; 73 const [race, cls] = FACTIONS[factionOf(b.faction)?.name]?.[0] || ['beast', 'wolf']; 74 const mini = createMini({ id: b.id, name: b.name, race, class: cls, faction: b.faction }, { enemy: true }); 75 const t = add(mini, b.x, b.y); 76 if (b.kind === 'guard') mini.position.x += 0.3; 77 t.hide = () => bandObjs[b.id].cleared || hexDistance(t.x, t.y, ...here) > REVEAL; 78 t.band = true; 79 bandObjs[b.id] = { t, mini, cleared: b.cleared, goal: mini.position.clone() }; 80 } 81 82 const beacon = new THREE.Mesh(new THREE.CylinderGeometry(0.18, 0.18, 6, 12, 1, true), mat(T.accent, { emissive: T.accent, emissiveIntensity: 1.2, transparent: true, opacity: 0.4, side: THREE.DoubleSide, depthWrite: false })); 83 beacon.visible = false; 84 group.add(beacon); 85 let objective = null; 86 87 88 function moveBands(cur) { 89 for (const b of cur.map.bands || []) { 90 const o = bandObjs[b.id]; 91 if (!o) continue; 92 o.cleared = b.cleared; 93 Object.assign(o.t, { x: b.x, y: b.y }); 94 o.mini.userData.tile = { x: b.x, y: b.y }; 95 o.goal.copy(worldPos(b.x, b.y)); 96 if (b.kind === 'guard') o.goal.x += 0.3; 97 } 98 } 99 100 function refresh(cur) { 101 for (const p of cur.map.pois) { 102 const o = poiObjs[p.id]; 103 if (o.cleared !== p.cleared) { 104 o.cleared = p.cleared; 105 setCleared(o.t.obj, p.cleared, o.c); 106 } 107 } 108 for (const l of cur.map.loot) lootObjs[l.id].collected = l.collected; 109 moveBands(cur); 110 for (const lm of cur.map.landmarks || []) { 111 const o = markObjs[lm.id]; 112 if (o.used !== lm.used) { 113 o.used = lm.used; 114 setUsed(o.t.obj, lm.used); 115 } 116 o.lm = lm; 117 } 118 const next = cur.campaign.main.steps.find((s) => !s.done); 119 const target = next && cur.map.pois.find((p) => p.id === next.poi); 120 objective = target ? poiObjs[target.id].t : null; 121 beacon.visible = !!objective; 122 if (objective) { 123 const bp = worldPos(target.x, target.y); 124 beacon.position.set(bp.x, bp.y + 3, bp.z); 125 } 126 applyThings(); 127 } 128 129 const life = createLife(group); 130 for (const { t } of Object.values(poiObjs)) life.track(t.obj); 131 132 function smokes() { 133 const out = []; 134 for (const sp of chimneys) if (board.seen(sp.x, sp.y)) out.push({ p: worldPos(sp.x, sp.y).setY(worldPos(sp.x, sp.y).y + 0.9), size: 1 }); 135 for (const { poi, cleared } of Object.values(poiObjs)) if (cleared && board.seen(poi.x, poi.y)) out.push({ p: worldPos(poi.x, poi.y).setY(worldPos(poi.x, poi.y).y + 0.45), size: 0.7 }); 136 life.setSources(out); 137 } 138 139 function applyThings() { 140 smokes(); 141 for (const t of things) { 142 const v = (board.seen(t.x, t.y) || t === objective) && !t.hide() && !(staged && hexDistance(t.x, t.y, ...here) <= ARENA); 143 t.obj.visible = v && (t.band || t.x !== standing[0] || t.y !== standing[1]); 144 if (t.label) t.label.visible = v && labelsOn; 145 } 146 } 147 148 function reveal(px, py) { 149 here = [px, py]; 150 for (const { lm } of Object.values(markObjs)) if (lm.kind === 'lookout' && lm.used) board.reveal(lm.x, lm.y, LOOKOUT); 151 board.reveal(px, py, REVEAL); 152 applyThings(); 153 } 154 155 const trail = (next) => (next.trail || []).map(([x, y]) => worldPos(x, y)); 156 const hexAt = (p) => worldToHex(p.x + board.center.x, p.z + board.center.z); 157 158 const raycaster = new THREE.Raycaster(); 159 function pick(ndc, camera) { 160 raycaster.setFromCamera(ndc, camera); 161 const solids = things.filter((t) => t.obj.visible).map((t) => t.obj); 162 const hit = raycaster.intersectObjects([board.pick, ...solids], true)[0]; 163 if (!hit) return null; 164 for (let o = hit.object; o; o = o.parent) if (o.userData.tile) return o.userData.tile; 165 return board.hexOf(hit.instanceId); 166 } 167 168 let last = 0; 169 function update(t, dark, focus) { 170 life.update(t, dark, focus); 171 const dt = Math.min(0.1, t - last); 172 last = t; 173 board.update(t); 174 for (const o of Object.values(bandObjs)) { 175 const d = o.goal.clone().sub(o.mini.position); 176 const moving = d.length() > 0.02; 177 if (moving) { 178 o.mini.position.add(d.clampLength(0, BAND_SPEED * dt)); 179 o.mini.rotation.y = Math.atan2(d.x, d.z); 180 } 181 if (o.mini.visible) o.mini.userData.animate(moving ? 'walk' : 'idle', dt, moving ? 3 : 0); 182 } 183 beacon.material.opacity = 0.35 + Math.sin(t * 3) * 0.12; 184 for (const l of Object.values(lootObjs)) { 185 const gem = l.t.obj.userData.gem; 186 if (gem) gem.position.y = 0.7 + Math.sin(t * 2.5) * 0.06; 187 } 188 } 189 190 let labelsOn = true; 191 function showLabels(on) { 192 labelsOn = on; 193 beacon.visible = on && !!objective; 194 applyThings(); 195 } 196 197 let sheltered = ''; 198 function shelter(at, eye) { 199 const [px, py] = hexAt(at); 200 const dx = eye.x - at.x; 201 const dz = eye.z - at.z; 202 const len = Math.hypot(dx, dz) || 1; 203 const list = []; 204 for (let y = py - 1; y <= py + 1; y++) { 205 for (let x = px - 1; x <= px + 1; x++) { 206 if (x < 0 || y < 0 || x >= state.map.width || y >= state.map.height || hexDistance(x, y, px, py) > 1) continue; 207 const p = worldPos(x, y); 208 if ((x === px && y === py) || ((p.x - at.x) * dx + (p.z - at.z) * dz) / len > 0.25) list.push(y * state.map.width + x); 209 } 210 } 211 const key = list.join(); 212 if (key === sheltered) return; 213 sheltered = key; 214 standing = [px, py]; 215 board.shelter(list); 216 applyThings(); 217 } 218 219 const stage = (on) => { 220 staged = on; 221 showLabels(!on); 222 board.arena(...(on ? [...here, ARENA] : [])); 223 }; 224 const dispose = () => scene.remove(group); 225 return { group, worldPos, reveal, shelter, showLabels, stage, refresh, moveBands, trail, hexAt, walkable: board.walkable, pick, update, dispose }; 226}