338 lines
11 KiB
TypeScript
338 lines
11 KiB
TypeScript
import { ClientLevel } from "./level/client_level.ts";
|
|
import type { BlockHitResult } from "./level/client_level.ts";
|
|
import { LocalPlayer } from "./entity/local_player.ts";
|
|
import { RemotePlayer } from "./entity/remote_player.ts";
|
|
import { ItemEntity } from "./entity/item_entity.ts";
|
|
import { RemoteEntity } from "./entity/remote_entity.ts";
|
|
import type { Entity } from "./entity/entity.ts";
|
|
import { Camera } from "./camera.ts";
|
|
import { Options } from "./options.ts";
|
|
import { MultiPlayerGameMode } from "./game_mode.ts";
|
|
import { ClientPacketListener } from "./packet_listener.ts";
|
|
import { GameRenderer } from "./rendering/game_renderer.ts";
|
|
import { ChatComponent } from "./gui/chat_component.ts";
|
|
import { GuiInventoryScreen, GuiScreen } from "./gui/gui_screen.ts";
|
|
import { GuiPlayerInventory } from "./gui/gui_player_inventory.ts";
|
|
import { GuiChat } from "./gui/gui_chat.ts";
|
|
import { InputManager } from "./input_manager.ts";
|
|
import { Connection } from "./network.ts";
|
|
import { PauseScreen } from "./gui/pause_screen.ts";
|
|
import { back_to_title } from "./gui/title_screen.ts";
|
|
import { TICK_DELTA } from "$/common/constants.ts";
|
|
import { EverythingRegistry } from "$/common/everything_registry.ts";
|
|
import type { EntityJson } from "$/common/mod_data.ts";
|
|
|
|
// how far from the eyes entities can be hit, like minecraft
|
|
const ENTITY_REACH = 3;
|
|
// how far from the eyes blocks can be broken and used
|
|
const BLOCK_REACH = 6;
|
|
|
|
// after a long stall (a hidden tab, a debugger) the game skips ahead instead of running every missed tick at once
|
|
const MAX_TICKS_PER_FRAME = 10;
|
|
|
|
// the game client, like minecraft's Minecraft class: owns the level, the player, the screens and the renderer.
|
|
// the game runs in fixed ticks (TICKS_PER_SECOND), input and drawing happen every frame
|
|
export class Client {
|
|
connection: Connection;
|
|
options = new Options();
|
|
level: ClientLevel;
|
|
player: LocalPlayer;
|
|
camera = new Camera();
|
|
game_mode: MultiPlayerGameMode;
|
|
packet_listener: ClientPacketListener;
|
|
game_renderer = new GameRenderer();
|
|
chat = new ChatComponent();
|
|
|
|
// open screens, the last one is on top and gets the input
|
|
screens: GuiScreen[] = [];
|
|
// what the player is looking at, updated every frame
|
|
hit_result: BlockHitResult | undefined;
|
|
// the entity or other player being looked at, closer than any block. hit_result is undefined then
|
|
entity_hit: Entity | undefined;
|
|
debugging = false;
|
|
|
|
// seconds of game time not ticked yet
|
|
#pending_time = 0;
|
|
// whether the attack button is held on a block, breaking it advances every tick
|
|
#attacking = false;
|
|
#stopped = false;
|
|
// the render distance the server was last told, it sends chunks for it
|
|
#sent_view_distance: number;
|
|
// called once when the connection drops, with why
|
|
#on_disconnect: (message: string) => void;
|
|
|
|
constructor(connection: Connection, on_disconnect: (message: string) => void) {
|
|
this.connection = connection;
|
|
this.#on_disconnect = on_disconnect;
|
|
|
|
this.level = new ClientLevel(connection.server_blocks, this.options.render_distance);
|
|
this.level.time = connection.time;
|
|
this.#sent_view_distance = this.options.render_distance;
|
|
|
|
const spawn = connection.spawn;
|
|
this.player = new LocalPlayer(this, connection.id, connection.name);
|
|
this.player.set_position(spawn.x, spawn.y, spawn.z);
|
|
this.player.yaw = spawn.yaw;
|
|
this.player.pitch = spawn.pitch;
|
|
this.player.inventories.hotbar_selected = connection.selected_slot;
|
|
this.level.add_entity(this.player);
|
|
for (const info of connection.initial_players) {
|
|
this.level.add_entity(new RemotePlayer(this.level, info));
|
|
}
|
|
for (const info of connection.initial_entities) {
|
|
this.level.add_entity(
|
|
info.kind === "item" ? new ItemEntity(this.level, info) : new RemoteEntity(this.level, info),
|
|
);
|
|
}
|
|
|
|
this.game_mode = new MultiPlayerGameMode(this);
|
|
this.packet_listener = new ClientPacketListener(this);
|
|
}
|
|
|
|
get screen(): GuiScreen | undefined {
|
|
return this.screens.at(-1);
|
|
}
|
|
|
|
push_screen(screen: GuiScreen) {
|
|
this.screens.push(screen);
|
|
}
|
|
|
|
pop_screen() {
|
|
this.screens.pop()?.on_close();
|
|
}
|
|
|
|
// leaving on purpose, from the pause screen
|
|
disconnect() {
|
|
this.#stopped = true;
|
|
this.connection.close();
|
|
back_to_title();
|
|
}
|
|
|
|
#stop() {
|
|
this.#stopped = true;
|
|
this.level.dispose();
|
|
InputManager.set_mouse_grabbed(false);
|
|
}
|
|
|
|
// one frame: input, as many ticks as are due, then drawing between the last tick and the next
|
|
run_frame(delta: number) {
|
|
if (this.#stopped) {
|
|
return;
|
|
}
|
|
const screen = this.screen;
|
|
this.screen?.on_tick(delta);
|
|
// a screen that closed itself just now, like chat on enter, had this frame's keys
|
|
this.#handle_keybinds(screen !== undefined && this.screen === undefined);
|
|
|
|
this.#pending_time += delta;
|
|
let ticks = 0;
|
|
while (this.#pending_time >= TICK_DELTA && ticks < MAX_TICKS_PER_FRAME) {
|
|
this.tick();
|
|
if (this.#stopped) {
|
|
return;
|
|
}
|
|
this.#pending_time -= TICK_DELTA;
|
|
ticks += 1;
|
|
}
|
|
if (ticks === MAX_TICKS_PER_FRAME) {
|
|
this.#pending_time = Math.min(this.#pending_time, TICK_DELTA);
|
|
}
|
|
|
|
this.game_renderer.render(this, this.#pending_time / TICK_DELTA);
|
|
}
|
|
|
|
// one step of the game: what the server sent (chunks included), breaking, then every entity
|
|
tick() {
|
|
this.packet_listener.handle_packets();
|
|
if (this.connection.closed) {
|
|
this.#stop();
|
|
this.#on_disconnect("Lost connection to the server");
|
|
return;
|
|
}
|
|
|
|
if (this.#attacking && this.hit_result) {
|
|
this.game_mode.continue_destroy_block(this.hit_result);
|
|
}
|
|
|
|
const view_distance = Math.round(this.options.render_distance);
|
|
if (view_distance !== this.#sent_view_distance) {
|
|
this.#sent_view_distance = view_distance;
|
|
this.level.view_distance = view_distance;
|
|
this.connection.send({ type: "view_distance", distance: view_distance });
|
|
}
|
|
this.level.tick();
|
|
}
|
|
|
|
// closed_screen: a screen closed this frame and took its keys, so they don't also open the inventory or pick a
|
|
// hotbar slot. typing "/give pickaxe" and enter in the same slow frame would otherwise open the inventory
|
|
#handle_keybinds(closed_screen: boolean) {
|
|
const player = this.player;
|
|
|
|
// typing into a screen's text field, keys are letters, not keybinds
|
|
if (!closed_screen && !this.screen?.typing) {
|
|
this.#handle_keys();
|
|
}
|
|
|
|
if (InputManager.is_mouse_grabbed()) {
|
|
const mouse = InputManager.get_look_delta();
|
|
player.turn(mouse.x, mouse.y);
|
|
}
|
|
InputManager.set_mouse_grabbed(!this.screen);
|
|
|
|
this.#pick();
|
|
this.#handle_block_interaction();
|
|
|
|
if (!this.screen && !closed_screen) {
|
|
this.#handle_hotbar();
|
|
}
|
|
}
|
|
|
|
#handle_keys() {
|
|
const options = this.options;
|
|
const player = this.player;
|
|
|
|
if (InputManager.is_key_pressed(options.key_inventory)) {
|
|
if (!this.screen) {
|
|
this.push_screen(new GuiPlayerInventory(player.inventories, (m) => this.connection.send(m)));
|
|
} else if (this.screen instanceof GuiInventoryScreen) {
|
|
this.pop_screen();
|
|
}
|
|
}
|
|
if (InputManager.is_key_pressed(options.key_drop)) {
|
|
this.#handle_drop();
|
|
}
|
|
if (InputManager.is_key_pressed(options.key_chat) && !this.screen) {
|
|
this.push_screen(new GuiChat(this));
|
|
}
|
|
// straight into a command, like minecraft
|
|
if (InputManager.is_key_pressed("Slash") && !this.screen) {
|
|
this.push_screen(new GuiChat(this, "/"));
|
|
}
|
|
// browsers eat the escape that lets go of the mouse, so losing the mouse pauses too
|
|
const lost_mouse = InputManager.take_lost_pointer_lock();
|
|
if (InputManager.is_key_pressed("Escape")) {
|
|
if (this.screen) {
|
|
this.pop_screen();
|
|
} else {
|
|
this.push_screen(new PauseScreen(this));
|
|
}
|
|
} else if (lost_mouse && !this.screen) {
|
|
this.push_screen(new PauseScreen(this));
|
|
}
|
|
if (InputManager.is_key_pressed(options.key_debug)) {
|
|
this.debugging = !this.debugging;
|
|
}
|
|
if (InputManager.is_key_pressed(options.key_fullscreen)) {
|
|
InputManager.toggle_fullscreen();
|
|
}
|
|
}
|
|
|
|
// the held item while playing, the slot under the mouse in an inventory (only with nothing on the cursor)
|
|
#handle_drop() {
|
|
const all = InputManager.is_key_down("ControlLeft") || InputManager.is_key_down("ControlRight");
|
|
const inventories = this.player.inventories;
|
|
const screen = this.screen;
|
|
if (!screen) {
|
|
this.game_mode.drop_item(inventories.inventory, "inventory", inventories.hotbar_selected, all);
|
|
return;
|
|
}
|
|
const slot = screen instanceof GuiInventoryScreen ? screen.hovering : undefined;
|
|
const container = slot && screen instanceof GuiInventoryScreen
|
|
? screen.get_container(slot.container)
|
|
: undefined;
|
|
if (slot && container && !slot.output && !inventories.cursor.item) {
|
|
this.game_mode.drop_item(container, slot.container, slot.index, all);
|
|
}
|
|
}
|
|
|
|
// what's under the crosshair: an entity, or the first block in front of it
|
|
#pick() {
|
|
const player = this.player;
|
|
const eye_y = player.y + player.eye_height;
|
|
const picked = this.level.pick_entity(
|
|
player.x,
|
|
eye_y,
|
|
player.z,
|
|
player.yaw,
|
|
player.pitch,
|
|
ENTITY_REACH,
|
|
(entity) =>
|
|
entity instanceof RemotePlayer ||
|
|
(entity instanceof RemoteEntity && !entity.dead && is_hittable(entity)),
|
|
);
|
|
// a block in front of the entity hides it
|
|
this.hit_result = this.level.pick(
|
|
player.x,
|
|
eye_y,
|
|
player.z,
|
|
player.yaw,
|
|
player.pitch,
|
|
picked?.distance ?? BLOCK_REACH,
|
|
);
|
|
this.entity_hit = picked && !this.hit_result ? picked.entity : undefined;
|
|
}
|
|
|
|
// clicks happen right away, holding to break advances in tick()
|
|
#handle_block_interaction() {
|
|
const hit = this.hit_result;
|
|
const attacking = !this.screen && InputManager.is_mouse_down(0);
|
|
this.#attacking = attacking;
|
|
|
|
if (!attacking || !hit) {
|
|
this.game_mode.stop_destroy_block();
|
|
}
|
|
if (this.screen) {
|
|
return;
|
|
}
|
|
|
|
// the held item swings on every click, and keeps swinging while breaking, like minecraft
|
|
if (InputManager.is_mouse_pressed(0) || InputManager.is_mouse_pressed(2) || (attacking && hit)) {
|
|
this.game_renderer.hand.swing();
|
|
}
|
|
|
|
if (this.entity_hit && InputManager.is_mouse_pressed(0)) {
|
|
this.game_mode.attack(this.entity_hit);
|
|
}
|
|
if (hit) {
|
|
if (InputManager.is_mouse_pressed(0)) {
|
|
this.game_mode.start_destroy_block(hit);
|
|
}
|
|
if (!attacking && InputManager.is_mouse_pressed(2)) {
|
|
this.game_mode.use_item_on(hit);
|
|
}
|
|
} else if (InputManager.is_mouse_pressed(2)) {
|
|
this.game_mode.use_item();
|
|
} else if (!this.entity_hit && InputManager.is_mouse_pressed(0)) {
|
|
this.game_mode.swing();
|
|
}
|
|
}
|
|
|
|
#handle_hotbar() {
|
|
const inventories = this.player.inventories;
|
|
const previous = inventories.hotbar_selected;
|
|
|
|
const scroll = InputManager.get_wheel_delta();
|
|
if (scroll > 0) {
|
|
inventories.hotbar_selected = Math.min(8, inventories.hotbar_selected + 1);
|
|
} else if (scroll < 0) {
|
|
inventories.hotbar_selected = Math.max(0, inventories.hotbar_selected - 1);
|
|
}
|
|
|
|
const pressed = this.options.key_hotbar.findIndex((key) => InputManager.is_key_pressed(key));
|
|
if (pressed !== -1) {
|
|
inventories.hotbar_selected = pressed;
|
|
}
|
|
|
|
if (inventories.hotbar_selected !== previous) {
|
|
this.connection.send({ type: "select_slot", slot: inventories.hotbar_selected });
|
|
// the server starts the attack cooldown over too
|
|
this.player.last_attack = performance.now();
|
|
}
|
|
}
|
|
}
|
|
|
|
// mobs can be hit, scripted entities like the fishing hook can't. kinds the client doesn't know are left to the server
|
|
function is_hittable(entity: RemoteEntity) {
|
|
return EverythingRegistry.get<EntityJson>("entities", entity.type)?.behavior !== "scripted";
|
|
}
|