// Game State let wasmReady = false; let gameStarted = false; let audioMuted = false; // Initialize WASM async function initWASM() { const go = new Go(); try { const result = await WebAssembly.instantiateStreaming(fetch("main.wasm"), go.importObject); go.run(result.instance); wasmReady = true; // Hide loading screen document.getElementById('loading').style.display = 'none'; console.log('✅ WASM loaded successfully'); // Load initial leaderboard setTimeout(() => { if (window.requestLeaderboard) { window.requestLeaderboard(); } }, 500); } catch (err) { console.error('❌ Failed to load WASM:', err); document.getElementById('loading').innerHTML = '
Fehler beim Laden: ' + err.message + '
'; } } // Menu Navigation function showMainMenu() { hideAllScreens(); document.getElementById('menu').classList.remove('hidden'); } function showCoopMenu() { hideAllScreens(); document.getElementById('coopMenu').classList.remove('hidden'); } function showSettings() { hideAllScreens(); document.getElementById('settingsMenu').classList.remove('hidden'); } function showLeaderboard() { hideAllScreens(); document.getElementById('leaderboardMenu').classList.remove('hidden'); loadLeaderboard(); } function hideAllScreens() { document.querySelectorAll('.overlay-screen').forEach(screen => { screen.classList.add('hidden'); }); } function hideMenu() { document.getElementById('menu').style.display = 'none'; // Canvas sichtbar machen für Gameplay const canvas = document.querySelector('canvas'); if (canvas) { canvas.classList.add('game-active'); } } function showMenu() { document.getElementById('menu').style.display = 'flex'; document.getElementById('menu').classList.remove('hidden'); showMainMenu(); // Canvas verstecken im Menü const canvas = document.querySelector('canvas'); if (canvas) { canvas.classList.remove('game-active'); } } // Game Functions function startSoloGame() { if (!wasmReady) { alert('Spiel wird noch geladen...'); return; } const playerName = document.getElementById('playerName').value || 'ANON'; // Store in localStorage for WASM to read localStorage.setItem('escape_player_name', playerName); localStorage.setItem('escape_game_mode', 'solo'); localStorage.setItem('escape_room_id', ''); // Hide ALL screens including main menu hideAllScreens(); document.getElementById('menu').style.display = 'none'; gameStarted = true; // Canvas sichtbar machen const canvas = document.querySelector('canvas'); if (canvas) { canvas.classList.add('game-active'); } // Trigger WASM game start if (window.startGame) { window.startGame('solo', playerName, ''); } console.log('🎮 Solo game started:', playerName); } function createRoom() { if (!wasmReady) { alert('Spiel wird noch geladen...'); return; } const playerName = document.getElementById('playerName').value || 'ANON'; const roomID = 'R' + Math.random().toString(36).substr(2, 5).toUpperCase(); const teamName = 'TEAM'; // Store in localStorage localStorage.setItem('escape_player_name', playerName); localStorage.setItem('escape_game_mode', 'coop'); localStorage.setItem('escape_room_id', roomID); localStorage.setItem('escape_team_name', teamName); localStorage.setItem('escape_is_host', 'true'); // Verstecke ALLE Screens inkl. Hauptmenü hideAllScreens(); document.getElementById('menu').style.display = 'none'; // Zeige HTML Lobby Screen document.getElementById('lobbyScreen').classList.remove('hidden'); document.getElementById('lobbyRoomCode').textContent = roomID; document.getElementById('lobbyHostControls').classList.remove('hidden'); document.getElementById('lobbyStatus').textContent = 'Du bist Host - starte wenn bereit!'; // Trigger WASM game start (im Hintergrund) if (window.startGame) { window.startGame('coop', playerName, roomID, teamName, true); } console.log('🎮 Room created:', roomID); } function joinRoom() { if (!wasmReady) { alert('Spiel wird noch geladen...'); return; } const playerName = document.getElementById('playerName').value || 'ANON'; const roomID = document.getElementById('joinRoomCode').value.toUpperCase(); const teamName = document.getElementById('teamNameJoin').value || 'TEAM'; if (!roomID || roomID.length < 4) { alert('Bitte gib einen gültigen Raum-Code ein!'); return; } // Store in localStorage localStorage.setItem('escape_player_name', playerName); localStorage.setItem('escape_game_mode', 'coop'); localStorage.setItem('escape_room_id', roomID); localStorage.setItem('escape_team_name', teamName); localStorage.setItem('escape_is_host', 'false'); // Verstecke ALLE Screens inkl. Hauptmenü hideAllScreens(); document.getElementById('menu').style.display = 'none'; // Zeige HTML Lobby Screen document.getElementById('lobbyScreen').classList.remove('hidden'); document.getElementById('lobbyRoomCode').textContent = roomID; document.getElementById('lobbyHostControls').classList.add('hidden'); document.getElementById('lobbyStatus').textContent = 'Warte auf Host...'; // Trigger WASM game start (im Hintergrund) if (window.startGame) { window.startGame('coop', playerName, roomID, teamName, false); } console.log('🎮 Joining room:', roomID); } // Lobby Functions function startGameFromLobby() { // Host startet das Spiel hideAllScreens(); document.getElementById('menu').style.display = 'none'; gameStarted = true; // Canvas sichtbar machen const canvas = document.querySelector('canvas'); if (canvas) { canvas.classList.add('game-active'); } // Signal an WASM senden dass Spiel starten soll if (window.startGameFromLobby_WASM) { window.startGameFromLobby_WASM(); } console.log('🎮 Host started game from lobby'); } function leaveLobby() { location.reload(); } // Update Lobby Player List (called by WASM) function updateLobbyPlayers(players) { const list = document.getElementById('lobbyPlayerList'); if (!players || players.length === 0) { list.innerHTML = '