From 5605b314e5592fa88b24053a604bfe870f980f52 Mon Sep 17 00:00:00 2001 From: Half-Shot Date: Wed, 19 Feb 2025 23:27:13 +0000 Subject: [PATCH] [feat] Loading menus now appear on startup and on game load. --- src/frontend/components/atoms/loading.tsx | 11 +++++-- src/frontend/components/ingame-view.tsx | 37 ++++++++++++++++------- src/frontend/components/loading-page.tsx | 17 ++++++++--- src/frontend/components/menu.tsx | 8 ++--- src/frontend/components/menus/lobby.tsx | 1 - src/game.ts | 5 ++- 6 files changed, 56 insertions(+), 23 deletions(-) diff --git a/src/frontend/components/atoms/loading.tsx b/src/frontend/components/atoms/loading.tsx index 2abe108..8078721 100644 --- a/src/frontend/components/atoms/loading.tsx +++ b/src/frontend/components/atoms/loading.tsx @@ -8,7 +8,7 @@ export function Loading({ className, loadingDone, }: { - progress: number; + progress?: number; className?: string; loadingDone: () => void; }) { @@ -27,6 +27,11 @@ export function Loading({ if (!videoRef.current) { return; } + if (progress === undefined) { + videoRef.current.playbackRate = 2; + videoRef.current.play(); + return; + } const expectedProgress = VIDEO_TIME_S * progress; const currentTime = videoRef.current.currentTime; if (expectedProgress > currentTime) { @@ -38,14 +43,16 @@ export function Loading({ } }, [videoRef, progress]); + // Always play muted, because it prevents browsers blocking the animation. return ( + /> ); } diff --git a/src/frontend/components/ingame-view.tsx b/src/frontend/components/ingame-view.tsx index c4cd63b..d870813 100644 --- a/src/frontend/components/ingame-view.tsx +++ b/src/frontend/components/ingame-view.tsx @@ -3,7 +3,8 @@ import styles from "./ingame-view.module.css"; import { Game } from "../../game"; import { AmmoCount, GameReactChannel } from "../../interop/gamechannel"; import { WeaponSelector } from "./gameui/weapon-select"; -import { IRunningGameInstance } from "../../logic/gameinstance"; +import { IRunningGameInstance, LocalGameInstance } from "../../logic/gameinstance"; +import { LoadingPage } from "./loading-page"; export function IngameView({ scenario, @@ -16,21 +17,35 @@ export function IngameView({ gameReactChannel: GameReactChannel; gameInstance: IRunningGameInstance; }) { + const [hasLoaded, setLoaded] = useState(false); const [fatalError, setFatalError] = useState(); const [game, setGame] = useState(); const ref = useRef(null); const [weaponMenu, setWeaponMenu] = useState(null); useEffect(() => { - Game.create(window, scenario, gameReactChannel, gameInstance, level) - .then((game) => { - (window as unknown as { wormgine: Game }).wormgine = game; - game.loadResources().then(() => { - setGame(game); - }); - }) - .catch((ex) => { - setFatalError(ex); + + async function init() { + if (gameInstance instanceof LocalGameInstance) { + // XXX: Only so the game feels more responsive by capturing this inside the loading phase. + await gameInstance.startGame(); + console.log("Game started"); + } + const game = await Game.create(window, scenario, gameReactChannel, gameInstance, level); + setGame(game); + game.ready$.subscribe(r => { + if (r) { + console.log("Game loaded"); + setLoaded(true); + } }); + // Bind the game to the window such that we can debug it. + (globalThis as unknown as { wormgine: Game }).wormgine = game; + await game.loadResources(); + } + + void init().catch((ex) => { + setFatalError(ex); + }); }, []); useEffect(() => { @@ -42,7 +57,6 @@ export function IngameView({ return; } - // Bind the game to the window such that we can debug it. ref.current.appendChild(game.canvas); game.run().catch((ex) => { setFatalError(ex); @@ -85,6 +99,7 @@ export function IngameView({ return ( <> +
{ if (!scope.current) { @@ -24,17 +29,21 @@ export function LoadingPage({ { opacity: 1 }, { delay: 0, duration: 0.25, ease: "easeIn" }, ); - } else if (isLoadingDone) { + } else if (isLoadingDone || force) { await animate( scope.current, { opacity: 0 }, { delay: 0.5, duration: 0.5, ease: "easeIn" }, ); - console.log("safetoremove"); + setShouldOverlay(false); } } void runAnim(); - }, [visible, isLoadingDone, scope.current]); + }, [visible, force, isLoadingDone, scope.current]); + + if (!shouldOverlay) { + return null; + } return ( <> diff --git a/src/frontend/components/menu.tsx b/src/frontend/components/menu.tsx index 70a3456..b5c692e 100644 --- a/src/frontend/components/menu.tsx +++ b/src/frontend/components/menu.tsx @@ -94,7 +94,7 @@ function mainMenu( Skirmish -