Files
bworld/server/game/entity/item_entity.ts
T
2026-09-28 15:12:06 -03:00

157 lines
4.9 KiB
TypeScript

import { type ItemData, ItemStack } from "$/common/inventory.ts";
import type { EntityInfo } from "$/common/protocol.ts";
import { type Attacker, type DamageSource, Entity, finite, type SavedEntity } from "./entity.ts";
export const ITEM_ENTITY_TYPE = "bworld:item";
// minecraft's item physics, its per tick numbers turned into per second ones where they're speeds
export const ITEM_SIZE = 0.25;
// 0.04 blocks per tick, per tick
const GRAVITY = -16;
// what's left of the speed after each tick
const AIR_DRAG = 0.98;
const GROUND_FRICTION = 0.6 * 0.98;
// slower than this counts as stopped, so items don't creep along forever
const MIN_SPEED = 0.01;
// how fast an item stuck inside a block floats out of it
const UNSTICK_SPEED = 2;
// items this close (past their own size) merge into one stack
const MERGE_DISTANCE = 0.5;
const MAX_HEALTH = 5;
// 5 minutes
export const DESPAWN_TICKS = 6000;
// how long before a popped block drop can be picked up, and one a player drops
export const BLOCK_DROP_PICKUP_DELAY = 10;
export const PLAYER_DROP_PICKUP_DELAY = 40;
export interface SavedItemEntity extends SavedEntity {
item: ItemData;
age: number;
pickup_delay: number;
health?: number;
}
// 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 {
readonly type = ITEM_ENTITY_TYPE;
item: ItemStack;
override width = ITEM_SIZE;
override height = ITEM_SIZE;
override eye_height = ITEM_SIZE / 2;
override gravity = GRAVITY;
// ticks until a player can pick it up
pickup_delay: number;
// fire, lava and explosions wear it down
health = MAX_HEALTH;
constructor(
level: Entity["level"],
item = new ItemStack("bworld:air", 0),
x = 0,
y = 0,
z = 0,
pickup_delay = 0,
) {
super(level);
this.item = item;
this.set_position(x, y, z);
this.pickup_delay = pickup_delay;
}
override tick() {
this.base_tick();
if (this.removed) return;
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
const level = this.level;
if (level.has_collision(Math.floor(this.x), Math.floor(this.y + this.height / 2), Math.floor(this.z))) {
this.vy = UNSTICK_SPEED;
}
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;
if (this.age >= DESPAWN_TICKS) {
this.remove();
} else if (this.age % 2 === 0) {
this.merge_with_neighbours();
}
}
// items don't get hurt by falling
override update_fall_distance() {}
override hurt(amount: number, source: DamageSource) {
if (this.is_invulnerable_to(source) || !(amount > 0)) return false;
this.health -= amount;
if (this.health <= 0) this.remove();
return true;
}
// whether two stacks could be one
can_merge_with(other: ItemEntity) {
return other !== this && !other.removed && other.item.type_id === this.item.type_id &&
JSON.stringify(other.item.data) === JSON.stringify(this.item.data) &&
this.item.amount < this.item.max_amount && other.item.amount < other.item.max_amount;
}
// the smaller stack goes into the bigger one, like minecraft's ItemEntity.tryToMerge
merge_with_neighbours() {
const reach = MERGE_DISTANCE + ITEM_SIZE;
for (const other of this.level.entities_near(this.x, this.y, this.z, reach)) {
if (this.removed) return;
if (!(other instanceof ItemEntity) || !this.can_merge_with(other)) continue;
if (Math.abs(this.y - other.y) > ITEM_SIZE) continue;
const [into, from] = this.item.amount >= other.item.amount ? [this, other] : [other, this];
const moving = Math.min(from.item.amount, into.item.max_amount - into.item.amount);
into.item.amount += moving;
from.item.amount -= moving;
into.pickup_delay = Math.max(into.pickup_delay, from.pickup_delay);
into.age = Math.min(into.age, from.age);
if (from.item.amount === 0) {
from.remove();
}
}
}
// someone took amount out of the stack. taking all of it takes the entity, which clients see fly to them
take(by: Attacker, amount: number) {
this.item.amount -= amount;
if (this.item.amount <= 0) {
this.level.take_item(this, by.id);
}
}
override info(): EntityInfo {
return { kind: "item", id: this.id, x: this.x, y: this.y, z: this.z, item: this.item.to_data() };
}
override save_additional(saved: SavedItemEntity) {
super.save_additional(saved);
saved.item = this.item.to_data();
saved.age = this.age;
saved.pickup_delay = this.pickup_delay;
if (this.health !== MAX_HEALTH) saved.health = this.health;
}
override load_additional(saved: SavedItemEntity) {
super.load_additional(saved);
this.item = ItemStack.from_data(saved.item);
this.age = finite(saved.age, 0);
this.pickup_delay = finite(saved.pickup_delay, 0);
this.health = finite(saved.health, MAX_HEALTH);
}
}