Move away from browser as a main plataform
This commit is contained in:
+29
-5
@@ -6,7 +6,7 @@ import { Client } from "./client.ts";
|
||||
import { InputManager } from "./input_manager.ts";
|
||||
import { Connection } from "./network.ts";
|
||||
import { Options } from "./options.ts";
|
||||
import { connect, HandshakeError, is_trusted, join, remember_trust, ServerAddress } from "./handshake.ts";
|
||||
import { connect, HandshakeError, is_trusted, join, remember_trust, ServerAddress, type Welcome } from "./handshake.ts";
|
||||
import { build_atlas, engine_textures } from "./atlas.ts";
|
||||
import { set_texture_animations } from "./texture_animations.ts";
|
||||
import type { TextureJson } from "@/common/texture_animation.ts";
|
||||
@@ -18,6 +18,7 @@ import type { GuiScreen } from "./gui/gui_screen.ts";
|
||||
import { in_desktop_app, type JoinServer, TitleScreen } from "./gui/title_screen.ts";
|
||||
import { MainMenuScreen } from "./gui/main_menu.ts";
|
||||
import { DisconnectedScreen } from "./gui/disconnected_screen.ts";
|
||||
import { ConfirmModsScreen } from "./gui/confirm_mods_screen.ts";
|
||||
import { profiler } from "./profiler.ts";
|
||||
import { platform } from "./platform.ts";
|
||||
|
||||
@@ -27,6 +28,8 @@ class ClientLoop {
|
||||
last_time = 0;
|
||||
client: Client | undefined;
|
||||
screen: GuiScreen | undefined;
|
||||
// over the screen, it gets the input while it's there
|
||||
overlay: GuiScreen | undefined;
|
||||
|
||||
frame_count = 0;
|
||||
last_fps_time = 0;
|
||||
@@ -56,6 +59,16 @@ class ClientLoop {
|
||||
InputManager.set_mouse_grabbed(false);
|
||||
}
|
||||
|
||||
// shown over whatever screen is joining, see ConfirmModsScreen
|
||||
confirm_mods(address: ServerAddress, welcome: Welcome): Promise<boolean> {
|
||||
return new Promise((resolve) => {
|
||||
this.overlay = new ConfirmModsScreen(address, welcome, (join) => {
|
||||
this.overlay = undefined;
|
||||
resolve(join);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
play(connection: Connection) {
|
||||
const client = new Client(connection, (message) => this.show_screen(new DisconnectedScreen(message)));
|
||||
set_mods_client(client);
|
||||
@@ -80,8 +93,9 @@ class ClientLoop {
|
||||
if (this.client) {
|
||||
this.client.run_frame(delta);
|
||||
} else if (this.screen) {
|
||||
this.screen.on_tick(delta);
|
||||
(this.overlay ?? this.screen).on_tick(delta);
|
||||
this.screen.on_render();
|
||||
this.overlay?.on_render();
|
||||
}
|
||||
profiler.section("submit", end_drawing);
|
||||
profiler.end_frame();
|
||||
@@ -107,7 +121,12 @@ class ClientLoop {
|
||||
class FailedAfterLoadingMods extends Error {}
|
||||
|
||||
// the game only runs against a server, it owns the world and everything in it. see docs/modding/networking/index.md
|
||||
async function join_server(address: ServerAddress, name: string, status: (text: string) => void): Promise<Connection> {
|
||||
async function join_server(
|
||||
address: ServerAddress,
|
||||
name: string,
|
||||
status: (text: string) => void,
|
||||
confirm_mods: (address: ServerAddress, welcome: Welcome) => Promise<boolean>,
|
||||
): Promise<Connection> {
|
||||
const { socket, welcome } = await connect(address, name);
|
||||
console.log(`Connected to ${address.ws_url}`);
|
||||
|
||||
@@ -115,7 +134,7 @@ async function join_server(address: ServerAddress, name: string, status: (text:
|
||||
try {
|
||||
if (address.cross_origin && !is_trusted(address, welcome)) {
|
||||
status("Waiting for you to accept the server's mods...");
|
||||
if (!await platform.confirm_mods(address, welcome)) {
|
||||
if (!await confirm_mods(address, welcome)) {
|
||||
throw new HandshakeError(`You didn't join ${address.base.host}`);
|
||||
}
|
||||
remember_trust(address, welcome);
|
||||
@@ -178,7 +197,12 @@ export async function start_client() {
|
||||
const join_game: JoinServer = async (address, name, status) => {
|
||||
let connection: Connection;
|
||||
try {
|
||||
connection = await join_server(address, name, status);
|
||||
connection = await join_server(
|
||||
address,
|
||||
name,
|
||||
status,
|
||||
(address, welcome) => loop.confirm_mods(address, welcome),
|
||||
);
|
||||
} catch (e) {
|
||||
if (e instanceof FailedAfterLoadingMods) {
|
||||
loop.show_screen(new DisconnectedScreen(e.message));
|
||||
|
||||
Reference in New Issue
Block a user