182 lines
6.3 KiB
TypeScript
182 lines
6.3 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
import { type ItemData, ItemStack } from "$/common/inventory.ts";
|
|
import type { EntityInfo } from "$/common/protocol.ts";
|
|
import { TICK_DELTA } from "$/common/constants.ts";
|
|
import { physics } from "$/common/physics.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;
|
|
// deeper in water than this it floats instead of falling: speeding up 0.0005 blocks per tick each tick until it rises
|
|
// at 0.06 blocks per tick, slowing sideways a little, like minecraft's setUnderwaterMovement. it bobs at the surface
|
|
const FLOAT_DEPTH = 0.1;
|
|
const FLOAT_ACCELERATION = 0.0005 * 20;
|
|
const FLOAT_SPEED = 0.06 * 20;
|
|
const WATER_DRAG = 0.99;
|
|
// 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;
|
|
// it falls in tick() instead, see there
|
|
override gravity = 0;
|
|
|
|
// 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;
|
|
}
|
|
|
|
// 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.liquid_depth(this, (x, y, z) => level.liquid_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;
|
|
|
|
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. the cheap checks first, comparing data last
|
|
can_merge_with(other: ItemEntity) {
|
|
return other !== this && !other.removed && other.item.type_id === this.item.type_id &&
|
|
this.item.amount < this.item.max_amount && other.item.amount < other.item.max_amount &&
|
|
(other.item.data === this.item.data || JSON.stringify(other.item.data) === JSON.stringify(this.item.data));
|
|
}
|
|
|
|
// the smaller stack goes into the bigger one, like minecraft's ItemEntity.tryToMerge
|
|
merge_with_neighbours() {
|
|
// a full stack can't take more or give any, so there's no one to look for
|
|
if (this.item.amount >= this.item.max_amount) return;
|
|
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) || Math.abs(this.y - other.y) > ITEM_SIZE) continue;
|
|
if (!this.can_merge_with(other)) 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);
|
|
}
|
|
}
|