Entity movement

This commit is contained in:
2026-11-21 13:23:12 -03:00
parent dd53613758
commit 0f58a9cbfe
4 changed files with 96 additions and 2 deletions
+69 -1
View File
@@ -3,12 +3,19 @@
// mobs mods define in entities/<name>.json, drawn with a bedrock model from models/<name>.geo.json
import { assert, assertAlmostEquals, assertEquals, assertNotEquals, assertStringIncludes } from "@std/assert";
import { entity_category, entity_model_from_geo, type EntityBoneJson, validate_entity } from "@/common/mod_data.ts";
import {
entity_category,
entity_model_from_geo,
type EntityBoneJson,
type EntityJson,
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 { mod_entity_factory } from "@/server/game/entity/mod_entity.ts";
import { container_items, test_game } from "./helpers.ts";
Deno.test("a bedrock geo.json becomes an entity model with only what the engine uses", () => {
@@ -164,3 +171,64 @@ Deno.test("solid blocks can't be placed where a mob or player is", async () => {
send(1, { type: "use_block", x: 3, y: 100, z: 3, face: "top" });
assertEquals(game.world.get_block_id(3, 101, 3), "cubi:air");
});
// a mob with no goals, so it only walks where it's sent, at 0.5 100 0.5 on a stone floor that ends at x 3 with a deep
// pit beyond, and a wall at x 2 when wall is set. alice is there too, so the mob thinks
async function walker(fields: Partial<EntityJson>, wall = false) {
const t = await test_game("mods");
for (let x = -4; x <= 8; x++) {
for (let z = -4; z <= 4; z++) {
for (let y = 90; y < 104; y++) t.game.set_block(x, y, z, y === 99 && x <= 3 ? "bworld:stone" : "cubi:air");
if (wall && x === 2) t.game.set_block(x, 100, z, "bworld:stone");
}
}
t.join(1, "alice");
t.send(1, { type: "move", x: -3.5, y: 100, z: -3.5, yaw: 0, pitch: 0 });
const json: EntityJson = {
id: "test:walker",
texture: "bworld:chicken",
behavior: "test:still",
width: 0.6,
height: 1.8,
...fields,
};
const mob = mod_entity_factory(json, new Map([["test:still", () => {}]]))(t.game.entities) as Mob;
mob.set_position(0.5, 100, 0.5);
t.game.entities.add_entity(mob);
return { game: t.game, mob };
}
// the highest and lowest its feet got over ticks, walking to x
function walk(game: { tick(): void }, mob: Mob, x: number, ticks: number) {
let [lowest, highest] = [mob.y, mob.y];
for (let i = 0; i < ticks; i++) {
mob.move_control.move_to(x, 100, 0.5, 1);
game.tick();
lowest = Math.min(lowest, mob.y);
highest = Math.max(highest, mob.y);
}
return { lowest, highest };
}
Deno.test("a mob that doesn't jump stays down at a wall a mob that jumps gets onto", async () => {
const jumper = await walker({}, true);
assert(walk(jumper.game, jumper.mob, 2.5, 60).highest >= 101);
const stays = await walker({ jumps: false }, true);
assertAlmostEquals(walk(stays.game, stays.mob, 2.5, 60).highest, 100, 1e-6);
});
Deno.test("a mob that doesn't drop stops at an edge, unless something knocks it off", async () => {
const faller = await walker({});
assert(walk(faller.game, faller.mob, 6.5, 100).lowest < 99);
const { game, mob } = await walker({ max_fall: 0 });
assertAlmostEquals(walk(game, mob, 6.5, 100).lowest, 100, 1e-6);
// like a sneaking player, it can lean out until only the edge of its box is over the floor
assert(mob.x - mob.width / 2 < 4, `it got to x ${mob.x}`);
// hit from the side away from the pit
mob.knockback(0.4, -1, 0);
for (let i = 0; i < 40; i++) game.tick();
assert(mob.y < 99, `it's still at y ${mob.y}`);
});