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
+98
View File
@@ -136,6 +136,104 @@ export interface ClientRender {
create: (block: VisualBlock, instances: Instances) => Visual,
options?: { replaces_model?: boolean },
): void;
// every entity of this type gets an animator from create while the client has it, which poses its model's bones
// each frame instead of the engine's head look and humanoid swing, like a model's setupAnim. only during setup,
// and one per type. see ANIMATION.md
register_entity_animator(type: Id, create: (entity: AnimatedEntity) => EntityAnimator): void;
// a clip, from a bedrock animation file in animations/: animation.walker.walk in animations/walker.animation.json
// is my_mod:walker/walk. throws when there isn't one, so a typo shows up at setup
clip(id: string): Clip;
}
// what poses one entity. time is its age on this client in ticks, with the part of the next one, the same time
// AnimationState takes. one that throws is removed, with the error in the console, and the entity posed without it
export interface EntityAnimator {
// before each frame the entity is drawn
pose(pose: EntityPose, time: number): void;
// a field of its client data changed (see MobRef.set_client_data in cubi/server), or was already set when the
// animator was made
on_data?(field: string, time: number): void;
}
// the entity an animator poses
export interface AnimatedEntity {
readonly id: string;
readonly type: Id;
// a field of its client data now, a copy, see MobRef.set_client_data in cubi/server
get_client_data<T>(field: string): T | undefined;
// how far it's walked, about 1 a tick at full speed, and how fast it's walking now, 0 to 1. they ease
// toward its movement, so a clip doesn't jump when it starts or stops
walk_position(time: number): number;
walk_speed(time: number): number;
// how far through its attack swing it is, 0 to 1, 0 when it isn't swinging
attack_progress(time: number): number;
// how a bone facing the way its body does turns to where it looks, in degrees, as EntityPose.rotate takes them
readonly head_pitch: number;
readonly head_yaw: number;
}
// a clip from animations/, see ClientRender.clip
export interface Clip {
readonly id: string;
// seconds
readonly length: number;
// one that doesn't stays on its last frame after its length
readonly loop: boolean;
}
// a model's bones for one frame, all at rest until something moves them. what's played and added adds up: rotations
// and positions add, scales multiply. bones are the model's by name, ones it doesn't have do nothing
export interface EntityPose {
// clip at seconds into it. weight scales what it does, 1 when not given
play(clip: Clip, seconds: number, weight?: number): void;
// clip from when state started, nothing while it's stopped
play_state(clip: Clip, state: AnimationState, time: number, weight?: number): void;
// clip as it walks, like neoforge's applyWalk: as far through as it's walked (a second of clip for 20 of
// walk_position) and as strongly as it's walking, times speed_scale, at most 1
play_walk(clip: Clip, entity: AnimatedEntity, time: number, speed_scale?: number): void;
// on top of the clips, in degrees, pixels and how much it's scaled, like the clips. bedrock's +x, like the model
rotate(bone: string, x: number, y: number, z: number): void;
move(bone: string, x: number, y: number, z: number): void;
scale(bone: string, x: number, y: number, z: number): void;
// turns a bone to where the entity looks, like the engine does a bone named head without an animator
look(bone: string): void;
}
// when something an animator plays started, like neoforge's AnimationState. time is in ticks, see EntityAnimator
export class AnimationState {
#started: number | undefined;
get started() {
return this.#started !== undefined;
}
start(time: number) {
this.#started = time;
}
stop() {
this.#started = undefined;
}
start_if_stopped(time: number) {
if (this.#started === undefined) this.#started = time;
}
// starts it when condition holds and it's stopped, and stops it when it doesn't
animate_when(condition: boolean, time: number) {
if (condition) this.start_if_stopped(time);
else this.stop();
}
// seconds since it started, at 20 ticks a second. 0 while it's stopped
seconds(time: number) {
return this.#started === undefined ? 0 : (time - this.#started) / 20;
}
// whether a clip that doesn't loop has played to its end since this started. never for one that loops
finished(clip: Clip, time: number) {
return this.started && !clip.loop && this.seconds(time) >= clip.length;
}
}
// the block a visual draws
+5
View File
@@ -284,6 +284,11 @@ export interface MobRef extends LivingRef {
jump(): void;
// swings its arm and hits target with its attack_damage, wherever target is
attack(target: Player | MobRef): void;
// what clients see of its state, like BlockRef's: what its animator reads (see register_entity_animator in
// cubi/client). each field is any json value, saved with it, and sent to clients when it changes. undefined
// removes one
get_client_data<T>(field: string): T | undefined;
set_client_data(field: string, value: unknown): void;
}
// commands, brigadier's way like neoforge's: see common/commands.ts and docs/modding/misc/commands.md