This commit is contained in:
2026-11-22 13:21:27 -03:00
parent e751d22c6e
commit ac791b886e
23 changed files with 494 additions and 91 deletions
+13 -2
View File
@@ -16,7 +16,9 @@ import { sound_range, sound_to_play } from "@/common/mod_data.ts";
import type { ServerPlayer } from "../player.ts";
import type { ServerWorld } from "../world.ts";
import type { DamageSource, Entity, EntityLevel, SavedEntity } from "./entity.ts";
import { load_entity } from "./entity_types.ts";
import { create_entity, load_entity } from "./entity_types.ts";
import { ObjectEntity } from "./mod_entity.ts";
import { EverythingRegistry, type ItemRegistry } from "@/common/everything_registry.ts";
import { ItemEntity } from "./item_entity.ts";
import { MOBS_PATH_THROUGH } from "./pathfinding.ts";
import type { LivingEntity } from "./living_entity.ts";
@@ -180,7 +182,16 @@ export class EntityManager implements EntityLevel {
this.#host.after_hurt(entity, amount, source);
}
spawn_item(x: number, y: number, z: number, stack: ItemStack, pickup_delay = 0): ItemEntity {
// a cubi:item, or the entity the item lies around as instead, holding it (see ItemRegistry.entity)
spawn_item(x: number, y: number, z: number, stack: ItemStack, pickup_delay = 0): Entity {
const type = EverythingRegistry.get<ItemRegistry>("items", stack.type_id)?.entity;
const object = type === undefined ? undefined : create_entity(this, type);
if (object instanceof ObjectEntity) {
object.item = stack;
object.set_position(x, y, z);
this.add_entity(object);
return object;
}
const entity = new ItemEntity(this, stack, x, y, z, pickup_delay);
this.add_entity(entity);
return entity;
+32 -27
View File
@@ -43,6 +43,36 @@ export interface SavedItemEntity extends SavedEntity {
health?: number;
}
// one tick of falling, sliding and floating like an item lying around, for an entity whose own gravity is 0 (see
// ItemEntity.gravity)
export function move_like_item(entity: Entity) {
// covered by a block placed on it, float out the top instead of being stuck inside. only inside what it bumps
// into, like minecraft's noCollision, so it can lie in a block with a hollow, like a basin
const level = entity.level;
if (!physics.box_is_free(entity, 0, 0, 0, (x, y, z) => level.collision_shape(x, y, z))) {
entity.vy = UNSTICK_SPEED;
}
// like minecraft's ItemEntity.tick, gravity goes on its speed before it moves, not along the way like
// move_body's. the floating numbers are made for that: an item bobbing up out of the water drops straight back
// in, instead of hanging above it a tick longer and falling twice as fast
if (physics.fluid_depth(entity, (x, y, z) => level.fluid_at(x, y, z)) > FLOAT_DEPTH) {
entity.vx *= WATER_DRAG;
entity.vz *= WATER_DRAG;
if (entity.vy < FLOAT_SPEED) entity.vy += FLOAT_ACCELERATION;
} else if (!entity.no_gravity) {
entity.vy += GRAVITY * TICK_DELTA;
}
entity.move();
const friction = entity.on_ground ? GROUND_FRICTION : AIR_DRAG;
entity.vx *= friction;
entity.vy *= AIR_DRAG;
entity.vz *= friction;
if (Math.abs(entity.vx) < MIN_SPEED) entity.vx = 0;
if (Math.abs(entity.vz) < MIN_SPEED) entity.vz = 0;
}
// a stack of items lying in the world, like minecraft's ItemEntity. only the server simulates it,
// clients draw where they're told it is
export class ItemEntity extends Entity {
@@ -51,7 +81,7 @@ export class ItemEntity extends Entity {
override width = ITEM_SIZE;
override height = ITEM_SIZE;
override eye_height = ITEM_SIZE / 2;
// it falls in tick() instead, see there
// it falls in move_like_item instead, see there
override gravity = 0;
// ticks until a player can pick it up
@@ -79,32 +109,7 @@ export class ItemEntity extends Entity {
if (this.pickup_delay > 0) {
this.pickup_delay -= 1;
}
// covered by a block placed on it, float out the top instead of being stuck inside. only inside what it bumps
// into, like minecraft's noCollision, so it can lie in a block with a hollow, like a basin
const level = this.level;
if (!physics.box_is_free(this, 0, 0, 0, (x, y, z) => level.collision_shape(x, y, z))) {
this.vy = UNSTICK_SPEED;
}
// like minecraft's ItemEntity.tick, gravity goes on its speed before it moves, not along the way like
// move_body's. the floating numbers are made for that: an item bobbing up out of the water drops straight back
// in, instead of hanging above it a tick longer and falling twice as fast
if (physics.fluid_depth(this, (x, y, z) => level.fluid_at(x, y, z)) > FLOAT_DEPTH) {
this.vx *= WATER_DRAG;
this.vz *= WATER_DRAG;
if (this.vy < FLOAT_SPEED) this.vy += FLOAT_ACCELERATION;
} else if (!this.no_gravity) {
this.vy += GRAVITY * TICK_DELTA;
}
this.move();
const friction = this.on_ground ? GROUND_FRICTION : AIR_DRAG;
this.vx *= friction;
this.vy *= AIR_DRAG;
this.vz *= friction;
if (Math.abs(this.vx) < MIN_SPEED) this.vx = 0;
if (Math.abs(this.vz) < MIN_SPEED) this.vz = 0;
move_like_item(this);
if (this.age >= DESPAWN_TICKS) {
this.remove();
+56 -3
View File
@@ -2,11 +2,14 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
// the kinds of entity mods define in entities/<name>.json (see EntityJson): animals that wander around, panic when hurt
// and look at players now and then, like minecraft's simpler animals, monsters that go after players, and scripted
// ones that stay where scripts put them
// and look at players now and then, like minecraft's simpler animals, monsters that go after players, scripted ones
// that stay where scripts put them, and objects that lie around holding an item
import { type ItemData, ItemStack } from "@/common/inventory.ts";
import { entity_category, type EntityJson } from "@/common/mod_data.ts";
import type { EntityData } from "@/common/protocol.ts";
import { ATTRIBUTES, type AttributeType } from "./attributes.ts";
import { Entity } from "./entity.ts";
import { Entity, type SavedEntity } from "./entity.ts";
import { move_like_item } from "./item_entity.ts";
import type { EntityFactory } from "./entity_types.ts";
import {
FloatGoal,
@@ -30,6 +33,9 @@ export function mod_entity_factory(
if (json.behavior === "scripted") {
return (level) => new ScriptedEntity(level, json);
}
if (json.behavior === "object") {
return (level) => new ObjectEntity(level, json);
}
const monster = json.behavior === "monster";
const behavior = json.behavior?.includes(":") ? json.behavior : undefined;
@@ -110,3 +116,50 @@ class ScriptedEntity extends Entity {
return false;
}
}
// a thing lying around, holding an item, like neoforge's custom item entities (see ItemRegistry.entity): it falls,
// slides and floats like a dropped item, but nobody picks it up by walking into it, it never despawns or merges, and it
// can't be hurt. what happens when it's clicked is up to mods
export class ObjectEntity extends Entity {
readonly type: string;
// it falls in move_like_item instead
override gravity = 0;
item: ItemStack | undefined;
constructor(level: Entity["level"], json: EntityJson) {
super(level);
this.type = json.id;
this.width = json.width;
this.height = json.height;
this.eye_height = json.height * 0.85;
}
override tick() {
this.base_tick();
if (this.removed || this.vehicle) return;
move_like_item(this);
}
// it isn't hurt by falling either
override update_fall_distance() {}
override synced_data(): EntityData {
const data = super.synced_data();
if (this.item) data.item = this.item.to_synced_data();
return data;
}
override save_additional(saved: SavedObjectEntity) {
super.save_additional(saved);
if (this.item) saved.item = this.item.to_data();
}
override load_additional(saved: SavedObjectEntity) {
super.load_additional(saved);
this.item = saved.item ? ItemStack.from_data(saved.item) : undefined;
}
}
interface SavedObjectEntity extends SavedEntity {
item?: ItemData;
}