74 lines
3.8 KiB
TypeScript
74 lines
3.8 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// items drawn with a model, placed in each place they're shown by the model's display transforms
|
|
import { assert, assertAlmostEquals, assertEquals } from "@std/assert";
|
|
import { mat4 } from "gl-matrix";
|
|
import { display_transform, drawn_as_model, item_model_of } from "$/client/rendering/item_renderer.ts";
|
|
import type { DisplayTransform } from "$/common/block_models.ts";
|
|
import { ItemStack } from "$/common/inventory.ts";
|
|
import { load_and_check } from "$/tools/check_mods.ts";
|
|
import { create_mod } from "$/tools/new_mod.ts";
|
|
import { copy_dir, test_game } from "./helpers.ts";
|
|
|
|
const HANDLE = {
|
|
from: [7, 0, 7],
|
|
to: [9, 14, 9],
|
|
faces: { north: { texture: "#0" }, south: { texture: "#0" }, top: { texture: "#0" } },
|
|
};
|
|
const GUI: DisplayTransform = { rotation: [30, 45, 0], scale: [0.625, 0.625, 0.625] };
|
|
|
|
// mods/bworld and a mod made from the template, with a wrench item drawn with this model
|
|
function mods_with(model: Record<string, unknown>) {
|
|
const dir = Deno.makeTempDirSync({ prefix: "bworld_item_display_" });
|
|
copy_dir("mods/bworld", `${dir}/bworld`);
|
|
create_mod("copper_tools", "Copper Tools", dir);
|
|
const write = (path: string, json: Record<string, unknown>) => {
|
|
Deno.mkdirSync(`${dir}/copper_tools/${path.slice(0, path.lastIndexOf("/"))}`, { recursive: true });
|
|
Deno.writeTextFileSync(`${dir}/copper_tools/${path}.json`, JSON.stringify({ format_version: 1, ...json }));
|
|
};
|
|
write("models/wrench", { model: { id: "copper_tools:wrench", ...model } });
|
|
write("items/wrench", {
|
|
item: { id: "copper_tools:wrench", texture: "bworld:stick", model: "copper_tools:wrench" },
|
|
});
|
|
return dir;
|
|
}
|
|
|
|
Deno.test("an item with a model is drawn with it, in the model's own textures, placed by its display transforms", async () => {
|
|
const dir = mods_with({ textures: { "0": "bworld:oak_planks" }, elements: [HANDLE], display: { gui: GUI } });
|
|
await test_game(dir);
|
|
const wrench = new ItemStack("copper_tools:wrench", 1);
|
|
const model = item_model_of(wrench)!;
|
|
assertEquals(model.quads.length, 3);
|
|
assert(model.quads.every((quad) => quad.texture === "bworld:oak_planks"));
|
|
assertEquals(model.display, { gui: GUI });
|
|
// held and dropped like a block where it has no transform of its own
|
|
assert(drawn_as_model(wrench));
|
|
assertEquals(item_model_of(new ItemStack("bworld:stick", 1)), undefined);
|
|
assert(!drawn_as_model(new ItemStack("bworld:stick", 1)));
|
|
Deno.removeSync(dir, { recursive: true });
|
|
});
|
|
|
|
Deno.test("check-mods reports an item's model that doesn't exist", () => {
|
|
const dir = mods_with({ textures: { "0": "bworld:oak_planks" }, elements: [HANDLE] });
|
|
Deno.removeSync(`${dir}/copper_tools/models/wrench.json`);
|
|
const errors = load_and_check(dir).find((mod) => mod.id === "copper_tools")!.report.errors.join("\n");
|
|
assert(errors.includes("model copper_tools:wrench doesn't exist"), errors);
|
|
Deno.removeSync(dir, { recursive: true });
|
|
});
|
|
|
|
Deno.test("a display transform moves, turns around z then y then x, and scales, around the model's middle", () => {
|
|
// an item's points are centered on x and z, from 0 to 1 high
|
|
const near = (actual: number[], expected: number[]) =>
|
|
actual.forEach((v, i) => assertAlmostEquals(v, expected[i], 1e-9));
|
|
const base = mat4.fromTranslation(mat4.create(), [10, 20, 30]);
|
|
// no transform: the model's middle where base puts things
|
|
near(display_transform(base, {})(0, 0.5, 0), [10, 20, 30]);
|
|
// translation is in pixels
|
|
near(display_transform(base, { translation: [16, 8, 0] })(0, 0.5, 0), [11, 20.5, 30]);
|
|
// the middle of its east side ends up on top: north (-z) after y, then up after x
|
|
near(display_transform(base, { rotation: [90, 90, 0] })(0.5, 0.5, 0), [10, 20.5, 30]);
|
|
near(display_transform(base, { rotation: [0, 90, 0] })(0.5, 0.5, 0), [10, 20, 29.5]);
|
|
near(display_transform(base, { scale: [0.5, 0.5, 0.5] })(0.5, 1, 0), [10.25, 20.25, 30]);
|
|
});
|