Awesome menu and saves
This commit is contained in:
+19
-48
@@ -23,8 +23,8 @@ const TITLE_SCALE = 8;
|
||||
// for the player when it fails
|
||||
export type JoinServer = (address: ServerAddress, name: string, status: (text: string) => void) => Promise<void>;
|
||||
|
||||
// the first thing players see, like minecraft's title and multiplayer screens rolled into one: a name, a server,
|
||||
// and a button to join it. in the desktop app there's also singleplayer, which joins the app's own server
|
||||
// the first thing players see in the browser, like minecraft's title and multiplayer screens rolled into one: a
|
||||
// name, a server, and a button to join it. the desktop app has a main menu instead, see main_menu.ts
|
||||
export class TitleScreen extends GuiScreen {
|
||||
#join_server: JoinServer;
|
||||
|
||||
@@ -36,15 +36,9 @@ export class TitleScreen extends GuiScreen {
|
||||
);
|
||||
server = new EditBox(WIDTH, ROW_HEIGHT, new TextInput(initial_server(), 256, (char) => char !== " "), "host:port");
|
||||
join_button = new Button("Join server", WIDTH, ROW_HEIGHT, () => this.#join());
|
||||
// the page was served by a game server of its own to play on alone, see desktop/main.ts
|
||||
singleplayer_button = has_singleplayer()
|
||||
? new Button("Singleplayer", WIDTH, ROW_HEIGHT, () => this.#join_singleplayer())
|
||||
: undefined;
|
||||
credits_button = new Button("Credits", WIDTH, ROW_HEIGHT, () => this.#open_credits());
|
||||
// in the browser, the desktop app for this computer's os
|
||||
download_button = has_singleplayer()
|
||||
? undefined
|
||||
: new Button(`Download for ${download_os().name}`, WIDTH, ROW_HEIGHT, download_desktop_app);
|
||||
// the desktop app for this computer's os
|
||||
download_button = new Button(`Download for ${download_os().name}`, WIDTH, ROW_HEIGHT, download_desktop_app);
|
||||
// open over the title screen, it gets the input while it's there
|
||||
#credits: CreditsScreen | undefined;
|
||||
|
||||
@@ -86,10 +80,9 @@ export class TitleScreen extends GuiScreen {
|
||||
if (InputManager.is_key_pressed("Enter")) {
|
||||
this.#join();
|
||||
}
|
||||
this.singleplayer_button?.handle_input();
|
||||
this.join_button.handle_input();
|
||||
this.credits_button.handle_input();
|
||||
this.download_button?.handle_input();
|
||||
this.download_button.handle_input();
|
||||
}
|
||||
|
||||
on_render(): void {
|
||||
@@ -105,16 +98,15 @@ export class TitleScreen extends GuiScreen {
|
||||
this.#label("Name", this.name);
|
||||
this.#label("Server", this.server);
|
||||
this.name.render();
|
||||
this.singleplayer_button?.render();
|
||||
this.server.render();
|
||||
this.join_button.render();
|
||||
this.credits_button.render();
|
||||
this.download_button?.render();
|
||||
this.download_button.render();
|
||||
|
||||
if (this.status) {
|
||||
const width = measure_text(this.status, TEXT_SCALE);
|
||||
const x = (canvas.width - width) / 2;
|
||||
const y = (this.download_button ?? this.credits_button).y + ROW_HEIGHT + GAP;
|
||||
const y = this.download_button.y + ROW_HEIGHT + GAP;
|
||||
draw_rect(x - 8, y - 4, width + 16, TEXT_HEIGHT * TEXT_SCALE + 8, [0, 0, 0, 0.5]);
|
||||
draw_text(this.status, x, y, TEXT_SCALE, this.status_is_error ? [1, 0.45, 0.45, 1] : [1, 1, 1, 1]);
|
||||
}
|
||||
@@ -136,28 +128,17 @@ export class TitleScreen extends GuiScreen {
|
||||
|
||||
#layout() {
|
||||
const x = (canvas.width - WIDTH) / 2;
|
||||
const singleplayer_height = this.singleplayer_button ? ROW_HEIGHT + GAP : 0;
|
||||
const download_height = this.download_button ? ROW_HEIGHT + GAP : 0;
|
||||
const total = 2 * (LABEL_GAP + ROW_HEIGHT + GAP) + singleplayer_height + 2 * ROW_HEIGHT + GAP +
|
||||
download_height;
|
||||
const total = 2 * (LABEL_GAP + ROW_HEIGHT + GAP) + 3 * ROW_HEIGHT + 2 * GAP;
|
||||
let y = (canvas.height - total) / 2 + 40;
|
||||
for (const field of [this.name, this.server]) {
|
||||
field.x = x;
|
||||
field.y = y + LABEL_GAP;
|
||||
y += LABEL_GAP + ROW_HEIGHT + GAP;
|
||||
if (field === this.name && this.singleplayer_button) {
|
||||
this.singleplayer_button.x = x;
|
||||
this.singleplayer_button.y = y;
|
||||
y += singleplayer_height;
|
||||
}
|
||||
}
|
||||
this.join_button.x = x;
|
||||
this.join_button.y = y;
|
||||
this.credits_button.x = x;
|
||||
this.credits_button.y = y + ROW_HEIGHT + GAP;
|
||||
if (this.download_button) {
|
||||
this.download_button.x = x;
|
||||
this.download_button.y = this.credits_button.y + ROW_HEIGHT + GAP;
|
||||
for (const button of [this.join_button, this.credits_button, this.download_button]) {
|
||||
button.x = x;
|
||||
button.y = y;
|
||||
y += ROW_HEIGHT + GAP;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -181,18 +162,10 @@ export class TitleScreen extends GuiScreen {
|
||||
await this.#connect(address);
|
||||
}
|
||||
|
||||
// the server that served this page
|
||||
async #join_singleplayer() {
|
||||
if (this.#joining) {
|
||||
return;
|
||||
}
|
||||
await this.#connect(server_address(location.host), "Starting singleplayer...");
|
||||
}
|
||||
|
||||
async #connect(address: ServerAddress, status = `Connecting to ${address.base.host}...`) {
|
||||
async #connect(address: ServerAddress) {
|
||||
remember(LAST_NAME_KEY, this.name.value);
|
||||
this.#set_joining(true);
|
||||
this.status = status;
|
||||
this.status = `Connecting to ${address.base.host}...`;
|
||||
this.status_is_error = false;
|
||||
try {
|
||||
await this.#join_server(address, this.name.value, (text) => this.status = text);
|
||||
@@ -205,9 +178,6 @@ export class TitleScreen extends GuiScreen {
|
||||
#set_joining(joining: boolean) {
|
||||
this.#joining = joining;
|
||||
this.name.active = this.server.active = this.join_button.active = !joining;
|
||||
if (this.singleplayer_button) {
|
||||
this.singleplayer_button.active = !joining;
|
||||
}
|
||||
}
|
||||
|
||||
#show_error(message: string) {
|
||||
@@ -243,7 +213,8 @@ function remember(key: string, value: string) {
|
||||
}
|
||||
}
|
||||
|
||||
function has_singleplayer() {
|
||||
// the page is the desktop app's, see desktop/main.ts
|
||||
export function in_desktop_app() {
|
||||
return new URL(location.href).searchParams.has("singleplayer");
|
||||
}
|
||||
|
||||
@@ -263,12 +234,12 @@ function download_desktop_app() {
|
||||
}
|
||||
|
||||
// a server's mods can't be unloaded, so going back to the title screen starts the page over. without ?server=
|
||||
// or ?name=, so the fields show what was used last. the desktop app keeps its ?singleplayer and ?server=, its saved
|
||||
// fields don't last between launches and the page's own address isn't a server to join
|
||||
// or ?name=, so the fields show what was used last. the desktop app keeps its ?singleplayer and ?server=, for its
|
||||
// main menu
|
||||
export function back_to_title() {
|
||||
const page = new URL(location.href);
|
||||
const next = new URL(location.pathname, location.href);
|
||||
if (has_singleplayer()) {
|
||||
if (in_desktop_app()) {
|
||||
next.searchParams.set("server", page.searchParams.get("server") ?? "");
|
||||
next.searchParams.set("singleplayer", "");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user