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
+8
View File
@@ -13,6 +13,7 @@ import type { EntityData } from "@/common/protocol.ts";
import type { ItemStack } from "@/common/inventory.ts";
import { wrap_angle } from "@/common/utils.ts";
import { TICK_DELTA } from "@/common/constants.ts";
import { is_object } from "@/common/schema.ts";
import { physics } from "@/common/physics.ts";
// minecraft's despawning distances: gone at once past the first, maybe after a while past the second
@@ -40,6 +41,7 @@ export interface SavedMob extends SavedLivingEntity {
home_pos?: [number, number, number];
home_radius?: number;
drop_chances?: Partial<Record<EquipmentSlot, number>>;
client_data?: Record<string, unknown>;
}
// what natural spawning counts a mob as, like minecraft's MobCategory
@@ -52,6 +54,9 @@ export abstract class Mob extends LivingEntity {
persistence_required = false;
can_pick_up_loot = false;
left_handed = false;
// what clients see of a mod's mob's state, see MobRef.set_client_data. replaced rather than changed, so a save never
// shares it
client_data: Record<string, unknown> = {};
// where it stays around, see is_within_home. no home when the radius is -1
home: { x: number; y: number; z: number } | undefined;
home_radius = -1;
@@ -265,6 +270,7 @@ export abstract class Mob extends LivingEntity {
override synced_data(): EntityData {
const data = super.synced_data();
if (this.left_handed) data.left_handed = true;
if (Object.keys(this.client_data).length) data.client_data = this.client_data;
return data;
}
@@ -281,6 +287,7 @@ export abstract class Mob extends LivingEntity {
saved.home_radius = this.home_radius;
}
if (Object.keys(this.drop_chances).length) saved.drop_chances = { ...this.drop_chances };
if (Object.keys(this.client_data).length) saved.client_data = this.client_data;
}
override load_additional(saved: SavedMob) {
@@ -289,6 +296,7 @@ export abstract class Mob extends LivingEntity {
this.persistence_required = saved.persistence_required === true;
this.can_pick_up_loot = saved.can_pick_up_loot === true;
this.left_handed = saved.left_handed === true;
this.client_data = is_object(saved.client_data) ? structuredClone(saved.client_data) : {};
if (Array.isArray(saved.home_pos) && typeof saved.home_radius === "number") {
const [x, y, z] = saved.home_pos.map((n) => finite(n, 0));
this.restrict_to(x, y, z, saved.home_radius);
+6
View File
@@ -750,6 +750,12 @@ export class ModRuntime {
mob.level.entity_event(mob, "swing");
mob.do_hurt_target(entity);
},
get_client_data: (field) => structuredClone(mob.client_data[field]) as never,
set_client_data(field, value) {
if (typeof field !== "string" || field === "") throw new Error("a client data field needs a name");
const { [field]: _, ...rest } = mob.client_data;
mob.client_data = value === undefined ? rest : { ...rest, [field]: structuredClone(value) };
},
};
return Object.defineProperties(living_ref(mob), Object.getOwnPropertyDescriptors(extra)) as MobRef;
}