UX: auto-start emulator, stop button, cleanup on navigate away

- Emulator auto-starts when Play page loads (no intermediate launch screen)
- Added ⏹ Stop button while game is running
- Back arrow and Stop both kill the emulator (js-dos CI.exit())
- Cleanup on unmount prevents background emulation
- Loading state shown while js-dos CDN loads
- Manual "Start Game" fallback if auto-start fails
This commit is contained in:
David Alvarez
2026-05-23 18:58:52 +02:00
parent 2b82404cf8
commit 338ec870f5
2 changed files with 167 additions and 59 deletions
+167 -59
View File
@@ -7,9 +7,12 @@
let game = $state(null);
let loading = $state(true);
let error = $state("");
let emulatorReady = $state(false);
let booting = $state(false);
let running = $state(false);
let started = false;
let dosContainer;
let dosCI = null;
async function load() {
loading = true;
@@ -21,34 +24,83 @@
loading = false;
}
function startEmulator() {
if (!game || !game.ready) return;
async function startEmulator() {
if (!dosContainer || !game?.ready || running || started) return;
started = true;
booting = true;
// Load js-dos from CDN dynamically
const css = document.createElement("link");
css.rel = "stylesheet";
css.href = "https://v8.js-dos.com/latest/js-dos.css";
document.head.appendChild(css);
// Load js-dos CSS
const link = document.createElement("link");
link.rel = "stylesheet";
link.href = "https://v8.js-dos.com/latest/js-dos.css";
document.head.appendChild(link);
const script = document.createElement("script");
script.src = "https://v8.js-dos.com/latest/js-dos.js";
script.onload = () => {
const url = bundleUrl(game);
if (window.Dos && dosContainer && url) {
window.Dos(dosContainer, { url });
emulatorReady = true;
// Load js-dos script from CDN if not already loaded
if (!window.Dos) {
try {
await new Promise((resolve, reject) => {
const s = document.createElement("script");
s.src = "https://v8.js-dos.com/latest/js-dos.js";
s.onload = resolve;
s.onerror = reject;
document.head.appendChild(s);
});
} catch {
error = "Failed to load emulator. Check your internet connection.";
booting = false;
return;
}
};
document.head.appendChild(script);
emulatorReady = true;
}
// Start the DOS emulator
try {
const url = bundleUrl(game);
if (!url || !window.Dos) {
throw new Error("Failed to resolve game bundle");
}
dosCI = await window.Dos(dosContainer, { url });
running = true;
} catch (e) {
error = "Failed to start emulator: " + e.message;
}
booting = false;
}
function stopEmulator() {
// Kill the emulator process
if (dosCI) {
try { dosCI.exit(); } catch (_) {}
dosCI = null;
}
if (dosContainer) {
dosContainer.innerHTML = "";
}
running = false;
// Navigate back to game detail
push(`/game/${game.id}`);
}
// Load game data on mount
$effect(() => { load(); });
// Cleanup on unmount
// Auto-start emulator once game data and DOM are ready
$effect(() => {
if (game?.ready && dosContainer && !started) {
startEmulator();
}
});
// Cleanup on unmount (navigating away, browser back, etc.)
$effect(() => {
return () => {
// js-dos handles its own cleanup
if (dosCI) {
try { dosCI.exit(); } catch (_) {}
dosCI = null;
}
if (dosContainer) {
dosContainer.innerHTML = "";
}
running = false;
};
});
</script>
@@ -56,31 +108,47 @@
{#if loading}
<div class="loading">Loading...</div>
{:else if error}
<div class="error">{error}</div>
<a href="#/" class="back-link">← Back to Library</a>
<div class="play-page">
<a href="#/" class="back-link">← Back to Library</a>
<div class="error-msg">{error}</div>
</div>
{:else if game}
<div class="play-page">
<!-- Top bar -->
<div class="play-header">
<a href={`#/game/${game.id}`} class="back-link"> {game.title}</a>
{#if !emulatorReady}
<button class="btn btn-primary" onclick={startEmulator}>
▶ Launch Emulator
</button>
<button class="link-button" onclick={stopEmulator}> {game.title}</button>
{#if running}
<button class="btn stop-btn" onclick={stopEmulator}> Stop</button>
{/if}
</div>
{#if emulatorReady}
<!-- Booting state -->
{#if booting && !running}
<div class="booting">
<div class="booting-icon">⚙️</div>
<h2>{game.title}</h2>
<p>Starting emulator...</p>
<p class="save-hint">💾 Game saves are stored automatically in your browser</p>
</div>
{/if}
<!-- Emulator canvas -->
{#if running}
<div class="emulator-wrapper">
<div bind:this={dosContainer} class="dos-container"></div>
</div>
{:else}
<div class="launch-screen">
<div class="launch-icon">🕹️</div>
{/if}
<!-- Empty state (shouldn't normally show) -->
{#if !booting && !running}
<div class="booting">
<div class="booting-icon">🕹️</div>
<h2>{game.title}</h2>
<p class="launch-info">
{game.year ? `${game.year} · ` : ""}{game.genre || ""}
</p>
<p class="launch-hint">Click "Launch Emulator" above to start playing</p>
{#if game.ready}
<button class="btn btn-primary" onclick={startEmulator}> Start Game</button>
{:else}
<p>This game is not ready yet.</p>
{/if}
<p class="save-hint">💾 Game saves are stored automatically in your browser</p>
</div>
{/if}
@@ -89,16 +157,75 @@
<style>
.play-page { padding: 16px 0; }
.loading { text-align: center; padding: 60px; color: var(--text-dim); }
.error { background: #331111; border: 1px solid #cc3333; color: #ff6666; padding: 12px; border-radius: var(--radius-sm); margin-bottom: 16px; }
.back-link { display: inline-block; color: var(--text-dim); }
.loading {
text-align: center;
padding: 60px;
color: var(--text-dim);
}
.error-msg {
background: #331111;
border: 1px solid #cc3333;
color: #ff6666;
padding: 12px 16px;
border-radius: var(--radius-sm);
margin-top: 16px;
}
.back-link {
display: inline-block;
color: var(--text-dim);
font-size: 0.9rem;
}
.back-link:hover { color: var(--phosphor); }
.link-button {
background: none;
border: none;
color: var(--text-dim);
font-size: 0.9rem;
cursor: pointer;
padding: 0;
font-family: var(--font-sans);
}
.link-button:hover { color: var(--phosphor); }
.play-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 16px;
margin-bottom: 12px;
}
.stop-btn {
border-color: #cc3333;
color: #ff4444;
}
.stop-btn:hover {
background: #331111;
box-shadow: 0 0 12px #331111;
}
/* Booting / transitional state */
.booting {
text-align: center;
padding: 60px 20px;
border: 1px dashed var(--border);
border-radius: var(--radius);
}
.booting-icon { font-size: 3rem; margin-bottom: 16px; }
.booting h2 {
color: var(--phosphor);
font-family: var(--font-mono);
margin-bottom: 8px;
}
.booting p { color: var(--text-dim); }
.save-hint {
display: inline-block;
margin-top: 16px;
padding: 6px 16px;
border-radius: 12px;
background: var(--phosphor-burn);
color: var(--phosphor-dim);
font-size: 0.85rem;
}
/* Emulator */
.emulator-wrapper {
background: #000;
border-radius: var(--radius);
@@ -114,23 +241,4 @@
width: 100% !important;
height: 100% !important;
}
.launch-screen {
text-align: center;
padding: 80px 20px;
border: 1px dashed var(--border);
border-radius: var(--radius);
}
.launch-icon { font-size: 4rem; margin-bottom: 16px; }
.launch-screen h2 { color: var(--phosphor); font-family: var(--font-mono); margin-bottom: 8px; }
.launch-info { color: var(--text-dim); margin-bottom: 24px; }
.launch-hint { color: var(--text-dim); margin-bottom: 8px; }
.save-hint {
color: var(--phosphor-dim);
font-size: 0.85rem;
margin-top: 16px;
background: var(--phosphor-burn);
display: inline-block;
padding: 6px 16px;
border-radius: 12px;
}
</style>
</style>