// 玩家資料 let playerData = []; // 載入CSV檔案 async function loadPlayerData() { try { const response = await fetch('ID.csv'); const data = await response.text(); // 解析CSV資料 const rows = data.split('\n'); const headers = rows[0].split(','); // 跳過標題行,處理每一行資料 for (let i = 1; i < rows.length; i++) { if (rows[i].trim() === '') continue; const values = rows[i].split(','); const player = { id: values[0].trim(), name: values[1].trim(), profession: values[2].trim() }; playerData.push(player); } console.log('玩家資料載入成功:', playerData); } catch (error) { console.error('載入玩家資料失敗:', error); } } // 支援跨裝置存檔的功能 function generateSaveCode(playerId) { const progress = localStorage.getItem(`gameProgress_${playerId}`); if (progress) { // 將遊戲進度轉為Base64編碼 return btoa(JSON.stringify({ id: playerId, progress: JSON.parse(progress) })); } return null; } function loadFromSaveCode(saveCode) { try { // 解碼並解析存檔數據 const data = JSON.parse(atob(saveCode)); const playerId = data.id; const progress = data.progress; // 將進度存入localStorage localStorage.setItem(`gameProgress_${playerId}`, JSON.stringify(progress)); localStorage.setItem('currentPlayerId', playerId); return { success: true, playerId: playerId }; } catch (error) { console.error('載入存檔失敗:', error); return { success: false, error: '存檔格式錯誤' }; } } // 頁面載入完成後的初始化 document.addEventListener('DOMContentLoaded', async function() { console.log('DOM 載入完成,開始初始化...'); // 載入玩家資料 await loadPlayerData(); // 獲取所有按鈕元素 const startGameBtn = document.getElementById('start-btn'); const loadGameBtn = document.getElementById('load-btn'); const achievementsBtn = document.getElementById('achievements-btn'); const exitGameBtn = document.getElementById('exit-btn'); const toggleBgmBtn = document.getElementById('toggle-bgm'); // 調試:檢查按鈕是否正確獲取 console.log('開始遊戲按鈕:', startGameBtn); console.log('讀取進度按鈕:', loadGameBtn); console.log('成就圖鑑按鈕:', achievementsBtn); console.log('結束遊戲按鈕:', exitGameBtn); console.log('音樂開關按鈕:', toggleBgmBtn); // 獲取模態框元素 const playerModal = document.getElementById('id-modal'); const loadModal = document.getElementById('welcome-modal'); const closeModalBtns = document.querySelectorAll('.close-btn, .close-welcome-btn'); // 調試:檢查模態框是否正確獲取 console.log('玩家選擇模態框:', playerModal); console.log('讀取進度模態框:', loadModal); console.log('關閉按鈕:', closeModalBtns); // 開始遊戲按鈕 if (startGameBtn) { startGameBtn.addEventListener('click', function() { console.log('點擊開始遊戲按鈕'); if (playerModal) { playerModal.style.display = 'block'; // 直接顯示 ID 輸入框,而不是玩家列表 } else { console.error('找不到玩家選擇模態框'); } }); } else { console.error('找不到開始遊戲按鈕'); } // 讀取進度按鈕 if (loadGameBtn) { loadGameBtn.addEventListener('click', function() { console.log('點擊讀取進度按鈕'); // 檢查是否有當前玩家ID const currentPlayerId = localStorage.getItem('currentPlayerId'); if (currentPlayerId) { // 檢查是否有該玩家的存檔 const existingProgress = localStorage.getItem(`gameProgress_${currentPlayerId}`); if (existingProgress) { if (confirm(`發現玩家 ${currentPlayerId} 的存檔,是否載入進度?`)) { window.location.href = 'kingdom_map.html'; } } else { alert(`玩家 ${currentPlayerId} 沒有存檔記錄`); } } else { alert('請先開始遊戲或輸入玩家ID'); } }); } else { console.error('找不到讀取進度按鈕'); } // 成就圖鑑按鈕 if (achievementsBtn) { achievementsBtn.addEventListener('click', function() { console.log('點擊成就圖鑑按鈕'); window.location.href = 'achievements.html'; }); } else { console.error('找不到成就圖鑑按鈕'); } // 結束遊戲按鈕 if (exitGameBtn) { exitGameBtn.addEventListener('click', function() { console.log('點擊結束遊戲按鈕'); if (confirm('確定要結束遊戲嗎?')) { window.close(); } }); } else { console.error('找不到結束遊戲按鈕'); } // 關閉模態框 closeModalBtns.forEach(function(btn) { btn.addEventListener('click', function() { console.log('點擊關閉模態框按鈕'); const modal = btn.closest('.modal'); if (modal) { modal.style.display = 'none'; } }); }); // 點擊模態框外部關閉 window.addEventListener('click', function(event) { if (event.target.classList.contains('modal')) { event.target.style.display = 'none'; } }); // 處理 ID 輸入確認 const confirmIdBtn = document.getElementById('confirm-id'); const playerIdInput = document.getElementById('player-id'); const idError = document.getElementById('id-error'); if (confirmIdBtn && playerIdInput) { confirmIdBtn.addEventListener('click', function() { const playerId = playerIdInput.value.trim(); if (!playerId) { if (idError) idError.textContent = '請輸入ID'; return; } // 設置玩家資料 localStorage.setItem('currentPlayerId', playerId); localStorage.setItem('currentPlayerName', playerId); // 暫時使用 ID 作為名稱 // 檢查是否有存檔 const existingProgress = localStorage.getItem(`gameProgress_${playerId}`); if (existingProgress) { if (confirm(`發現 ${playerId} 的存檔,是否繼續之前的進度?`)) { window.location.href = 'kingdom_map.html'; } else { // 重新開始遊戲 localStorage.removeItem(`gameProgress_${playerId}`); window.location.href = 'prologue.html'; } } else { // 新遊戲 window.location.href = 'prologue.html'; } }); } });