Water phyisics
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
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";
|
||||
@@ -15,6 +17,12 @@ const GROUND_FRICTION = 0.6 * 0.98;
|
||||
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;
|
||||
@@ -40,7 +48,8 @@ export class ItemEntity extends Entity {
|
||||
override width = ITEM_SIZE;
|
||||
override height = ITEM_SIZE;
|
||||
override eye_height = ITEM_SIZE / 2;
|
||||
override gravity = GRAVITY;
|
||||
// it falls in tick() instead, see there
|
||||
override gravity = 0;
|
||||
|
||||
// ticks until a player can pick it up
|
||||
pickup_delay: number;
|
||||
@@ -74,6 +83,17 @@ export class ItemEntity extends Entity {
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user