Entity movement
This commit is contained in:
@@ -72,6 +72,8 @@ export abstract class LivingEntity extends Entity {
|
|||||||
// minecraft's per tick movement speed, see travel
|
// minecraft's per tick movement speed, see travel
|
||||||
speed = 0;
|
speed = 0;
|
||||||
jumping = false;
|
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;
|
#jump_delay = 0;
|
||||||
|
|
||||||
// the attributes this kind has and their base values, like minecraft's default attribute suppliers. kinds add
|
// 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.jumping) {
|
||||||
if (this.in_water) {
|
if (this.in_water) {
|
||||||
this.vy += SWIM_UP * TICKS_PER_SECOND;
|
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_from_ground();
|
||||||
this.#jump_delay = JUMP_DELAY;
|
this.#jump_delay = JUMP_DELAY;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ import { EQUIPMENT_SLOTS, type EquipmentSlot, LivingEntity, type SavedLivingEnti
|
|||||||
import type { EntityData } from "@/common/protocol.ts";
|
import type { EntityData } from "@/common/protocol.ts";
|
||||||
import type { ItemStack } from "@/common/inventory.ts";
|
import type { ItemStack } from "@/common/inventory.ts";
|
||||||
import { wrap_angle } from "@/common/utils.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
|
// minecraft's despawning distances: gone at once past the first, maybe after a while past the second
|
||||||
const DESPAWN_DISTANCE = 128;
|
const DESPAWN_DISTANCE = 128;
|
||||||
@@ -58,6 +60,8 @@ export abstract class Mob extends LivingEntity {
|
|||||||
no_action_time = 0;
|
no_action_time = 0;
|
||||||
// Math.random, tests replace it
|
// Math.random, tests replace it
|
||||||
random: () => number = Math.random;
|
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
|
// what it does, most important first. target goals pick who to go after, like minecraft's targetSelector
|
||||||
goals = new GoalSelector();
|
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
|
// the goals, then the controls, like minecraft's Mob.serverAiStep
|
||||||
override server_ai_step() {
|
override server_ai_step() {
|
||||||
if (!this.#registered_goals) {
|
if (!this.#registered_goals) {
|
||||||
|
|||||||
@@ -40,6 +40,8 @@ export function mod_entity_factory(
|
|||||||
override width = json.width;
|
override width = json.width;
|
||||||
override height = json.height;
|
override height = json.height;
|
||||||
override eye_height = json.height * 0.85;
|
override eye_height = json.height * 0.85;
|
||||||
|
override jumps = json.jumps ?? true;
|
||||||
|
override max_fall = json.max_fall ?? 3;
|
||||||
|
|
||||||
override get category(): MobCategory {
|
override get category(): MobCategory {
|
||||||
return category;
|
return category;
|
||||||
|
|||||||
@@ -3,12 +3,19 @@
|
|||||||
|
|
||||||
// mobs mods define in entities/<name>.json, drawn with a bedrock model from models/<name>.geo.json
|
// mobs mods define in entities/<name>.json, drawn with a bedrock model from models/<name>.geo.json
|
||||||
import { assert, assertAlmostEquals, assertEquals, assertNotEquals, assertStringIncludes } from "@std/assert";
|
import { assert, assertAlmostEquals, assertEquals, assertNotEquals, assertStringIncludes } from "@std/assert";
|
||||||
import { entity_category, entity_model_from_geo, type EntityBoneJson, validate_entity } from "@/common/mod_data.ts";
|
import {
|
||||||
|
entity_category,
|
||||||
|
entity_model_from_geo,
|
||||||
|
type EntityBoneJson,
|
||||||
|
type EntityJson,
|
||||||
|
validate_entity,
|
||||||
|
} from "@/common/mod_data.ts";
|
||||||
import type { Texture } from "@/client/renderer/mod.ts";
|
import type { Texture } from "@/client/renderer/mod.ts";
|
||||||
import { IDENTITY } from "@/client/rendering/model_math.ts";
|
import { IDENTITY } from "@/client/rendering/model_math.ts";
|
||||||
import { push_model } from "@/client/rendering/model_renderer.ts";
|
import { push_model } from "@/client/rendering/model_renderer.ts";
|
||||||
import { Mob } from "@/server/game/entity/mob.ts";
|
import { Mob } from "@/server/game/entity/mob.ts";
|
||||||
import { create_entity } from "@/server/game/entity/entity_types.ts";
|
import { create_entity } from "@/server/game/entity/entity_types.ts";
|
||||||
|
import { mod_entity_factory } from "@/server/game/entity/mod_entity.ts";
|
||||||
import { container_items, test_game } from "./helpers.ts";
|
import { container_items, test_game } from "./helpers.ts";
|
||||||
|
|
||||||
Deno.test("a bedrock geo.json becomes an entity model with only what the engine uses", () => {
|
Deno.test("a bedrock geo.json becomes an entity model with only what the engine uses", () => {
|
||||||
@@ -164,3 +171,64 @@ Deno.test("solid blocks can't be placed where a mob or player is", async () => {
|
|||||||
send(1, { type: "use_block", x: 3, y: 100, z: 3, face: "top" });
|
send(1, { type: "use_block", x: 3, y: 100, z: 3, face: "top" });
|
||||||
assertEquals(game.world.get_block_id(3, 101, 3), "cubi:air");
|
assertEquals(game.world.get_block_id(3, 101, 3), "cubi:air");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// a mob with no goals, so it only walks where it's sent, at 0.5 100 0.5 on a stone floor that ends at x 3 with a deep
|
||||||
|
// pit beyond, and a wall at x 2 when wall is set. alice is there too, so the mob thinks
|
||||||
|
async function walker(fields: Partial<EntityJson>, wall = false) {
|
||||||
|
const t = await test_game("mods");
|
||||||
|
for (let x = -4; x <= 8; x++) {
|
||||||
|
for (let z = -4; z <= 4; z++) {
|
||||||
|
for (let y = 90; y < 104; y++) t.game.set_block(x, y, z, y === 99 && x <= 3 ? "bworld:stone" : "cubi:air");
|
||||||
|
if (wall && x === 2) t.game.set_block(x, 100, z, "bworld:stone");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
t.join(1, "alice");
|
||||||
|
t.send(1, { type: "move", x: -3.5, y: 100, z: -3.5, yaw: 0, pitch: 0 });
|
||||||
|
const json: EntityJson = {
|
||||||
|
id: "test:walker",
|
||||||
|
texture: "bworld:chicken",
|
||||||
|
behavior: "test:still",
|
||||||
|
width: 0.6,
|
||||||
|
height: 1.8,
|
||||||
|
...fields,
|
||||||
|
};
|
||||||
|
const mob = mod_entity_factory(json, new Map([["test:still", () => {}]]))(t.game.entities) as Mob;
|
||||||
|
mob.set_position(0.5, 100, 0.5);
|
||||||
|
t.game.entities.add_entity(mob);
|
||||||
|
return { game: t.game, mob };
|
||||||
|
}
|
||||||
|
|
||||||
|
// the highest and lowest its feet got over ticks, walking to x
|
||||||
|
function walk(game: { tick(): void }, mob: Mob, x: number, ticks: number) {
|
||||||
|
let [lowest, highest] = [mob.y, mob.y];
|
||||||
|
for (let i = 0; i < ticks; i++) {
|
||||||
|
mob.move_control.move_to(x, 100, 0.5, 1);
|
||||||
|
game.tick();
|
||||||
|
lowest = Math.min(lowest, mob.y);
|
||||||
|
highest = Math.max(highest, mob.y);
|
||||||
|
}
|
||||||
|
return { lowest, highest };
|
||||||
|
}
|
||||||
|
|
||||||
|
Deno.test("a mob that doesn't jump stays down at a wall a mob that jumps gets onto", async () => {
|
||||||
|
const jumper = await walker({}, true);
|
||||||
|
assert(walk(jumper.game, jumper.mob, 2.5, 60).highest >= 101);
|
||||||
|
|
||||||
|
const stays = await walker({ jumps: false }, true);
|
||||||
|
assertAlmostEquals(walk(stays.game, stays.mob, 2.5, 60).highest, 100, 1e-6);
|
||||||
|
});
|
||||||
|
|
||||||
|
Deno.test("a mob that doesn't drop stops at an edge, unless something knocks it off", async () => {
|
||||||
|
const faller = await walker({});
|
||||||
|
assert(walk(faller.game, faller.mob, 6.5, 100).lowest < 99);
|
||||||
|
|
||||||
|
const { game, mob } = await walker({ max_fall: 0 });
|
||||||
|
assertAlmostEquals(walk(game, mob, 6.5, 100).lowest, 100, 1e-6);
|
||||||
|
// like a sneaking player, it can lean out until only the edge of its box is over the floor
|
||||||
|
assert(mob.x - mob.width / 2 < 4, `it got to x ${mob.x}`);
|
||||||
|
|
||||||
|
// hit from the side away from the pit
|
||||||
|
mob.knockback(0.4, -1, 0);
|
||||||
|
for (let i = 0; i < 40; i++) game.tick();
|
||||||
|
assert(mob.y < 99, `it's still at y ${mob.y}`);
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user