Files
bworld/tests/gui_player_test.ts
T
2026-11-04 21:51:26 -03:00

68 lines
3.8 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// the player in the inventory turns to look at the mouse
import { assert, assertEquals } from "@std/assert";
import { gui_player_faces, gui_player_pose } from "@/client/rendering/gui_player.ts";
import { AssetManager } from "@/client/assets.ts";
import type { Texture } from "@/client/renderer/mod.ts";
import { ItemStack } from "@/common/inventory.ts";
import { test_game } from "./helpers.ts";
import { part_point, PARTS, pose_to_world } from "@/client/rendering/player_model.ts";
// where a point of the head or body is, in blocks, the viewer at +z with +x on their right and +y up
function where(part_name: "head" | "body", dx: number, dy: number, local: [number, number, number]) {
const pose = gui_player_pose(false, dx, dy);
const part = PARTS.find((p) => p.name === part_name)!;
return pose_to_world(pose)(...part_point(pose, part, ...local));
}
// the middle of the front of the head (its face) and of the body, and their middles. the front is the low z side
const head_front = (dx: number, dy: number) => where("head", dx, dy, [0, 4, -4]);
const head_middle = (dx: number, dy: number) => where("head", dx, dy, [0, 4, 0]);
const body_front = (dx: number, dy: number) => where("body", dx, dy, [0, -6, -2]);
const body_middle = (dx: number, dy: number) => where("body", dx, dy, [0, -6, 0]);
Deno.test("with the mouse on the eyes, the player faces the viewer", () => {
const front = head_front(0, 0);
const middle = head_middle(0, 0);
assert(front[2] > middle[2] + 0.2, `${front} isn't toward the viewer from ${middle}`);
assert(Math.abs(front[0] - middle[0]) < 1e-9 && Math.abs(front[1] - middle[1]) < 1e-9);
});
Deno.test("the head and body turn toward the mouse, the head more", () => {
for (const dx of [40, -40]) {
const head = head_front(dx, 0)[0] - head_middle(dx, 0)[0];
const body = body_front(dx, 0)[0] - body_middle(dx, 0)[0];
assert(Math.sign(head) === Math.sign(dx), `head turns ${head} for a mouse at ${dx}`);
assert(Math.sign(body) === Math.sign(dx), `body turns ${body} for a mouse at ${dx}`);
// both are 4 pixels from the middle, the body's 2: compare their angles
assert(Math.abs(head) / 0.25 > Math.abs(body) / 0.125, `the head turns less than the body for ${dx}`);
}
});
Deno.test("the head tilts toward the mouse, down when it's below", () => {
assert(head_front(0, 40)[1] < head_middle(0, 40)[1], "doesn't look down");
assert(head_front(0, -40)[1] > head_middle(0, -40)[1], "doesn't look up");
});
Deno.test("what the player wears is drawn on them too, with the atlas, in among their faces back to front", async () => {
await test_game("mods");
const skin = { width: 64, height: 64, tex: "skin" } as unknown as Texture;
const atlas = { width: 256, height: 256, tex: "atlas" } as unknown as Texture;
AssetManager.instance.assets["bworld:textures"] = atlas as never;
// where each texture is in the atlas, none known: the renderer falls back to a spot of its own
AssetManager.instance.assets["bworld:textures_info"] = {} as never;
const plain = gui_player_faces(skin, false, 0, 0);
const gloved = gui_player_faces(skin, false, 0, 0, [new ItemStack("artifacts:power_glove", 1)]);
assert(plain.every((face) => face.texture === skin));
const glove = gloved.filter((face) => face.texture === atlas);
assert(glove.length > 0, "no glove");
assertEquals(gloved.length - glove.length, plain.length);
// sorted together with the body's faces, back to front
const depth = (face: { vertices: number[][] }) => face.vertices.reduce((sum, v) => sum + v[2], 0);
const depths = gloved.map(depth);
assertEquals(depths, [...depths].sort((a, b) => a - b));
// and in among them, not all over or under the body
assert(gloved.indexOf(glove[0]) > 0 && gloved.indexOf(glove.at(-1)!) < gloved.length - 1);
});