| 1 | "use strict"; |
| 2 | const { describe, it } = require("node:test"); |
| 3 | const assert = require("node:assert/strict"); |
| 4 | const PpdaViewFrame = require("../src/ppda/viewFrame.js"); |
| 5 | |
| 6 | describe("PpdaViewFrame", () => |
| 7 | { |
| 8 | it("picks nearest yaw", () => |
| 9 | { |
| 10 | const yaws = [0, 30, 60, 90]; |
| 11 | assert.equal(PpdaViewFrame.pickYaw(10, yaws), 0); |
| 12 | assert.equal(PpdaViewFrame.pickYaw(20, yaws), 30); |
| 13 | assert.equal(PpdaViewFrame.pickYaw(350, yaws), 0); |
| 14 | }); |
| 15 | |
| 16 | it("frameIndex wraps", () => |
| 17 | { |
| 18 | assert.equal(PpdaViewFrame.frameIndex(0, 4, 4), 0); |
| 19 | assert.equal(PpdaViewFrame.frameIndex(1, 4, 4), 0); |
| 20 | assert.equal(PpdaViewFrame.frameIndex(0.6, 4, 4), 2); |
| 21 | }); |
| 22 | |
| 23 | it("uvRect from view", () => |
| 24 | { |
| 25 | const uv = PpdaViewFrame.uvRect({ x: 10, y: 20, w: 5, h: 8 }, 100, 200); |
| 26 | assert.equal(uv.u0, 0.1); |
| 27 | assert.equal(uv.v0, 0.1); |
| 28 | assert.equal(uv.u1, 0.15); |
| 29 | assert.equal(uv.v1, 0.14); |
| 30 | }); |
| 31 | |
| 32 | it("relativeYawDeg front when looking at facing NPC", () => |
| 33 | { |
| 34 | // NPC at origin facing +X (0). Player at +X → sees front → rel ~0 |
| 35 | const rel = PpdaViewFrame.relativeYawDeg(10, 0, 0, 0, 0); |
| 36 | assert.ok(rel < 1 || rel > 359); |
| 37 | }); |
| 38 | |
| 39 | it("walk-around: front → left (90) → back (180) → right (270)", () => |
| 40 | { |
| 41 | // NPC at origin facing +X. Yaw 0=front, 90=model left. |
| 42 | const yaws = [0, 90, 180, 270]; |
| 43 | const facing = 0; |
| 44 | // Front: player east |
| 45 | assert.equal(PpdaViewFrame.pickYaw( |
| 46 | PpdaViewFrame.relativeYawDeg(10, 0, 0, 0, facing), yaws), 0); |
| 47 | // NPC's left = north (−Y) |
| 48 | assert.equal(PpdaViewFrame.pickYaw( |
| 49 | PpdaViewFrame.relativeYawDeg(0, -10, 0, 0, facing), yaws), 90); |
| 50 | // Back: west |
| 51 | assert.equal(PpdaViewFrame.pickYaw( |
| 52 | PpdaViewFrame.relativeYawDeg(-10, 0, 0, 0, facing), yaws), 180); |
| 53 | // Right = south (+Y) |
| 54 | assert.equal(PpdaViewFrame.pickYaw( |
| 55 | PpdaViewFrame.relativeYawDeg(0, 10, 0, 0, facing), yaws), 270); |
| 56 | }); |
| 57 | |
| 58 | it("pickPitchFromHeight ignores look; same height → 0", () => |
| 59 | { |
| 60 | const pitches = [-45, 0, 45]; |
| 61 | assert.equal(PpdaViewFrame.pickPitchFromHeight(16, 16, 50, pitches), 0); |
| 62 | // NPC much higher than eye → view from below → negative paperdoll pitch. |
| 63 | assert.equal(PpdaViewFrame.pickPitchFromHeight(0, 100, 50, pitches), -45); |
| 64 | // NPC much lower → from above → +45 |
| 65 | assert.equal(PpdaViewFrame.pickPitchFromHeight(100, 0, 50, pitches), 45); |
| 66 | }); |
| 67 | }); |