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
+3 -1
View File
@@ -72,6 +72,8 @@ export abstract class LivingEntity extends Entity {
// minecraft's per tick movement speed, see travel
speed = 0;
jumping = false;
// whether jumping lifts it off the ground. one that doesn't still steps up its step height and swims up in water
jumps = true;
#jump_delay = 0;
// the attributes this kind has and their base values, like minecraft's default attribute suppliers. kinds add
@@ -188,7 +190,7 @@ export abstract class LivingEntity extends Entity {
if (this.jumping) {
if (this.in_water) {
this.vy += SWIM_UP * TICKS_PER_SECOND;
} else if (this.on_ground && this.#jump_delay === 0) {
} else if (this.jumps && this.on_ground && this.#jump_delay === 0) {
this.jump_from_ground();
this.#jump_delay = JUMP_DELAY;
}
+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) {
+2
View File
@@ -40,6 +40,8 @@ export function mod_entity_factory(
override width = json.width;
override height = json.height;
override eye_height = json.height * 0.85;
override jumps = json.jumps ?? true;
override max_fall = json.max_fall ?? 3;
override get category(): MobCategory {
return category;