Entity movement

This commit is contained in:
2026-11-21 13:23:12 -03:00
parent dd53613758
commit 0f58a9cbfe
4 changed files with 96 additions and 2 deletions
+22
View File
@@ -11,6 +11,8 @@ import { EQUIPMENT_SLOTS, type EquipmentSlot, LivingEntity, type SavedLivingEnti
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 { physics } from "@/common/physics.ts";
// minecraft's despawning distances: gone at once past the first, maybe after a while past the second
const DESPAWN_DISTANCE = 128;
@@ -58,6 +60,8 @@ export abstract class Mob extends LivingEntity {
no_action_time = 0;
// Math.random, tests replace it
random: () => number = Math.random;
// how many blocks it drops on its way, see PATHFINDING.md. 0 never walks off an edge
max_fall = 3;
// what it does, most important first. target goals pick who to go after, like minecraft's targetSelector
goals = new GoalSelector();
@@ -106,6 +110,24 @@ export abstract class Mob extends LivingEntity {
}
}
// a mob that never drops stops at edges like a sneaking player, still stepping down its step height. not while it's
// off the ground, so something that knocks it into the air can still knock it off
override move() {
if (this.max_fall === 0 && this.on_ground && this.vy <= 0) {
const is_solid = (x: number, y: number, z: number) => this.level.collision_shape(x, y, z);
const [dx, dz] = physics.back_off_from_edge(
this,
this.vx * TICK_DELTA,
this.vz * TICK_DELTA,
this.step_height,
is_solid,
);
this.vx = dx / TICK_DELTA;
this.vz = dz / TICK_DELTA;
}
return super.move();
}
// the goals, then the controls, like minecraft's Mob.serverAiStep
override server_ai_step() {
if (!this.#registered_goals) {