Swimming
This commit is contained in:
@@ -4,8 +4,6 @@ import {
|
||||
FACE_OFFSETS,
|
||||
Faces,
|
||||
faces,
|
||||
PLAYER_EYE_HEIGHT,
|
||||
PLAYER_HEIGHT,
|
||||
PLAYER_WIDTH,
|
||||
TICK_DELTA,
|
||||
TICKS_PER_SECOND,
|
||||
@@ -437,7 +435,7 @@ export class GameServer {
|
||||
throw_item(player: ServerPlayer, stack: ItemStack) {
|
||||
const entity = this.spawn_item(
|
||||
player.x,
|
||||
player.y + PLAYER_EYE_HEIGHT - 0.3,
|
||||
player.y + player.eye_height - 0.3,
|
||||
player.z,
|
||||
stack,
|
||||
PLAYER_DROP_PICKUP_DELAY,
|
||||
@@ -480,7 +478,7 @@ export class GameServer {
|
||||
player.teleport_id += 1;
|
||||
this.#send(player, { type: "teleport", id: player.teleport_id, x, y, z, ...(motion && { motion }) });
|
||||
this.#update_view(player);
|
||||
this.#broadcast({ type: "player_move", id: player.id, x, y, z, yaw: player.yaw, pitch: player.pitch }, player);
|
||||
this.#broadcast(player_move(player), player);
|
||||
}
|
||||
|
||||
set_time(time: number) {
|
||||
@@ -758,7 +756,8 @@ export class GameServer {
|
||||
}
|
||||
this.#on_move(player, x, y, z);
|
||||
Object.assign(player, { x, y, z, yaw, pitch });
|
||||
this.#broadcast({ type: "player_move", id: player.id, x, y, z, yaw, pitch }, player);
|
||||
player.set_swimming(message.swimming === true);
|
||||
this.#broadcast(player_move(player), player);
|
||||
this.#update_view(player);
|
||||
break;
|
||||
}
|
||||
@@ -1009,7 +1008,7 @@ export class GameServer {
|
||||
#occupied(x: number, y: number, z: number) {
|
||||
return this.players().some((player) =>
|
||||
box_overlaps_block(
|
||||
{ x: player.x, y: player.y, z: player.z, width: PLAYER_WIDTH, height: PLAYER_HEIGHT },
|
||||
{ x: player.x, y: player.y, z: player.z, width: PLAYER_WIDTH, height: player.height },
|
||||
x,
|
||||
y,
|
||||
z,
|
||||
@@ -1493,7 +1492,7 @@ export class GameServer {
|
||||
const reach_xz = PLAYER_WIDTH / 2 + PICKUP_REACH_XZ + ITEM_SIZE / 2;
|
||||
return Math.abs(player.x - entity.x) <= reach_xz && Math.abs(player.z - entity.z) <= reach_xz &&
|
||||
entity.y + ITEM_SIZE >= player.y - PICKUP_REACH_Y &&
|
||||
entity.y <= player.y + PLAYER_HEIGHT + PICKUP_REACH_Y;
|
||||
entity.y <= player.y + player.height + PICKUP_REACH_Y;
|
||||
}
|
||||
|
||||
// as much as fits, the rest stays on the ground
|
||||
@@ -1629,14 +1628,14 @@ export class GameServer {
|
||||
|
||||
#in_reach(player: ServerPlayer, x: number, y: number, z: number) {
|
||||
const dx = x + 0.5 - player.x;
|
||||
const dy = y + 0.5 - (player.y + PLAYER_EYE_HEIGHT);
|
||||
const dy = y + 0.5 - (player.y + player.eye_height);
|
||||
const dz = z + 0.5 - player.z;
|
||||
return dx * dx + dy * dy + dz * dz <= MAX_REACH * MAX_REACH;
|
||||
}
|
||||
|
||||
// from the eyes to the closest point of its box
|
||||
#entity_in_reach(player: ServerPlayer, entity: LivingEntity) {
|
||||
const eye_y = player.y + PLAYER_EYE_HEIGHT;
|
||||
const eye_y = player.y + player.eye_height;
|
||||
const half = entity.width / 2;
|
||||
const dx = Math.max(entity.x - half - player.x, 0, player.x - entity.x - half);
|
||||
const dy = Math.max(entity.y - eye_y, 0, eye_y - entity.y - entity.height);
|
||||
@@ -1779,3 +1778,9 @@ function placed_state(block_id: string, player: ServerPlayer): number | undefine
|
||||
const facing = ((quarter % 4) + 4) % 4;
|
||||
return set_state_value(default_block_value(0, info), info, "facing", facing)! >>> 16;
|
||||
}
|
||||
|
||||
// where a player is now, for everyone else
|
||||
function player_move(player: ServerPlayer): ServerMessage {
|
||||
const { id, x, y, z, yaw, pitch } = player;
|
||||
return { type: "player_move", id, x, y, z, yaw, pitch, ...(player.swimming && { swimming: true }) };
|
||||
}
|
||||
|
||||
@@ -1002,6 +1002,7 @@ function player_api(game: GameServer, player: ServerPlayer): Player {
|
||||
return effect && { amplifier: effect.amplifier, duration: effect.duration };
|
||||
},
|
||||
remove_effect: (id) => player.remove_effect(known_effect(id)),
|
||||
is_swimming: () => player.swimming,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
+17
-1
@@ -1,4 +1,10 @@
|
||||
import { PLAYER_EYE_HEIGHT, PLAYER_HEIGHT, PLAYER_WIDTH } from "$/common/constants.ts";
|
||||
import {
|
||||
PLAYER_EYE_HEIGHT,
|
||||
PLAYER_HEIGHT,
|
||||
PLAYER_SWIM_EYE_HEIGHT,
|
||||
PLAYER_SWIM_HEIGHT,
|
||||
PLAYER_WIDTH,
|
||||
} from "$/common/constants.ts";
|
||||
import { EverythingRegistry, type ItemRegistry } from "$/common/everything_registry.ts";
|
||||
import { Container, Cursor, ItemData, ItemStack } from "$/common/inventory.ts";
|
||||
import type { GridRecipe } from "$/common/mod_data.ts";
|
||||
@@ -45,6 +51,8 @@ export class ServerPlayer extends LivingEntity {
|
||||
override width = PLAYER_WIDTH;
|
||||
override height = PLAYER_HEIGHT;
|
||||
override eye_height = PLAYER_EYE_HEIGHT;
|
||||
// lying flat in the short box, as their client says. their client decides, like where they are
|
||||
swimming = false;
|
||||
|
||||
inventory = new Container(INVENTORY_SIZE);
|
||||
// hunger and saturation, health is the entity's
|
||||
@@ -180,6 +188,13 @@ export class ServerPlayer extends LivingEntity {
|
||||
return this.inventory.get_item(this.selected_slot);
|
||||
}
|
||||
|
||||
// the short box and low eyes while swimming, so drowning, reach and what they're in the way of match
|
||||
set_swimming(swimming: boolean) {
|
||||
this.swimming = swimming;
|
||||
this.height = swimming ? PLAYER_SWIM_HEIGHT : PLAYER_HEIGHT;
|
||||
this.eye_height = swimming ? PLAYER_SWIM_EYE_HEIGHT : PLAYER_EYE_HEIGHT;
|
||||
}
|
||||
|
||||
player_info(): PlayerInfo {
|
||||
return {
|
||||
id: this.id,
|
||||
@@ -192,6 +207,7 @@ export class ServerPlayer extends LivingEntity {
|
||||
skin: this.skin,
|
||||
slim: this.slim || undefined,
|
||||
...(this.worn_list().length ? { worn: this.worn_list() } : {}),
|
||||
...(this.swimming && { swimming: true }),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user