Rotation for cubes on entities

This commit is contained in:
2026-11-13 23:06:37 -03:00
parent c74ddedd6e
commit 9bf6b98953
5 changed files with 58 additions and 15 deletions
+34 -2
View File
@@ -2,8 +2,11 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
// mobs mods define in entities/<name>.json, drawn with a bedrock model from models/<name>.geo.json
import { assert, assertEquals, assertStringIncludes } from "@std/assert";
import { entity_model_from_geo, validate_entity } from "@/common/mod_data.ts";
import { assert, assertAlmostEquals, assertEquals, assertNotEquals, assertStringIncludes } from "@std/assert";
import { entity_model_from_geo, type EntityBoneJson, validate_entity } from "@/common/mod_data.ts";
import type { Texture } from "@/client/renderer/mod.ts";
import { IDENTITY } from "@/client/rendering/model_math.ts";
import { push_model } from "@/client/rendering/model_renderer.ts";
import { Mob } from "@/server/game/entity/mob.ts";
import { create_entity } from "@/server/game/entity/entity_types.ts";
import { container_items, test_game } from "./helpers.ts";
@@ -39,6 +42,35 @@ Deno.test("a bedrock geo.json becomes an entity model with only what the engine
assertEquals(entity_model_from_geo({ format_version: "1.12.0" }, "a:b").model, undefined);
});
Deno.test("a cube's rotation turns it around its pivot like the same rotation on its bone", () => {
const pivot = [1, 2, 3];
const rotation = [30, -45, 60];
const cube = { origin: [0, 0, 0], size: [2, 3, 4], uv: [0, 0] };
const geometry = {
description: { texture_width: 16, texture_height: 16 },
bones: [
{ name: "turned_bone", pivot, rotation, cubes: [cube] },
{ name: "turned_cube", pivot: [0, 0, 0], cubes: [{ ...cube, pivot, rotation }] },
{ name: "still", pivot: [0, 0, 0], cubes: [cube] },
],
};
const { model, problems } = entity_model_from_geo({ "minecraft:geometry": [geometry] }, "a:b");
assertEquals(problems, []);
const points = (name: string) => {
const out: number[] = [];
const atlas = { width: 16, height: 16 } as Texture;
const region = { x: 0, y: 0, width: 16, height: 16 };
const only = (bone: EntityBoneJson) => bone.name === name;
push_model(model!, () => IDENTITY, atlas, region, (x, y, z) => [x, y, z], undefined, only, (x, y, z) => {
out.push(x, y, z);
});
return out;
};
const turned = points("turned_cube");
points("turned_bone").forEach((n, i) => assertAlmostEquals(turned[i], n, 1e-9));
assertNotEquals(turned, points("still"));
});
Deno.test("an entity needs a texture and a size", () => {
assertEquals(validate_entity({ id: "a:b", model: "a:b", texture: "a:b", width: 0.5, height: 1 }), []);
assertEquals(validate_entity({ id: "a:b", model: "a:b", texture: "a:b", width: 0, height: 1, max_health: -1 }), [