Skip to content
Make the player visible with a procedural 3D humanoid132be7c
JavaScript146 lines7.1 KB
import { test } from 'node:test';import assert from 'node:assert/strict';import {  identity, multiply, translation, scaling, rotationY, rotationZ, transformPoint, perspective, lookAt,  computeCamera, POSES, POSE_KEYS, blendPoses, targetPose, smoothPose, buildSkeleton, hexToRgb, PALETTES, UNIT} from './pose.mjs';import { createMatch, step, simulate, CONFIG } from './game/engine.mjs';import { cubeMesh, defaultStage, normalizeStage } from './render3d.mjs'; const near = (a, b, eps = 1e-6) => assert.ok(Math.abs(a - b) < eps, `${a} !~ ${b}`); test('mat4: identity, translation and scaling compose', () => {  const m = multiply(translation(1, 2, 3), scaling(2, 2, 2));  assert.deepEqual(transformPoint(m, [1, 1, 1]), [3, 4, 5]);  assert.deepEqual(transformPoint(identity(), [4, 5, 6]), [4, 5, 6]);}); test('mat4: rotations are right-handed', () => {  const p = transformPoint(rotationZ(Math.PI / 2), [1, 0, 0]);  near(p[0], 0); near(p[1], 1);  const q = transformPoint(rotationY(Math.PI), [1, 0, 0]);  near(q[0], -1); near(q[2], 0);}); test('lookAt moves the eye to the origin and looks down -z', () => {  const v = lookAt([0, 0, 5], [0, 0, 0]);  const p = transformPoint(v, [0, 0, 0]);  near(p[2], -5); near(p[0], 0); near(p[1], 0);}); test('perspective projects a nearer point larger', () => {  const pr = perspective(Math.PI / 4, 16 / 9, 0.1, 50);  const a = transformPoint(pr, [1, 0, -2]), b = transformPoint(pr, [1, 0, -8]);  assert.ok(a[0] > b[0]);  assert.ok(a[2] < b[2]);}); test('computeCamera frames both fighters and widens with distance', () => {  const s = createMatch(0);  const cam = computeCamera(s, 16 / 9);  const mid = (s.fighters[0].x + s.fighters[1].x) / 2 * UNIT;  near(cam.target[0], mid); near(cam.eye[0], mid);  assert.ok(cam.eye[2] > 0);  assert.equal(cam.view.length, 16); assert.equal(cam.projection.length, 16);  const far = { ...s, fighters: [{ ...s.fighters[0], x: 50 }, { ...s.fighters[1], x: 900 }] };  assert.ok(computeCamera(far, 16 / 9).eye[2] > cam.eye[2]);}); test('POSES all define every joint; blend is linear and clamped', () => {  for (const [name, p] of Object.entries(POSES)) for (const k of POSE_KEYS) assert.equal(typeof p[k], 'number', `${name}.${k}`);  const mid = blendPoses(POSES.idle, POSES.ko, 0.5);  near(mid.lean, (POSES.idle.lean + POSES.ko.lean) / 2);  assert.deepEqual(blendPoses(POSES.idle, POSES.ko, 2), blendPoses(POSES.idle, POSES.ko, 1));}); test('targetPose maps engine stances to distinct poses', () => {  const base = createMatch(0).fighters[0];  const stances = ['idle', 'walk', 'punch', 'kick', 'hit', 'ko', 'jump', 'block'];  const seen = new Map();  for (const st of stances) {    const p = targetPose({ ...base, stance: st, vx: st === 'walk' ? 260 : 0, attack: st === 'punch' || st === 'kick' ? { type: st, t: 0.12, hasHit: false } : null }, 0.4, CONFIG.attacks);    seen.set(st, JSON.stringify(p));  }  assert.equal(new Set(seen.values()).size, stances.length);  assert.ok(targetPose({ ...base, stance: 'ko' }).lean < -1, 'KO lies down');}); test('targetPose follows the engine attack timeline (startup → hit → recovery)', () => {  const f = createMatch(0).fighters[0];  const spec = CONFIG.attacks.punch;  const at = (t) => targetPose({ ...f, stance: 'punch', attack: { type: 'punch', t, hasHit: false } }, 0, CONFIG.attacks);  near(at(0).lElbow, POSES.idle.lElbow);  assert.deepEqual(at(spec.startup + spec.active / 2), POSES.punchHit);  const end = spec.startup + spec.active + spec.recovery;  near(at(end).lElbow, POSES.idle.lElbow);}); test('walk cycle animates over time', () => {  const f = { ...createMatch(0).fighters[0], stance: 'walk', vx: 260 };  assert.notDeepEqual(targetPose(f, 0), targetPose(f, 0.2));}); test('smoothPose converges towards the target without overshoot', () => {  let p = smoothPose(null, POSES.idle, 0);  assert.deepEqual(p, { ...POSES.idle });  for (let i = 0; i < 120; i++) p = smoothPose(p, POSES.ko, 1 / 60);  near(p.lean, POSES.ko.lean, 1e-3);  const one = smoothPose(POSES.idle, POSES.ko, 1 / 60);  assert.ok(one.lean < POSES.idle.lean && one.lean > POSES.ko.lean);}); test('buildSkeleton places parts relative to the fighter and respects facing', () => {  const f = { ...createMatch(0).fighters[0], x: 300, y: 0, facing: 1 };  const parts = buildSkeleton(f, POSES.idle, PALETTES[0]);  const names = parts.map((p) => p.name);  for (const n of ['head', 'torso', 'lThigh', 'rShin', 'lForeArm', 'rHand']) assert.ok(names.includes(n), n);  const head = parts.find((p) => p.name === 'head');  const hp = transformPoint(head.matrix, [0, 0, 0]);  near(hp[0], 3, 0.15);  assert.ok(hp[1] > 1.3 && hp[1] < CONFIG.fighterHeight * UNIT, 'head below fighterHeight');  const foot = parts.find((p) => p.name === 'lFoot');  assert.ok(transformPoint(foot.matrix, [0, 0, 0])[1] < 0.1, 'feet on the floor');  const punching = buildSkeleton(f, POSES.punchHit, PALETTES[0]);  const hand = transformPoint(punching.find((p) => p.name === 'lHand').matrix, [0, 0, 0]);  assert.ok(hand[0] > 3.3, 'punch extends toward +x when facing right');  const back = buildSkeleton({ ...f, facing: -1 }, POSES.punchHit, PALETTES[0]);  assert.ok(transformPoint(back.find((p) => p.name === 'lHand').matrix, [0, 0, 0])[0] < 2.7, 'and toward -x when facing left');  const jumping = buildSkeleton({ ...f, y: 100 }, POSES.jump, PALETTES[0]);  assert.ok(transformPoint(jumping.find((p) => p.name === 'head').matrix, [0, 0, 0])[1] > hp[1] + 0.9);}); test('hexToRgb parses colours and falls back', () => {  assert.deepEqual(hexToRgb('#ff0000'), [1, 0, 0]);  assert.deepEqual(hexToRgb('nope', [0.5, 0.5, 0.5]), [0.5, 0.5, 0.5]);}); test('cubeMesh is a closed 6-face box with unit normals', () => {  const m = cubeMesh();  assert.equal(m.vertices.length, 24 * 6); assert.equal(m.indices.length, 36);  for (let i = 0; i < 24; i++) near(Math.hypot(m.vertices[i * 6 + 3], m.vertices[i * 6 + 4], m.vertices[i * 6 + 5]), 1);}); test('defaultStage / normalizeStage produce floor, wall and lights from level data', () => {  const s = createMatch(1);  const d = defaultStage(s.level);  assert.ok(d.boxes.some((b) => b.name === 'floor') && d.boxes.some((b) => b.name === 'backwall'));  assert.equal(d.fog, s.level.theme.fog);  const n = normalizeStage({ boxes: [{ position: [0, 0, 0], size: [1, 1, 1], color: '#00ff00' }], lights: [{ type: 'directional', direction: [0, 1, 0], color: [1, 1, 1] }] }, s.level);  assert.deepEqual(n.boxes[0].color, [0, 1, 0]); assert.deepEqual(n.lightDir, [0, 1, 0]);  assert.equal(normalizeStage(undefined, s.level).boxes.length, d.boxes.length);}); test('a simulated match yields valid poses every tick (engine untouched)', () => {  let s = createMatch(0);  const poses = [null, null];  for (let i = 0; i < 240; i++) {    s = step(s, [{ right: true, punch: i % 30 === 0 }, { left: true, kick: i % 45 === 0 }]);    s.fighters.forEach((f, j) => {      poses[j] = smoothPose(poses[j], targetPose(f, i / 60, CONFIG.attacks), 1 / 60);      for (const k of POSE_KEYS) assert.ok(Number.isFinite(poses[j][k]));      assert.equal(buildSkeleton(f, poses[j], PALETTES[j]).length, 17);    });  }  assert.ok(simulate(createMatch(0), [{}, {}], 10).tick === 10);});