Water phyisics

This commit is contained in:
2026-10-07 19:38:27 -03:00
parent d101fa2ef3
commit 78117f65c9
13 changed files with 338 additions and 13 deletions
+16 -1
View File
@@ -8,7 +8,7 @@ import { TICKS_PER_SECOND } from "$/common/constants.ts";
import type { BlockRegistry } from "$/common/everything_registry.ts";
import type { ItemStack } from "$/common/inventory.ts";
import type { Shape } from "$/common/block_shapes.ts";
import { type Collisions, physics } from "$/common/physics.ts";
import { type Collisions, type LiquidInCell, physics } from "$/common/physics.ts";
import type { EntityData, EntityEvent, EntityInfo } from "$/common/protocol.ts";
import type { ServerPlayer } from "../player.ts";
@@ -67,6 +67,8 @@ export interface EntityLevel {
collision_shape(x: number, y: number, z: number): boolean | Shape;
// water, and other blocks you're in instead of on
is_liquid(x: number, y: number, z: number): boolean;
// the liquid there and how high it is, for liquids pushing things along
liquid_at(x: number, y: number, z: number): LiquidInCell | undefined;
get_block_info(x: number, y: number, z: number): BlockRegistry | undefined;
add_entity(entity: Entity): void;
get_entity(id: string): Entity | undefined;
@@ -229,6 +231,14 @@ export abstract class Entity {
this.fall_distance = 0;
this.fire = 0;
}
if (this.pushed_by_liquid) {
physics.push_by_liquid(
this,
(x, y, z) => this.level.liquid_at(x, y, z),
(x, y, z) => this.level.collision_shape(x, y, z),
false,
);
}
if (this.fire > 0) {
if (this.fire_immune) {
@@ -246,6 +256,11 @@ export abstract class Entity {
}
}
// flowing liquid pushes it along, like minecraft's isPushedByFluid
get pushed_by_liquid() {
return true;
}
update_in_water() {
this.in_water = physics.in_water(this, (x, y, z) => this.level.is_liquid(x, y, z));
}
+6
View File
@@ -4,6 +4,7 @@
import { CHUNK_SIZE } from "$/common/constants.ts";
import { is_within_view } from "$/common/chunk_streaming.ts";
import { is_liquid_block } from "$/common/everything_registry.ts";
import { liquid_in_cell } from "$/common/utils.ts";
import type { ItemStack } from "$/common/inventory.ts";
import type { EntityEvent, EntityInfo, ServerMessage } from "$/common/protocol.ts";
import type { ServerPlayer } from "../player.ts";
@@ -101,6 +102,11 @@ export class EntityManager implements EntityLevel {
return this.#host.world.get_block_info(x, y, z);
}
liquid_at(x: number, y: number, z: number) {
const world = this.#host.world;
return liquid_in_cell(world.get_block_value(x, y, z), world.get_block_info(x, y, z));
}
nearest_player(x: number, y: number, z: number, max_distance: number): ServerPlayer | undefined {
let nearest: ServerPlayer | undefined;
let nearest_distance = max_distance;
+21 -1
View File
@@ -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;
+5
View File
@@ -134,6 +134,11 @@ export class ServerPlayer extends LivingEntity {
// their client moves them, this only burns, falls out of the world and counts effects and hurt time down. while
// knocked back it moves them like a mob that isn't walking anywhere, landing hands them back to their client
// its client moves it, liquids included, see LocalPlayer
override get pushed_by_liquid() {
return false;
}
override tick() {
this.base_tick();
this.attack_strength_ticker += 1;