Main menu
This commit is contained in:
+33
-10
@@ -14,8 +14,8 @@ 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 { canvas, resize_canvas } from "./renderer/mod.ts";
|
||||
import { show_fatal_error } from "./fatal.ts";
|
||||
import { PauseScreen } from "./gui/pause_screen.ts";
|
||||
import { back_to_title } from "./gui/title_screen.ts";
|
||||
import { TICK_DELTA } from "$/common/constants.ts";
|
||||
|
||||
// after a long stall (a hidden tab, a debugger) the game skips ahead instead of running every missed tick at once
|
||||
@@ -45,13 +45,12 @@ export class Client {
|
||||
// whether the attack button is held on a block, breaking it advances every tick
|
||||
#attacking = false;
|
||||
#stopped = false;
|
||||
// called once when the connection drops, with why
|
||||
#on_disconnect: (message: string) => void;
|
||||
|
||||
constructor(connection: Connection) {
|
||||
constructor(connection: Connection, on_disconnect: (message: string) => void) {
|
||||
this.connection = connection;
|
||||
|
||||
self.addEventListener("resize", resize_canvas);
|
||||
resize_canvas();
|
||||
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
|
||||
this.#on_disconnect = on_disconnect;
|
||||
|
||||
this.level = new ClientLevel(connection.seed);
|
||||
for (const [x, y, z, id, state] of connection.initial_changes) {
|
||||
@@ -88,8 +87,24 @@ export class Client {
|
||||
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;
|
||||
}
|
||||
this.screen?.on_tick(delta);
|
||||
this.#handle_keybinds();
|
||||
|
||||
@@ -114,8 +129,8 @@ export class Client {
|
||||
tick() {
|
||||
this.packet_listener.handle_packets();
|
||||
if (this.connection.closed) {
|
||||
show_fatal_error("Lost connection to the server");
|
||||
this.#stopped = true;
|
||||
this.#stop();
|
||||
this.#on_disconnect("Lost connection to the server");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -144,8 +159,16 @@ export class Client {
|
||||
if (InputManager.is_key_pressed(options.key_chat) && !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")) {
|
||||
this.pop_screen();
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user