Animators and coilhead animations

This commit is contained in:
2026-11-21 21:54:02 -03:00
parent 69ad4b80d0
commit 0c711b8163
21 changed files with 960 additions and 34 deletions
+145
View File
@@ -8,6 +8,11 @@ import { sample_channel, sample_clip } from "@/client/rendering/animation.ts";
import { apply, bone_matrices, type BoneOffsets, IDENTITY, type Vec3 } from "@/client/rendering/model_math.ts";
import { create_mod } from "@/tools/new_mod.ts";
import { load_and_check } from "@/tools/check_mods.ts";
import { type AnimatedEntity, AnimationState, type Clip, type EntityAnimator } from "@/common/mod_api/client.ts";
import { animation_for, type AnimatorSource, entity_animators } from "@/client/rendering/entity_animators.ts";
import { EverythingRegistry } from "@/common/everything_registry.ts";
import type { EntityData } from "@/common/protocol.ts";
import { copy_dir, errors_during, test_game, test_mob } from "./helpers.ts";
// the one clip a file with one animation reads into
function one_clip(animation: unknown): ClipJson {
@@ -195,3 +200,143 @@ Deno.test("a clip moves bones in the model's pixels, and scales them around thei
const pivot = [-2, 3, 4];
assertEquals(apply(scaled.get("arm")!, ...point), point.map((n, i) => pivot[i] + (n - pivot[i]) * [2, 3, 0.5][i]));
});
Deno.test("an animation state counts seconds from when it started, and knows when a clip has played through", () => {
const once: Clip = { id: "a:b/once", length: 1, loop: false };
const state = new AnimationState();
assertEquals([state.started, state.seconds(100), state.finished(once, 100)], [false, 0, false]);
state.animate_when(true, 10);
state.animate_when(true, 15);
assertEquals(state.seconds(20), 0.5);
assert(!state.finished(once, 29));
assert(state.finished(once, 30));
assert(!state.finished({ ...once, loop: true }, 300));
state.animate_when(false, 40);
assertEquals(state.started, false);
state.start_if_stopped(50);
state.start_if_stopped(60);
assertEquals(state.seconds(70), 1);
});
// an entity the way an animator sees it, standing still and looking ahead
function animated(type: string, data: EntityData = {}): AnimatorSource & { data: EntityData } {
return {
id: "e1",
type,
data,
age: 0,
yaw: 0,
head_yaw: 0,
pitch: 0,
walk_animation: () => ({ position: 0, speed: 0 }),
attack_animation: () => 0,
};
}
// registers an animator for a new entity type, and a clip turning the arm up to 90 degrees over a second
function animator_type(name: string, create: (entity: AnimatedEntity) => EntityAnimator) {
const type = `test:${name}`;
entity_animators.set(type, { mod: "test", create });
const clip_id = `test:${name}/raise`;
if (!EverythingRegistry.get("animations", clip_id)) {
EverythingRegistry.register<ClipJson>("animations", clip_id, {
id: clip_id,
length: 1,
loop: false,
bones: { arm: { rotation: [key(0, [0, 0, 0]), key(1, [90, 0, 0])] } },
});
}
return { type, raise: { id: clip_id, length: 1, loop: false } };
}
Deno.test("an animator hears about each client data field as it changes, and the ones there from the start", () => {
const heard: string[] = [];
const { type } = animator_type("listener", () => ({ pose() {}, on_data: (field) => heard.push(field) }));
const entity = animated(type, { client_data: { mood: 1 }, glowing: true });
const animation = animation_for(entity)!;
assertEquals(heard, ["mood"]);
const before = entity.data;
entity.data = { client_data: { mood: 1, state: 2 } };
animation.data_changed(before);
assertEquals(heard, ["mood", "state"]);
const again = entity.data;
entity.data = { client_data: { state: 3 } };
animation.data_changed(again);
assertEquals(heard, ["mood", "state", "mood", "state"]);
assertEquals(animation_for(animated("test:no_animator")), undefined);
});
Deno.test("an animator's pose plays clips and moves bones", () => {
const { type, raise } = animator_type("poser", () => ({
pose(pose, time) {
pose.play(raise, time / 20, 0.5);
pose.move("arm", 1, 2, 3);
pose.scale("leg", 2, 2, 2);
pose.look("head");
},
}));
const entity = { ...animated(type), head_yaw: Math.PI / 2, pitch: -Math.PI / 4 };
const offsets = animation_for(entity)!.pose(10)!;
assertEquals(offsets.get("arm"), { rotation: [22.5, 0, 0], position: [1, 2, 3], scale: [1, 1, 1] });
assertEquals(offsets.get("leg")!.scale, [2, 2, 2]);
// mirrored like a model's own rotations
const head = offsets.get("head")!.rotation;
assertAlmostEquals(head[0], 45);
assertAlmostEquals(head[1], -90);
});
Deno.test("a clip plays from when its state started, and with walking as strongly as it walks", () => {
const state = new AnimationState();
state.start(10);
const { type, raise } = animator_type("stateful", (entity) => ({
pose(pose, time) {
pose.play_state(raise, state, time);
pose.play_walk(raise, entity, time);
},
}));
// half a second of clip walked, at half speed
const animation = animation_for({ ...animated(type), walk_animation: () => ({ position: 10, speed: 0.5 }) })!;
// a quarter of a second into the state is 22.5 degrees, and walking half of 45
assertAlmostEquals(animation.pose(15)!.get("arm")!.rotation[0], 22.5 + 22.5);
state.stop();
assertAlmostEquals(animation.pose(15)!.get("arm")!.rotation[0], 22.5);
});
Deno.test("an animator that throws is removed, and the entity is posed without it", async () => {
const { type } = animator_type("thrower", () => ({
pose(pose) {
pose.play({ id: "test:nowhere/nothing", length: 1, loop: false }, 0);
},
}));
const animation = animation_for(animated(type))!;
const errors = await errors_during(() => assertEquals(animation.pose(0), undefined));
assertEquals(errors.length, 1);
assertStringIncludes(errors[0], "[test] the animator of test:thrower e1 threw in pose");
assertStringIncludes(errors[0], "test:nowhere/nothing isn't a clip");
assertEquals(animation.pose(1), undefined);
});
Deno.test("a mob's client data goes to the players who see it, and is saved with it", async () => {
const dir = Deno.makeTempDirSync({ prefix: "bworld_client_data_" });
copy_dir("mods/dangerous_business", `${dir}/dangerous_business`);
const t = await test_game(dir);
t.join(1, "alice");
const mob = test_mob(t.game, {}, 0.5, 100, 0.5);
const ref = t.game.mods.mob(mob);
t.game.tick();
t.take(1);
ref.set_client_data("state", { mood: 3 });
assertEquals(ref.get_client_data("state"), { mood: 3 });
t.game.tick();
const sent = t.take(1).find((m) => m.type === "entity_data" && m.id === mob.id);
assertEquals(sent && "data" in sent ? sent.data.client_data : undefined, { state: { mood: 3 } });
const copy = test_mob(t.game, {}, 2.5, 100, 0.5);
copy.load(mob.save());
assertEquals(t.game.mods.mob(copy).get_client_data("state"), { mood: 3 });
ref.set_client_data("state", undefined);
assertEquals(mob.synced_data().client_data, undefined);
Deno.removeSync(dir, { recursive: true });
});