98 lines
3.6 KiB
TypeScript
98 lines
3.6 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
import type { EntityData, EntityEvent, EntityInfo } from "$/common/protocol.ts";
|
|
import type { EntityJson } from "$/common/mod_data.ts";
|
|
import { EverythingRegistry } from "$/common/everything_registry.ts";
|
|
import type { ClientLevel } from "../level/client_level.ts";
|
|
import { Entity } from "./entity.ts";
|
|
|
|
// how many ticks a server position takes to reach, like minecraft's lerpTo
|
|
const LERP_TICKS = 3;
|
|
// how long it flashes red after being hurt, and falls over after dying, like minecraft
|
|
const HURT_TICKS = 10;
|
|
const DEATH_TICKS = 20;
|
|
// how long its attack swing takes, like minecraft's mobs
|
|
const SWING_TICKS = 6;
|
|
|
|
// an entity the server simulates, like a mob. this only follows where it's told it is and keeps what it was told
|
|
// it looks like, for drawing it
|
|
export class RemoteEntity extends Entity {
|
|
// the entity type, like "bworld:item"
|
|
readonly type: string;
|
|
// synced data, see EntityData
|
|
data: EntityData;
|
|
head_yaw: number;
|
|
// counting down after a hurt event, and up after a death one
|
|
hurt_time = 0;
|
|
death_time = 0;
|
|
dead = false;
|
|
// what's riding it, by id
|
|
passengers: string[] = [];
|
|
// ticks into its attack swing, undefined when it isn't swinging
|
|
swing_ticks: number | undefined;
|
|
|
|
#target = { x: 0, y: 0, z: 0, yaw: 0, pitch: 0, head_yaw: 0 };
|
|
#lerp_ticks = 0;
|
|
|
|
constructor(level: ClientLevel, info: Extract<EntityInfo, { kind: "entity" }>) {
|
|
// the size of mods' entities, player sized for kinds the client doesn't know
|
|
const json = EverythingRegistry.get<EntityJson>("entities", info.type);
|
|
super(level, info.id, json?.width ?? 0.6, json?.height ?? 1.8, json ? json.height * 0.85 : 1.62);
|
|
this.type = info.type;
|
|
this.data = info.data;
|
|
this.set_position(info.x, info.y, info.z);
|
|
this.yaw = info.yaw;
|
|
this.pitch = info.pitch;
|
|
this.head_yaw = info.head_yaw ?? info.yaw;
|
|
this.#target = { x: info.x, y: info.y, z: info.z, yaw: info.yaw, pitch: info.pitch, head_yaw: this.head_yaw };
|
|
}
|
|
|
|
lerp_to(x: number, y: number, z: number, yaw: number, pitch: number, head_yaw = yaw) {
|
|
this.#target = { x, y, z, yaw, pitch, head_yaw };
|
|
this.#lerp_ticks = LERP_TICKS;
|
|
}
|
|
|
|
on_event(event: EntityEvent) {
|
|
if (event === "hurt") {
|
|
this.hurt_time = HURT_TICKS;
|
|
} else if (event === "swing") {
|
|
// swinging again halfway through doesn't restart it, like minecraft
|
|
if (this.swing_ticks === undefined || this.swing_ticks >= SWING_TICKS / 2) this.swing_ticks = 0;
|
|
} else {
|
|
this.dead = true;
|
|
}
|
|
}
|
|
|
|
tick() {
|
|
if (this.hurt_time > 0) this.hurt_time -= 1;
|
|
if (this.dead) this.death_time = Math.min(DEATH_TICKS, this.death_time + 1);
|
|
|
|
if (this.#lerp_ticks > 0) {
|
|
const t = 1 / this.#lerp_ticks;
|
|
const target = this.#target;
|
|
this.x += (target.x - this.x) * t;
|
|
this.y += (target.y - this.y) * t;
|
|
this.z += (target.z - this.z) * t;
|
|
this.yaw += wrap_angle(target.yaw - this.yaw) * t;
|
|
this.pitch += (target.pitch - this.pitch) * t;
|
|
this.head_yaw += wrap_angle(target.head_yaw - this.head_yaw) * t;
|
|
this.#lerp_ticks -= 1;
|
|
}
|
|
this.update_walk_animation();
|
|
if (this.swing_ticks !== undefined && ++this.swing_ticks >= SWING_TICKS) this.swing_ticks = undefined;
|
|
}
|
|
|
|
// how far through its attack swing it is, 0 to 1, like minecraft's attackTime. 0 when it isn't swinging
|
|
attack_animation(partial_tick: number) {
|
|
return this.swing_ticks === undefined ? 0 : Math.min(1, (this.swing_ticks + partial_tick) / SWING_TICKS);
|
|
}
|
|
}
|
|
|
|
function wrap_angle(angle: number) {
|
|
angle %= Math.PI * 2;
|
|
if (angle >= Math.PI) angle -= Math.PI * 2;
|
|
if (angle < -Math.PI) angle += Math.PI * 2;
|
|
return angle;
|
|
}
|