Files
bworld/server/game/player.ts
T

123 lines
3.1 KiB
TypeScript

import { Container, Cursor, ItemData, ItemStack } from "$/common/inventory.ts";
import { PlayerInfo, ScreenLayout } from "$/common/protocol.ts";
import { type SavedVitals, Vitals } from "./vitals.ts";
import { PlayerChunks } from "./player_chunks.ts";
export const INVENTORY_SIZE = 9 * 4;
export const CRAFTING_SIZE = 10;
// a container screen a mod opened for this player, see ctx.ui.open_container
export interface OpenScreen {
container: Container;
layout: ScreenLayout;
// what the bars show, synced when they change
properties: Record<string, number>;
// what can go into each slot, checked when the player clicks
filters: Map<number, (item: ItemStack) => boolean>;
// asked before a player takes from an output slot
on_take: Map<number, (item: ItemStack) => boolean>;
on_close: (() => void)[];
}
// what's saved about a player between sessions, by name
export interface SavedPlayer {
x: number;
y: number;
z: number;
yaw: number;
pitch: number;
selected_slot: number;
inventory: (ItemData | null)[];
// players saved before health and hunger start full
vitals?: SavedVitals;
// base64 png, see /skin
skin?: string;
slim?: boolean;
}
export class ServerPlayer {
readonly conn: number;
readonly id = crypto.randomUUID();
readonly name: string;
x = 0;
y = 100;
z = 0;
yaw = 0;
pitch = 0;
inventory = new Container(INVENTORY_SIZE);
vitals = new Vitals();
// blocks fallen since last standing on something, for fall damage
fall_distance = 0;
// when the last move came, to tell walking from sprinting
last_move_tick = 0;
crafting = new Container(CRAFTING_SIZE);
cursor: Cursor = { item: undefined };
selected_slot = 0;
screen: OpenScreen | undefined;
// base64 png, see /skin. undefined is the default skin
skin: string | undefined;
// the skin is for the model with 3 pixel wide arms
slim = false;
// a /skin download is on its way, one at a time
downloading_skin = false;
// last json sent for each synced thing, so only changes get sent
sent = new Map<string, string>();
// the chunks their client has and still needs
chunks: PlayerChunks;
constructor(conn: number, name: string, view_distance: number) {
this.conn = conn;
this.name = name;
this.chunks = new PlayerChunks(view_distance);
}
get held_item(): ItemStack | undefined {
return this.inventory.get_item(this.selected_slot);
}
info(): PlayerInfo {
return {
id: this.id,
name: this.name,
x: this.x,
y: this.y,
z: this.z,
yaw: this.yaw,
pitch: this.pitch,
skin: this.skin,
slim: this.slim || undefined,
};
}
save(): SavedPlayer {
return {
x: this.x,
y: this.y,
z: this.z,
yaw: this.yaw,
pitch: this.pitch,
selected_slot: this.selected_slot,
inventory: this.inventory.to_data(),
vitals: this.vitals.save(),
skin: this.skin,
slim: this.slim || undefined,
};
}
load(saved: SavedPlayer) {
this.x = saved.x;
this.y = saved.y;
this.z = saved.z;
this.yaw = saved.yaw;
this.pitch = saved.pitch;
this.selected_slot = saved.selected_slot;
this.inventory.load(saved.inventory);
this.vitals.load(saved.vitals);
this.skin = saved.skin;
this.slim = saved.slim ?? false;
}
}