Basic entity system

This commit is contained in:
2026-09-28 15:12:06 -03:00
parent 84a622c3f1
commit 52ef4e5ca6
17 changed files with 3330 additions and 246 deletions
+4 -1
View File
@@ -3,6 +3,7 @@ import type { BlockHitResult } from "./level/client_level.ts";
import { LocalPlayer } from "./entity/local_player.ts";
import { RemotePlayer } from "./entity/remote_player.ts";
import { ItemEntity } from "./entity/item_entity.ts";
import { RemoteEntity } from "./entity/remote_entity.ts";
import { Camera } from "./camera.ts";
import { Options } from "./options.ts";
import { MultiPlayerGameMode } from "./game_mode.ts";
@@ -69,7 +70,9 @@ export class Client {
this.level.add_entity(new RemotePlayer(this.level, info));
}
for (const info of connection.initial_entities) {
this.level.add_entity(new ItemEntity(this.level, info));
this.level.add_entity(
info.kind === "item" ? new ItemEntity(this.level, info) : new RemoteEntity(this.level, info),
);
}
this.game_mode = new MultiPlayerGameMode(this);
+1 -1
View File
@@ -25,7 +25,7 @@ export class ItemEntity extends Entity {
#picked_up_by: Entity | undefined;
#pickup_age = 0;
constructor(level: ClientLevel, info: EntityInfo) {
constructor(level: ClientLevel, info: Extract<EntityInfo, { kind: "item" }>) {
super(level, info.id, 0.25, 0.25, 0.125);
this.item = ItemStack.from_data(info.item);
this.set_position(info.x, info.y, info.z);
+76
View File
@@ -0,0 +1,76 @@
import type { EntityData, EntityInfo } from "$/common/protocol.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;
// 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[] = [];
#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" }>) {
super(level, info.id, 0.6, 1.8, 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: "hurt" | "death") {
if (event === "hurt") {
this.hurt_time = HURT_TICKS;
} 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;
}
}
}
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;
}
+29 -1
View File
@@ -5,6 +5,7 @@ import { GuiContainer } from "./gui/gui_container.ts";
import { GuiChat } from "./gui/gui_chat.ts";
import { RemotePlayer } from "./entity/remote_player.ts";
import { ItemEntity } from "./entity/item_entity.ts";
import { RemoteEntity } from "./entity/remote_entity.ts";
import { Player } from "./entity/player.ts";
// minecraft's ChunkBatchSizeCalculator: how long loading a chunk takes here, so the server sends as many a tick as
@@ -94,12 +95,39 @@ export class ClientPacketListener {
break;
}
case "add_entity":
level.add_entity(new ItemEntity(level, message.entity));
level.add_entity(
message.entity.kind === "item"
? new ItemEntity(level, message.entity)
: new RemoteEntity(level, message.entity),
);
break;
case "move_entity": {
const entity = level.entities.get(message.id);
if (entity instanceof ItemEntity) {
entity.lerp_to(message.x, message.y, message.z);
} else if (entity instanceof RemoteEntity) {
entity.lerp_to(message.x, message.y, message.z, message.yaw, message.pitch, message.head_yaw);
}
break;
}
case "entity_data": {
const entity = level.entities.get(message.id);
if (entity instanceof RemoteEntity) {
entity.data = message.data;
}
break;
}
case "entity_event": {
const entity = level.entities.get(message.id);
if (entity instanceof RemoteEntity) {
entity.on_event(message.event);
}
break;
}
case "set_passengers": {
const entity = level.entities.get(message.id);
if (entity instanceof RemoteEntity) {
entity.passengers = message.passengers;
}
break;
}