// The vehicles the rules move: F-line streetcars, California St cable cars and cars. Each has a // mesh; every frame they take their pose from the game state. import * as THREE from "three"; import { groundAt } from "../city.js"; import { Builder, vertexColored } from "./kit.js"; import { sedanGeometry } from "./props.js"; function streetcar(livery) { const b = new Builder(); const L = 7.2; const W = 1.3; b.box(-W, 0.45, -L, W, 1.55, L, livery.body); b.box(-W - 0.02, 1.55, -L + 0.4, W + 0.02, 2.45, L - 0.4, 0x2a2f36, livery.trim); for (let z = -L + 1.3; z < L - 1; z += 1.6) b.box(-W - 0.04, 1.55, z - 0.07, W + 0.04, 2.45, z + 0.07, livery.trim); b.box(-W, 2.45, -L, W, 2.7, L, livery.trim); b.box(-W + 0.25, 2.7, -L + 0.5, W - 0.25, 3.05, L - 0.5, 0xc9c6bf); for (const z of [-L + 0.02, L - 0.02]) b.box(-W + 0.2, 1.6, z - 0.02, W - 0.2, 2.35, z + 0.02, 0x3a4048); b.box(-1.0, 0, -L + 1, 1.0, 0.5, -L + 3.2, 0x26282b); b.box(-1.0, 0, L - 3.2, 1.0, 0.5, L - 1, 0x26282b); b.box(-0.05, 3.05, 2, 0.05, 3.15, 6.5, 0x2b2b2b); b.box(-0.04, 3.05, 6.4, 0.04, 5.4, 6.5, 0x2b2b2b); return b.geometry(); } function cableCar() { const b = new Builder(); const red = 0x8e2b2b; const cream = 0xefe3c4; const wood = 0x6b4b34; const L = 4.5; b.box(-1.25, 0.5, -L, 1.25, 1.2, L, red); b.box(-1.2, 1.2, -L + 1.6, 1.2, 2.3, L - 1.6, cream); for (let z = -L + 2; z < L - 1.6; z += 1.0) b.box(-1.22, 1.45, z, 1.22, 2.1, z + 0.6, 0x3a3530); for (const s of [-1, 1]) for (const z of [-L + 0.2, -L + 1.4, L - 1.4, L - 0.2]) b.box(s * 1.15 - 0.05, 1.2, z - 0.05, s * 1.15 + 0.05, 2.6, z + 0.05, wood); b.box(-1.35, 2.6, -L - 0.1, 1.35, 2.85, L + 0.1, wood); b.box(-1.0, 2.85, -L + 0.5, 1.0, 3.2, L - 0.5, cream); b.box(-1.05, 0, -L + 0.8, 1.05, 0.55, -L + 2.4, 0x26282b); b.box(-1.05, 0, L - 2.4, 1.05, 0.55, L - 0.8, 0x26282b); b.box(-1.0, 1.2, -L, 1.0, 1.35, -L + 1.5, wood); b.box(-1.0, 1.2, L - 1.5, 1.0, 1.35, L, wood); return b.geometry(); } export function buildVehicles(scene, traffic) { const group = new THREE.Group(); const mat = vertexColored({ roughness: 0.5 }); const meshes = traffic.list.map((v) => { let mesh; if (v.kind === "car") mesh = new THREE.Mesh(sedanGeometry(), new THREE.MeshStandardMaterial({ vertexColors: true, color: v.color, roughness: 0.3, metalness: 0.25 })); else if (v.kind === "streetcar") mesh = new THREE.Mesh(streetcar(v.livery), mat); else mesh = new THREE.Mesh(cableCar(), mat); mesh.castShadow = true; mesh.receiveShadow = true; mesh.rotation.order = "YXZ"; group.add(mesh); return mesh; }); scene.add(group); return { dispose() { scene.remove(group); }, update(poses) { poses.forEach((p, k) => { const m = meshes[k]; const fx = Math.sin(p.heading); const fz = -Math.cos(p.heading); const half = p.v.len * 0.4; const front = groundAt(p.x + fx * half, p.z + fz * half); const back = groundAt(p.x - fx * half, p.z - fz * half); m.position.set(p.x, (front + back) / 2, p.z); m.rotation.set(Math.atan2(front - back, half * 2), -p.heading, 0); }); }, }; }