182 lines
6.1 KiB
TypeScript
182 lines
6.1 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// mods' entity animators, see ClientRender.register_entity_animator and docs/modding/entities/index.md#animators. each
|
|
// entity of a type with one gets its own, made with the entity, which says what its model's bones do every frame it's
|
|
// drawn
|
|
import type { AnimatedEntity, Clip, EntityAnimator, EntityPose } from "@/common/mod_api/client.ts";
|
|
import type { ClipJson } from "@/common/mod_data.ts";
|
|
import type { EntityData } from "@/common/protocol.ts";
|
|
import { EverythingRegistry } from "@/common/everything_registry.ts";
|
|
import { wrap_angle } from "@/common/utils.ts";
|
|
import { rest_offsets, sample_clip } from "./animation.ts";
|
|
import type { BoneOffsets } from "./model_math.ts";
|
|
|
|
export interface AnimatorRegistration {
|
|
mod: string;
|
|
create: (entity: AnimatedEntity) => EntityAnimator;
|
|
}
|
|
|
|
// by entity type
|
|
export const entity_animators = new Map<string, AnimatorRegistration>();
|
|
|
|
// what an animator reads of its entity, a RemoteEntity's
|
|
export interface AnimatorSource {
|
|
readonly id: string;
|
|
readonly type: string;
|
|
readonly data: EntityData;
|
|
// ticks since this client got it
|
|
readonly age: number;
|
|
readonly yaw: number;
|
|
readonly head_yaw: number;
|
|
readonly pitch: number;
|
|
walk_animation(partial_tick: number): { position: number; speed: number };
|
|
attack_animation(partial_tick: number): number;
|
|
}
|
|
|
|
const DEGREES = 180 / Math.PI;
|
|
|
|
// one entity's animator, undefined for a type without one
|
|
export function animation_for(source: AnimatorSource): EntityAnimation | undefined {
|
|
const registration = entity_animators.get(source.type);
|
|
return registration && new EntityAnimation(source, registration);
|
|
}
|
|
|
|
export class EntityAnimation {
|
|
#source: AnimatorSource;
|
|
#mod: string;
|
|
#entity: AnimatedEntity;
|
|
#animator: EntityAnimator | undefined;
|
|
|
|
constructor(source: AnimatorSource, { mod, create }: AnimatorRegistration) {
|
|
this.#source = source;
|
|
this.#mod = mod;
|
|
this.#entity = animated_entity(source);
|
|
this.#run("create", () => this.#animator = create(this.#entity));
|
|
for (const field of Object.keys(client_data(source.data))) this.#on_data(field);
|
|
}
|
|
|
|
// its client data was before and is now the source's
|
|
data_changed(before: EntityData) {
|
|
const old = client_data(before);
|
|
const now = client_data(this.#source.data);
|
|
for (const field of new Set([...Object.keys(old), ...Object.keys(now)])) {
|
|
if (JSON.stringify(old[field]) !== JSON.stringify(now[field])) this.#on_data(field);
|
|
}
|
|
}
|
|
|
|
// what the animator does to the bones at time, undefined when there's no animator (anymore)
|
|
pose(time: number): Map<string, BoneOffsets> | undefined {
|
|
const animator = this.#animator;
|
|
if (!animator) return undefined;
|
|
const offsets = new Map<string, BoneOffsets>();
|
|
const posed = this.#run("pose", () => animator.pose(entity_pose(offsets, this.#entity), time));
|
|
return posed ? offsets : undefined;
|
|
}
|
|
|
|
// the sound its next step makes, what the animator says when it has a step_sound, own otherwise
|
|
step_sound(own: string | undefined): string | undefined {
|
|
const animator = this.#animator;
|
|
if (!animator?.step_sound) return own;
|
|
let sound: string | undefined;
|
|
return this.#run("step_sound", () => sound = animator.step_sound!()) ? sound : own;
|
|
}
|
|
|
|
#on_data(field: string) {
|
|
const animator = this.#animator;
|
|
if (animator?.on_data) this.#run("on_data", () => animator.on_data!(field, this.#source.age));
|
|
}
|
|
|
|
// false when it threw, which is logged with the mod and the entity, and the animator is gone
|
|
#run(what: string, fn: () => void) {
|
|
try {
|
|
fn();
|
|
return true;
|
|
} catch (e) {
|
|
const { type, id } = this.#source;
|
|
console.error(`[${this.#mod}] the animator of ${type} ${id} threw in ${what}, it's removed:`, e);
|
|
this.#animator = undefined;
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
function client_data(data: EntityData): Record<string, unknown> {
|
|
return (data.client_data ?? {}) as Record<string, unknown>;
|
|
}
|
|
|
|
function animated_entity(source: AnimatorSource): AnimatedEntity {
|
|
// time is in ticks with the part of the next one, the source's walk and attack take that part
|
|
const partial = (time: number) => Math.min(Math.max(time - source.age, 0), 1);
|
|
return {
|
|
get id() {
|
|
return source.id;
|
|
},
|
|
get type() {
|
|
return source.type;
|
|
},
|
|
get_client_data: (field) => structuredClone(client_data(source.data)[field]) as never,
|
|
walk_position: (time) => source.walk_animation(partial(time)).position,
|
|
walk_speed: (time) => source.walk_animation(partial(time)).speed,
|
|
attack_progress: (time) => source.attack_animation(partial(time)),
|
|
// mirrored like a model's rotations, see model_math's own_matrix
|
|
get head_pitch() {
|
|
return -source.pitch * DEGREES;
|
|
},
|
|
get head_yaw() {
|
|
return -wrap_angle(source.head_yaw - source.yaw) * DEGREES;
|
|
},
|
|
};
|
|
}
|
|
|
|
// a clip by what ClientRender.clip gave
|
|
function clip_json(clip: Clip): ClipJson {
|
|
const json = EverythingRegistry.get<ClipJson>("animations", clip?.id);
|
|
if (!json) throw new Error(`${clip?.id} isn't a clip, see ctx.render.clip`);
|
|
return json;
|
|
}
|
|
|
|
function entity_pose(offsets: Map<string, BoneOffsets>, entity: AnimatedEntity): EntityPose {
|
|
const offset = (bone: string) => {
|
|
let found = offsets.get(bone);
|
|
if (!found) {
|
|
found = rest_offsets();
|
|
offsets.set(bone, found);
|
|
}
|
|
return found;
|
|
};
|
|
const pose: EntityPose = {
|
|
play(clip, seconds, weight = 1) {
|
|
sample_clip(clip_json(clip), seconds, offsets, weight);
|
|
},
|
|
play_state(clip, state, time, weight) {
|
|
if (state.started) pose.play(clip, state.seconds(time), weight);
|
|
},
|
|
play_walk(clip, walker, time, speed_scale = 1) {
|
|
pose.play(clip, walker.walk_position(time) / 20, Math.min(walker.walk_speed(time) * speed_scale, 1));
|
|
},
|
|
rotate(bone, x, y, z) {
|
|
const rotation = offset(bone).rotation;
|
|
rotation[0] += x;
|
|
rotation[1] += y;
|
|
rotation[2] += z;
|
|
},
|
|
move(bone, x, y, z) {
|
|
const position = offset(bone).position;
|
|
position[0] += x;
|
|
position[1] += y;
|
|
position[2] += z;
|
|
},
|
|
scale(bone, x, y, z) {
|
|
const scale = offset(bone).scale;
|
|
scale[0] *= x;
|
|
scale[1] *= y;
|
|
scale[2] *= z;
|
|
},
|
|
look(bone) {
|
|
pose.rotate(bone, entity.head_pitch, entity.head_yaw, 0);
|
|
},
|
|
};
|
|
return pose;
|
|
}
|