142 lines
5.3 KiB
TypeScript
142 lines
5.3 KiB
TypeScript
/**
|
|
* Every vehicle as simple shapes: a hull box tilted by the hull's pitch and roll, a turret box
|
|
* turned to the gun's heading, and a barrel at the gun's pitch. Sizes come from the bot's vehicle
|
|
* class (players: light or medium).
|
|
*/
|
|
import { sceneHeightAt, type SceneTerrain } from '@wto/ground-sim';
|
|
import {
|
|
BoxGeometry, Color, CylinderGeometry, Group, Mesh, MeshLambertMaterial, type Object3D,
|
|
} from 'three';
|
|
import { CSS2DObject } from 'three/addons/renderers/CSS2DRenderer.js';
|
|
import type { UnitAt } from '../frames.ts';
|
|
import { DEG } from './coords.ts';
|
|
|
|
/** Length, width, hull height per vehicle class (medium, light, heavy, tank destroyer, SPAA). */
|
|
const SIZES: readonly (readonly [number, number, number])[] = [
|
|
[7, 3.4, 1.3], [6, 3, 1.1], [7.6, 3.6, 1.4], [7, 3.3, 1.2], [6.5, 3.1, 1.2],
|
|
];
|
|
|
|
export const ARMY_HEX: Readonly<Record<number, number>> = { 1: 0x4aa3ff, 2: 0xff5c5c };
|
|
const DEAD = new Color(0x4a4a4a);
|
|
const SELECTED = new Color(0x6a5200);
|
|
const NONE = new Color(0);
|
|
|
|
/** A trace height further than this from the terrain is kept (bridge, building, platform). */
|
|
const OFF_GROUND_M = 1.5;
|
|
|
|
export interface Tank {
|
|
readonly root: Group;
|
|
readonly turret: Group;
|
|
readonly barrel: Group;
|
|
readonly label: CSS2DObject;
|
|
readonly mats: MeshLambertMaterial[];
|
|
/** Turret top above the hull origin, where lines and the gun camera start. */
|
|
readonly eyeY: number;
|
|
readonly length: number;
|
|
}
|
|
|
|
function sizeOf(u: UnitAt): readonly [number, number, number] {
|
|
const cls = u.bot?.vehicleClass ?? (u.unit.light ? 1 : 0);
|
|
return SIZES[cls] ?? SIZES[0] ?? [7, 3.4, 1.3];
|
|
}
|
|
|
|
function makeTank(u: UnitAt): Tank {
|
|
const [len, wid, hh] = sizeOf(u);
|
|
const hullMat = new MeshLambertMaterial();
|
|
const turretMat = new MeshLambertMaterial();
|
|
const root = new Group();
|
|
root.rotation.order = 'YZX';
|
|
const hull = new Mesh(new BoxGeometry(len, hh, wid), hullMat);
|
|
hull.position.y = hh / 2;
|
|
const turret = new Group();
|
|
turret.position.set(-len * 0.05, hh, 0);
|
|
const tw = wid * 0.7;
|
|
const th = hh * 0.65;
|
|
const turretBox = new Mesh(new BoxGeometry(len * 0.48, th, tw), turretMat);
|
|
turretBox.position.y = th / 2;
|
|
const barrel = new Group();
|
|
barrel.position.set(len * 0.2, th * 0.55, 0);
|
|
const tube = new Mesh(new CylinderGeometry(0.09, 0.11, len * 0.75, 8), turretMat);
|
|
tube.rotation.z = -Math.PI / 2;
|
|
tube.position.x = len * 0.375;
|
|
barrel.add(tube);
|
|
turret.add(turretBox, barrel);
|
|
root.add(hull, turret);
|
|
const div = document.createElement('div');
|
|
div.className = 'tank-label';
|
|
const label = new CSS2DObject(div);
|
|
label.position.y = hh + th + 2.5;
|
|
root.add(label);
|
|
for (const m of [hull, turretBox, tube]) m.userData['eid'] = u.unit.eid;
|
|
return { root, turret, barrel, label, mats: [hullMat, turretMat], eyeY: hh + th, length: len };
|
|
}
|
|
|
|
export class TankPool {
|
|
readonly group = new Group();
|
|
private readonly tanks = new Map<string, Tank>();
|
|
|
|
constructor(private readonly terrain: SceneTerrain | undefined) {}
|
|
|
|
get(eid: string): Tank | undefined {
|
|
return this.tanks.get(eid);
|
|
}
|
|
|
|
/** Meshes to pick with a ray. */
|
|
pickables(): Object3D[] {
|
|
const out: Object3D[] = [];
|
|
for (const t of this.tanks.values()) if (t.root.visible) t.root.traverse((o) => { if (o instanceof Mesh) out.push(o); });
|
|
return out;
|
|
}
|
|
|
|
terrainY(x: number, z: number): number {
|
|
return this.terrain === undefined ? 0 : sceneHeightAt(this.terrain, x, z);
|
|
}
|
|
|
|
/** Ground height for the vehicle: the terrain unless the trace puts it clearly above or below. */
|
|
groundY(x: number, y: number, z: number): number {
|
|
if (this.terrain === undefined) return y;
|
|
const g = sceneHeightAt(this.terrain, x, z);
|
|
return Math.abs(y - g) <= OFF_GROUND_M ? g : y;
|
|
}
|
|
|
|
update(units: ReadonlyMap<string, UnitAt>, selected: string | undefined, labels: (u: UnitAt) => string | undefined): void {
|
|
for (const [eid, t] of this.tanks) if (!units.has(eid)) t.root.visible = false;
|
|
for (const u of units.values()) {
|
|
let t = this.tanks.get(u.unit.eid);
|
|
if (t === undefined) {
|
|
t = makeTank(u);
|
|
this.tanks.set(u.unit.eid, t);
|
|
this.group.add(t.root);
|
|
}
|
|
this.place(t, u, u.unit.eid === selected, labels(u));
|
|
}
|
|
}
|
|
|
|
private place(t: Tank, u: UnitAt, selected: boolean, label: string | undefined): void {
|
|
t.root.visible = true;
|
|
t.root.position.set(u.x, this.groundY(u.x, u.y, u.z), -u.z);
|
|
t.root.rotation.set(u.hullRoll * DEG, u.yaw * DEG, u.hullPitch * DEG);
|
|
const rel = ((((u.gunYaw - u.yaw) % 360) + 540) % 360) - 180;
|
|
t.turret.rotation.y = rel * DEG;
|
|
const hullSlope = u.hullPitch * Math.cos(rel * DEG) + u.hullRoll * Math.sin(rel * DEG);
|
|
t.barrel.rotation.z = (u.gunPitch - hullSlope) * DEG;
|
|
const base = u.unit.alive ? new Color(ARMY_HEX[u.unit.army] ?? 0xcccccc) : DEAD;
|
|
t.mats.forEach((m, i) => {
|
|
m.color.copy(base).multiplyScalar(i === 0 ? 1 : 0.8);
|
|
m.emissive.copy(selected ? SELECTED : NONE);
|
|
});
|
|
t.label.visible = label !== undefined;
|
|
if (label !== undefined) t.label.element.textContent = label;
|
|
if (u.unit.human) t.label.element.classList.add('human');
|
|
}
|
|
|
|
dispose(): void {
|
|
for (const t of this.tanks.values()) {
|
|
t.root.traverse((o) => { if (o instanceof Mesh) o.geometry.dispose(); });
|
|
for (const m of t.mats) m.dispose();
|
|
t.label.element.remove();
|
|
}
|
|
this.tanks.clear();
|
|
}
|
|
}
|