Basic swimming

This commit is contained in:
2026-10-01 21:12:30 -03:00
parent 6e5703a482
commit d5015edfa1
10 changed files with 203 additions and 20 deletions
+3 -6
View File
@@ -7,7 +7,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 Collisions, move_body } from "$/common/physics.ts";
import { type Collisions, eyes_in_water, in_water, move_body } from "$/common/physics.ts";
import type { EntityData, EntityEvent, EntityInfo } from "$/common/protocol.ts";
import type { ServerPlayer } from "../player.ts";
@@ -239,15 +239,12 @@ export abstract class Entity {
}
update_in_water() {
const x = Math.floor(this.x);
const z = Math.floor(this.z);
this.in_water = this.level.is_liquid(x, Math.floor(this.y), z) ||
this.level.is_liquid(x, Math.floor(this.y + this.height / 2), z);
this.in_water = in_water(this, (x, y, z) => this.level.is_liquid(x, y, z));
}
// its eyes are in water, so it can't breathe
get eyes_in_water() {
return this.level.is_liquid(Math.floor(this.x), Math.floor(this.y + this.eye_height), Math.floor(this.z));
return eyes_in_water(this, this.eye_height, (x, y, z) => this.level.is_liquid(x, y, z));
}
// falls and moves by its velocity for one tick (see move_body), and counts how far it fell. returns what it hit
+2 -2
View File
@@ -3,6 +3,7 @@
// when those move or change, and forgets them when they leave it
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 type { ItemStack } from "$/common/inventory.ts";
import type { EntityEvent, EntityInfo, ServerMessage } from "$/common/protocol.ts";
import type { ServerPlayer } from "../player.ts";
@@ -89,8 +90,7 @@ export class EntityManager implements EntityLevel {
}
is_liquid(x: number, y: number, z: number) {
const info = this.#host.world.get_block_info(x, y, z);
return info !== undefined && !info.has_collision && info.replaceable === true;
return is_liquid_block(this.#host.world.get_block_info(x, y, z));
}
get_block_info(x: number, y: number, z: number) {
+2 -3
View File
@@ -10,7 +10,7 @@ import {
TICK_DELTA,
TICKS_PER_SECOND,
} from "$/common/constants.ts";
import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
import { BlockRegistry, EverythingRegistry, is_liquid_block, ItemRegistry } from "$/common/everything_registry.ts";
import { Container, ItemData, ItemStack } from "$/common/inventory.ts";
import {
AIR_ID,
@@ -1446,8 +1446,7 @@ export class GameServer {
// in a block entities pass through and players can place into, like water
#in_liquid(x: number, y: number, z: number) {
const info = this.world.get_block_info(Math.floor(x), Math.floor(y), Math.floor(z));
return info !== undefined && !info.has_collision && info.replaceable === true;
return is_liquid_block(this.world.get_block_info(Math.floor(x), Math.floor(y), Math.floor(z)));
}
// right click with food: it's eaten if they're hungry, or it's always edible