Files
bworld/tests/monster_test.ts
T
2026-10-13 11:57:43 -03:00

58 lines
2.3 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// "monster" entities go after players and whoever hurts them, like undergarden's rotwalker, see mods/undergarden
import { assert, assertEquals } from "@std/assert";
import { create_entity } from "$/server/game/entity/entity_types.ts";
import { Mob } from "$/server/game/entity/mob.ts";
import { stone_floor, test_game } from "./helpers.ts";
// a stone floor with alice on it, and a rotwalker 3 blocks away
async function setup() {
const t = await test_game("mods");
t.join(1, "alice");
stone_floor(t.game, 8);
t.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0 });
const rotwalker = create_entity(t.game.entities, "undergarden:rotwalker") as Mob;
rotwalker.set_position(0.5, 100, 3.5);
t.game.entities.add_entity(rotwalker);
t.take(1);
const ticks = (n: number) => {
for (let i = 0; i < n; i++) t.game.tick();
};
return { ...t, rotwalker, alice: t.game.players()[0], ticks };
}
Deno.test("a rotwalker has the original's stats, and despawns when nobody is near", async () => {
const { rotwalker } = await setup();
assert(rotwalker instanceof Mob);
assertEquals([rotwalker.width, rotwalker.height, rotwalker.max_health], [0.8, 2.5, 40]);
assertEquals(rotwalker.remove_when_far_away(1000), true);
});
Deno.test("a rotwalker walks up to a player it sees and hits them for its attack damage, swinging", async () => {
const { rotwalker, alice, ticks, take } = await setup();
let swung = false;
for (let i = 0; i < 200 && alice.health === 20; i++) {
ticks(1);
swung ||= take(1).some((m) => m.type === "entity_event" && m.id === rotwalker.id && m.event === "swing");
}
assertEquals(rotwalker.target, alice);
assertEquals(alice.health, 20 - 5);
assert(swung, "players nearby see it swing");
});
Deno.test("a monster goes after whoever hurt it, before the nearest player", async () => {
const { rotwalker, alice, ticks, join, send, game } = await setup();
join(2, "bob");
send(2, { type: "move", x: 0.5, y: 100, z: 5.5, yaw: 0, pitch: 0 });
const bob = game.players().find((p) => p.name === "bob")!;
rotwalker.target = alice;
ticks(5);
send(2, { type: "attack_entity", id: rotwalker.id });
// a bit less than bob's 1, its armor (physical shielding) takes some
assert(rotwalker.health < 40);
ticks(1);
assertEquals(rotwalker.target, bob);
});