1// What a piece or an ore chunk looks like: the palette roles, how wear and tier are 2// folded into the material colours, and the meshes themselves. 3import * as THREE from 'three'; 4import type { Kind } from '../sim'; 5import { lum } from './ink'; 6import { pieceGeometry, poly } from './shapes'; 7 8const ORE_R = 6.5; // world radius of an ore piece 9 10// A piece is a bright silhouette with a dimmer inset plate, so the cel shading 11// survives the downsample to cell resolution. The roles must never read as each 12// other: your hull is the brightest cream, another player's hull is gold, a 13// server-run AI hull is green, and loose wreckage is dim sepia. A piece flips 14// shade the moment it is bolted on or torn off, and turns red for everyone once 15// it starts taking hits. 16const SHADE = { 17 own: [lum(1, 7), lum(0.5, 7)], 18 other: [lum(0.8, 6), lum(0.4, 6)], 19 ai: [lum(0.75, 15), lum(0.35, 15)], 20 rogue: [lum(0.7, 13), lum(0.35, 13)], 21 loose: [lum(0.55, 5), lum(0.28, 5)], 22 ore: [lum(0.95, 6), lum(0.5, 4)], // ore in a planet's ring 23 towed: [lum(1, 7), lum(0.55, 6)], // ore on someone's line, bright as a hull 24}; 25export type Role = keyof typeof SHADE; 26const HURT = [ 27 [lum(0.85, 13), lum(0.4, 13)], // hit: rust red 28 [lum(1, 14), lum(0.5, 14)], // nearly gone: hot red 29]; 30// Upgrade tiers wear a border: gold at one, cream at two. Nothing at zero. 31const TIER = [lum(0, 0), lum(1, 6), lum(1, 12)]; 32 33export type Part = THREE.Mesh<THREE.BufferGeometry, THREE.MeshBasicMaterial>; 34 35export const shade = (g: THREE.Group, role: Role) => { 36 const c = SHADE[role]; 37 g.children.forEach((m, i) => (m as Part).material.color.copy(c[i])); 38}; 39 40// What a piece looks like is entirely its health, its tier and whether the ship it 41// belongs to is hostile to us. A hostile hull wears a strong red rim, which is a 42// statement about the ship, not damage to it. 43export type Wear = { hp: number; maxhp: number; tier: number }; 44export function paint(g: THREE.Group, role: Role, w: Wear, hostile: boolean): void { 45 const ratio = w.maxhp > 0 ? w.hp / w.maxhp : 1; 46 const [hi, lo] = ratio > 0.66 ? SHADE[role] : ratio > 0.33 ? HURT[0] : HURT[1]; 47 const [rim, edge, a, b] = g.children as Part[]; // built in this order 48 rim.visible = hostile; 49 if (hostile) rim.material.color.copy(HURT[1][0]); // the rim of an enemy 50 a.material.color.copy(hi); 51 b.material.color.copy(lo); 52 edge.visible = w.tier > 0 && !hostile; // the rim already says more than a tier 53 if (edge.visible) edge.material.color.copy(TIER[w.tier]); 54} 55 56// The tier border is a larger copy of the same shape drawn first, so the 57// silhouette covers its middle and only a rim of it shows. A hostile hull gets a 58// second, fatter copy outside that, so an enemy reads as a strong red border even 59// when a cell is several world units wide. 60export function piece(kind: Kind): THREE.Group { 61 const g = new THREE.Group(); 62 g.renderOrder = 1; // over the bodies, which are the only thing behind a ship 63 for (const [scale, v] of [[1.9, 0], [1.3, 0], [1, 1], [0.7, 0.5]]) { 64 g.add(new THREE.Mesh(pieceGeometry(kind, scale), new THREE.MeshBasicMaterial({ color: lum(v) }))); 65 } 66 return g; 67} 68 69// Ore is a small faceted chunk: a hexagon with a darker core, the one thing in 70// space that is neither a ship piece nor a body. 71const ORE_SHELL = new THREE.ShapeGeometry(poly(6, ORE_R)); 72const ORE_CORE = new THREE.ShapeGeometry(poly(6, ORE_R * 0.55, 0, 0.4)); 73export function oreChunk(): THREE.Group { 74 const g = new THREE.Group(); 75 g.renderOrder = 1; // ore never hides behind a planet it is orbiting 76 g.add(new THREE.Mesh(ORE_SHELL, new THREE.MeshBasicMaterial({ color: SHADE.ore[0] }))); 77 g.add(new THREE.Mesh(ORE_CORE, new THREE.MeshBasicMaterial({ color: SHADE.ore[1] }))); 78 shade(g, 'ore'); 79 return g; 80}