Spaces:
Running
Running
Upload 41 files
Browse files- achievements.js +10 -12
- script.js +180 -367
achievements.js
CHANGED
|
@@ -259,18 +259,18 @@ class AchievementSystem {
|
|
| 259 |
|
| 260 |
// 將成就添加到待處理佇列
|
| 261 |
addPendingAchievement(achievementId) {
|
| 262 |
-
let pendingAchievements = JSON.parse(localStorage.getItem(
|
| 263 |
if (!pendingAchievements.includes(achievementId)) {
|
| 264 |
pendingAchievements.push(achievementId);
|
| 265 |
-
localStorage.setItem(
|
| 266 |
console.log(`成就 ${achievementId} 已添加到待處理佇列`);
|
| 267 |
}
|
| 268 |
}
|
| 269 |
|
| 270 |
// 獲取並清除待處理成就
|
| 271 |
getAndClearPendingAchievements() {
|
| 272 |
-
const pendingAchievements = JSON.parse(localStorage.getItem(
|
| 273 |
-
localStorage.removeItem(
|
| 274 |
return pendingAchievements;
|
| 275 |
}
|
| 276 |
|
|
@@ -329,19 +329,19 @@ class AchievementSystem {
|
|
| 329 |
`;
|
| 330 |
|
| 331 |
// 播放音效
|
| 332 |
-
const audio = new Audio(
|
| 333 |
-
audio.play().catch(e => console.error(
|
| 334 |
|
| 335 |
// 添加到通知容器
|
| 336 |
this.toastContainer.appendChild(toast);
|
| 337 |
|
| 338 |
// 等待用戶點擊確認
|
| 339 |
await new Promise(resolve => {
|
| 340 |
-
const confirmButton = document.createElement(
|
| 341 |
-
confirmButton.textContent =
|
| 342 |
-
confirmButton.className =
|
| 343 |
toast.appendChild(confirmButton);
|
| 344 |
-
confirmButton.addEventListener(
|
| 345 |
});
|
| 346 |
toast.remove();
|
| 347 |
|
|
@@ -511,9 +511,7 @@ class AchievementSystem {
|
|
| 511 |
// 當頁面載入完成後初始化成就系統
|
| 512 |
document.addEventListener('DOMContentLoaded', () => {
|
| 513 |
// 創建成就系統實例
|
| 514 |
-
document.addEventListener(\'DOMContentLoaded\', function() {
|
| 515 |
window.achievementSystem = new AchievementSystem();
|
| 516 |
-
});
|
| 517 |
console.log('成就系統已掛載到全域 window 物件');
|
| 518 |
|
| 519 |
// 測試用:添加模擬解鎖按鈕
|
|
|
|
| 259 |
|
| 260 |
// 將成就添加到待處理佇列
|
| 261 |
addPendingAchievement(achievementId) {
|
| 262 |
+
let pendingAchievements = JSON.parse(localStorage.getItem('pendingAchievements')) || [];
|
| 263 |
if (!pendingAchievements.includes(achievementId)) {
|
| 264 |
pendingAchievements.push(achievementId);
|
| 265 |
+
localStorage.setItem('pendingAchievements', JSON.stringify(pendingAchievements));
|
| 266 |
console.log(`成就 ${achievementId} 已添加到待處理佇列`);
|
| 267 |
}
|
| 268 |
}
|
| 269 |
|
| 270 |
// 獲取並清除待處理成就
|
| 271 |
getAndClearPendingAchievements() {
|
| 272 |
+
const pendingAchievements = JSON.parse(localStorage.getItem('pendingAchievements')) || [];
|
| 273 |
+
localStorage.removeItem('pendingAchievements');
|
| 274 |
return pendingAchievements;
|
| 275 |
}
|
| 276 |
|
|
|
|
| 329 |
`;
|
| 330 |
|
| 331 |
// 播放音效
|
| 332 |
+
const audio = new Audio('ding.mp3');
|
| 333 |
+
audio.play().catch(e => console.error('播放音效失敗:', e));
|
| 334 |
|
| 335 |
// 添加到通知容器
|
| 336 |
this.toastContainer.appendChild(toast);
|
| 337 |
|
| 338 |
// 等待用戶點擊確認
|
| 339 |
await new Promise(resolve => {
|
| 340 |
+
const confirmButton = document.createElement('button');
|
| 341 |
+
confirmButton.textContent = '確認';
|
| 342 |
+
confirmButton.className = 'toast-confirm-button';
|
| 343 |
toast.appendChild(confirmButton);
|
| 344 |
+
confirmButton.addEventListener('click', resolve);
|
| 345 |
});
|
| 346 |
toast.remove();
|
| 347 |
|
|
|
|
| 511 |
// 當頁面載入完成後初始化成就系統
|
| 512 |
document.addEventListener('DOMContentLoaded', () => {
|
| 513 |
// 創建成就系統實例
|
|
|
|
| 514 |
window.achievementSystem = new AchievementSystem();
|
|
|
|
| 515 |
console.log('成就系統已掛載到全域 window 物件');
|
| 516 |
|
| 517 |
// 測試用:添加模擬解鎖按鈕
|
script.js
CHANGED
|
@@ -23,9 +23,9 @@ async function loadPlayerData() {
|
|
| 23 |
playerData.push(player);
|
| 24 |
}
|
| 25 |
|
| 26 |
-
console.log(
|
| 27 |
} catch (error) {
|
| 28 |
-
console.error(
|
| 29 |
}
|
| 30 |
}
|
| 31 |
|
|
@@ -49,396 +49,209 @@ function loadFromSaveCode(saveCode) {
|
|
| 49 |
const playerId = data.id;
|
| 50 |
const progress = data.progress;
|
| 51 |
|
| 52 |
-
//
|
| 53 |
-
const player = playerData.find(p => p.id === playerId);
|
| 54 |
-
if (!player) {
|
| 55 |
-
return { success: false, message: \'無效的玩家ID\' };
|
| 56 |
-
}
|
| 57 |
-
|
| 58 |
-
// 儲存進度
|
| 59 |
localStorage.setItem(`gameProgress_${playerId}`, JSON.stringify(progress));
|
| 60 |
-
localStorage.setItem(
|
| 61 |
-
localStorage.setItem(\'playerName\', player.name);
|
| 62 |
-
localStorage.setItem(\'playerProfession\', player.profession);
|
| 63 |
|
| 64 |
-
return { success: true,
|
| 65 |
-
} catch (
|
| 66 |
-
console.error(
|
| 67 |
-
return { success: false,
|
| 68 |
}
|
| 69 |
}
|
| 70 |
|
| 71 |
-
//
|
| 72 |
-
function
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
if (trialData.score > 0) {
|
| 112 |
-
gameState.visitedTrials.push(trialName);
|
| 113 |
-
|
| 114 |
-
gameState.completedTrials[trialName] = {
|
| 115 |
-
completed: trialData.completed,
|
| 116 |
-
stars: trialData.stars,
|
| 117 |
-
noErrors: trialData.noErrors
|
| 118 |
-
};
|
| 119 |
-
}
|
| 120 |
}
|
| 121 |
-
}
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
window.achievementSystem.checkAllAchievements(gameState);
|
| 125 |
}
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
// 解析CSV const rows = data.split("\n");
|
| 141 |
-
const headers = rows[0].split(",");
|
| 142 |
-
// 跳過標題行,處理每一行資料
|
| 143 |
-
for (let i = 1; i < rows.length; i++) {
|
| 144 |
-
if (rows[i].trim() === \'\') continue;
|
| 145 |
-
|
| 146 |
-
const values = rows[i].split(\'\\
|
| 147 |
-
\');
|
| 148 |
-
const player = {
|
| 149 |
-
id: values[0].trim(),
|
| 150 |
-
name: values[1].trim(),
|
| 151 |
-
profession: values[2].trim()
|
| 152 |
-
};
|
| 153 |
-
playerData.push(player);
|
| 154 |
-
}
|
| 155 |
-
|
| 156 |
-
console.log(\'玩家資料載入成功:\', playerData);
|
| 157 |
-
} catch (error) {
|
| 158 |
-
console.error(\'載入玩家資料失敗:\', error);
|
| 159 |
}
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
function
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
progress: JSON.parse(progress)
|
| 170 |
-
}));
|
| 171 |
}
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
function
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
const player = playerData.find(p => p.id === playerId);
|
| 184 |
-
if (!player) {
|
| 185 |
-
return { success: false, message: \'無效的玩家ID\' };
|
| 186 |
-
}
|
| 187 |
-
|
| 188 |
-
// 儲存進度
|
| 189 |
-
localStorage.setItem(`gameProgress_${playerId}`, JSON.stringify(progress));
|
| 190 |
-
localStorage.setItem(\'currentPlayerId\', playerId);
|
| 191 |
-
localStorage.setItem(\'playerName\', player.name);
|
| 192 |
-
localStorage.setItem(\'playerProfession\', player.profession);
|
| 193 |
-
|
| 194 |
-
return { success: true, player: player };
|
| 195 |
-
} catch (e) {
|
| 196 |
-
console.error(\"無效的存檔碼\", e);
|
| 197 |
-
return { success: false, message: \'無效的存檔碼格式\' };
|
| 198 |
}
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
function
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
-
|
| 209 |
-
if (!gameProgressStr) return;
|
| 210 |
|
| 211 |
-
|
|
|
|
|
|
|
|
|
|
| 212 |
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
|
|
|
|
|
|
|
|
|
| 218 |
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
// 填充試煉相關數據
|
| 239 |
-
if (gameProgress.completedTrials) {
|
| 240 |
-
for (const [trialName, trialData] of Object.entries(gameProgress.completedTrials)) {
|
| 241 |
-
if (trialData.score > 0) {
|
| 242 |
-
gameState.visitedTrials.push(trialName);
|
| 243 |
-
|
| 244 |
-
gameState.completedTrials[trialName] = {
|
| 245 |
-
completed: trialData.completed,
|
| 246 |
-
stars: trialData.stars,
|
| 247 |
-
noErrors: trialData.noErrors
|
| 248 |
-
};
|
| 249 |
}
|
| 250 |
-
}
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
// 檢查成就
|
| 254 |
-
window.achievementSystem.checkAllAchievements(gameState);
|
| 255 |
-
}
|
| 256 |
-
}
|
| 257 |
-
|
| 258 |
-
// 在頁面載入時檢查成就
|
| 259 |
-
document.addEventListener(\'DOMContentLoaded\', async function() {
|
| 260 |
-
await loadPlayerData();
|
| 261 |
-
|
| 262 |
-
// 獲取DOM元素
|
| 263 |
-
const startBtn = document.getElementById("start-btn");
|
| 264 |
-
console.log("startBtn:", startBtn);
|
| 265 |
-
const loadBtn = document.getElementById("load-btn");
|
| 266 |
-
console.log("loadBtn:", loadBtn);
|
| 267 |
-
const achievementsBtn = document.getElementById("achievements-btn");
|
| 268 |
-
console.log("achievementsBtn:", achievementsBtn);
|
| 269 |
-
const exitBtn = document.getElementById("exit-btn");
|
| 270 |
-
console.log("exitBtn:", exitBtn);
|
| 271 |
-
const idModal = document.getElementById("id-modal");
|
| 272 |
-
console.log("idModal:", idModal);
|
| 273 |
-
const welcomeModal = document.getElementById("welcome-modal");
|
| 274 |
-
console.log("welcomeModal:", welcomeModal);
|
| 275 |
-
const closeBtn = document.querySelector(".close-btn");
|
| 276 |
-
console.log("closeBtn:", closeBtn);
|
| 277 |
-
const closeWelcomeBtn = document.querySelector(".close-welcome-btn");
|
| 278 |
-
console.log("closeWelcomeBtn:", closeWelcomeBtn);
|
| 279 |
-
const confirmIdBtn = document.getElementById("confirm-id");
|
| 280 |
-
console.log("confirmIdBtn:", confirmIdBtn);
|
| 281 |
-
const playerIdInput = document.getElementById("player-id");
|
| 282 |
-
console.log("playerIdInput:", playerIdInput);
|
| 283 |
-
const idError = document.getElementById("id-error");
|
| 284 |
-
console.log("idError:", idError);
|
| 285 |
-
const welcomeMessage = document.getElementById("welcome-message");
|
| 286 |
-
console.log("welcomeMessage:", welcomeMessage);
|
| 287 |
-
const startAdventureBtn = document.getElementById("start-adventure");
|
| 288 |
-
console.log("startAdventureBtn:", startAdventureBtn);d(\'start-adventure\');
|
| 289 |
-
|
| 290 |
-
// 開始冒險按鈕點擊事件
|
| 291 |
-
if (startBtn) {
|
| 292 |
-
startBtn.addEventListener(\'click\', function() {
|
| 293 |
-
idModal.style.display = \'block\';
|
| 294 |
-
playerIdInput.value = \'\';
|
| 295 |
-
idError.style.display = \'none\';
|
| 296 |
-
});
|
| 297 |
-
}
|
| 298 |
-
|
| 299 |
-
// 關閉ID輸入彈窗
|
| 300 |
-
if (closeBtn) {
|
| 301 |
-
closeBtn.addEventListener(\'click\', function() {
|
| 302 |
-
idModal.style.display = \'none\';
|
| 303 |
-
});
|
| 304 |
-
}
|
| 305 |
-
|
| 306 |
-
// 關閉歡迎訊息彈窗
|
| 307 |
-
if (closeWelcomeBtn) {
|
| 308 |
-
closeWelcomeBtn.addEventListener(\'click\', function() {
|
| 309 |
-
welcomeModal.style.display = \'none\';
|
| 310 |
});
|
| 311 |
}
|
| 312 |
-
|
| 313 |
-
//
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
// 確認ID按鈕點擊事件
|
| 324 |
-
if (confirmIdBtn) {
|
| 325 |
-
confirmIdBtn.addEventListener(\'click\', function() {
|
| 326 |
-
const inputId = playerIdInput.value.trim();
|
| 327 |
-
|
| 328 |
-
if (inputId === \'\') {
|
| 329 |
-
idError.textContent = \'請輸入ID\';
|
| 330 |
-
idError.style.display = \'block\';
|
| 331 |
return;
|
| 332 |
}
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
// ID存在,顯示歡迎訊息
|
| 339 |
-
idModal.style.display = \'none\';
|
| 340 |
-
|
| 341 |
-
// 設置歡迎訊息
|
| 342 |
-
const message = `
|
| 343 |
-
<p class=\"highlight\">🌟 歡迎你,${player.profession}──${player.name}!</p>
|
| 344 |
-
<p>你被召喚至這個充滿數學魔力的世界,肩負起拯救「數學魔法王國」的重大使命。</p>
|
| 345 |
-
<p>傳說中,唯有真正的數學勇者,才能重新喚醒古老的「平方之力」──這股支撐王國、對抗黑暗的關鍵能量。</p>
|
| 346 |
-
<p>現在,時機已到。</p>
|
| 347 |
-
<p>接下來,你將踏上三段神聖試煉之路,學會感知、轉換與展開的力量。</p>
|
| 348 |
-
<p>前方的挑戰將磨練你的智慧與勇氣,</p>
|
| 349 |
-
<p>但只要堅持到底,王國的未來將因你而重生!</p>
|
| 350 |
-
<p>📜 任務即將開始,請準備好你的數學之心──</p>
|
| 351 |
-
<p>點擊下方按鈕,開啟你的冒險旅程!</p>
|
| 352 |
-
`;
|
| 353 |
-
|
| 354 |
-
welcomeMessage.innerHTML = message;
|
| 355 |
-
welcomeModal.style.display = \'block\';
|
| 356 |
-
|
| 357 |
-
// 儲存玩家ID到localStorage,實現跨裝置存檔
|
| 358 |
-
localStorage.setItem(\'currentPlayerId\', player.id);
|
| 359 |
-
localStorage.setItem(\'playerName\', player.name);
|
| 360 |
-
localStorage.setItem(\'playerProfession\', player.profession);
|
| 361 |
-
|
| 362 |
-
// 初始化遊戲進度
|
| 363 |
-
if (!localStorage.getItem(`gameProgress_${player.id}`)) {
|
| 364 |
-
const initialProgress = {
|
| 365 |
-
completedTrials: {
|
| 366 |
-
"平方之泉": {
|
| 367 |
-
completed: false,
|
| 368 |
-
score: 0,
|
| 369 |
-
challengesCompleted: [false, false, false, false, false, false]
|
| 370 |
-
},
|
| 371 |
-
"變換山谷": {
|
| 372 |
-
completed: false,
|
| 373 |
-
score: 0,
|
| 374 |
-
challengesCompleted: [false, false, false, false, false, false]
|
| 375 |
-
},
|
| 376 |
-
"展開之塔": {
|
| 377 |
-
completed: false,
|
| 378 |
-
score: 0,
|
| 379 |
-
challengesCompleted: [false, false, false, false, false, false]
|
| 380 |
-
}
|
| 381 |
-
},
|
| 382 |
-
lastSaved: new Date().toISOString()
|
| 383 |
-
};
|
| 384 |
-
|
| 385 |
-
localStorage.setItem(`gameProgress_${player.id}`, JSON.stringify(initialProgress));
|
| 386 |
-
}
|
| 387 |
-
|
| 388 |
-
// 解鎖首次登入成就
|
| 389 |
-
if (window.achievementSystem) {
|
| 390 |
-
window.achievementSystem.unlockAchievement(\'journey_1\');
|
| 391 |
-
}
|
| 392 |
-
|
| 393 |
} else {
|
| 394 |
-
|
| 395 |
-
idError.textContent = \'找不到此ID,請確認後重新輸入\';
|
| 396 |
-
idError.style.display = \'block\';
|
| 397 |
}
|
| 398 |
});
|
| 399 |
}
|
| 400 |
-
|
| 401 |
-
//
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
const playerProfession = localStorage.getItem(\'playerProfession\');
|
| 418 |
-
|
| 419 |
-
if (confirm(`檢測到 ${playerProfession}${playerName} 的存檔,是否繼續遊戲?`)) {
|
| 420 |
-
// 直接進入序章頁面
|
| 421 |
-
window.location.href = \'prologue.html\';
|
| 422 |
-
}
|
| 423 |
} else {
|
| 424 |
-
alert(
|
| 425 |
}
|
| 426 |
});
|
| 427 |
}
|
|
|
|
| 428 |
|
| 429 |
-
// 成就圖鑑按鈕點擊事件
|
| 430 |
-
if (achievementsBtn) {
|
| 431 |
-
achievementsBtn.addEventListener(\'click\', function() {
|
| 432 |
-
window.location.href = \'achievements.html\';
|
| 433 |
-
});
|
| 434 |
-
}
|
| 435 |
-
|
| 436 |
-
// 遊戲結束按鈕點擊事件
|
| 437 |
-
if (exitBtn) {
|
| 438 |
-
exitBtn.addEventListener(\'click\', function() {
|
| 439 |
-
if (confirm(\'確定要結束遊戲嗎?\')) {
|
| 440 |
-
alert(\'感謝遊玩《數學魔法王國:平方之旅》!\');
|
| 441 |
-
}
|
| 442 |
-
});
|
| 443 |
-
}
|
| 444 |
-
});
|
|
|
|
| 23 |
playerData.push(player);
|
| 24 |
}
|
| 25 |
|
| 26 |
+
console.log('玩家資料載入成功:', playerData);
|
| 27 |
} catch (error) {
|
| 28 |
+
console.error('載入玩家資料失敗:', error);
|
| 29 |
}
|
| 30 |
}
|
| 31 |
|
|
|
|
| 49 |
const playerId = data.id;
|
| 50 |
const progress = data.progress;
|
| 51 |
|
| 52 |
+
// 將進度存入localStorage
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
localStorage.setItem(`gameProgress_${playerId}`, JSON.stringify(progress));
|
| 54 |
+
localStorage.setItem('currentPlayerId', playerId);
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
return { success: true, playerId: playerId };
|
| 57 |
+
} catch (error) {
|
| 58 |
+
console.error('載入存檔失敗:', error);
|
| 59 |
+
return { success: false, error: '存檔格式錯誤' };
|
| 60 |
}
|
| 61 |
}
|
| 62 |
|
| 63 |
+
// 頁面載入完成後的初始化
|
| 64 |
+
document.addEventListener('DOMContentLoaded', async function() {
|
| 65 |
+
console.log('DOM 載入完成,開始初始化...');
|
| 66 |
+
|
| 67 |
+
// 載入玩家資料
|
| 68 |
+
await loadPlayerData();
|
| 69 |
+
|
| 70 |
+
// 獲取所有按鈕元素
|
| 71 |
+
const startGameBtn = document.getElementById('start-game');
|
| 72 |
+
const loadGameBtn = document.getElementById('load-game');
|
| 73 |
+
const achievementsBtn = document.getElementById('achievements');
|
| 74 |
+
const exitGameBtn = document.getElementById('exit-game');
|
| 75 |
+
const toggleBgmBtn = document.getElementById('toggle-bgm');
|
| 76 |
+
|
| 77 |
+
// 調試:檢查按鈕是否正確獲取
|
| 78 |
+
console.log('開始遊戲按鈕:', startGameBtn);
|
| 79 |
+
console.log('讀取進度按鈕:', loadGameBtn);
|
| 80 |
+
console.log('成就圖鑑按鈕:', achievementsBtn);
|
| 81 |
+
console.log('結束遊戲按鈕:', exitGameBtn);
|
| 82 |
+
console.log('音樂開關按鈕:', toggleBgmBtn);
|
| 83 |
+
|
| 84 |
+
// 獲取模態框元素
|
| 85 |
+
const playerModal = document.getElementById('player-modal');
|
| 86 |
+
const loadModal = document.getElementById('load-modal');
|
| 87 |
+
const closeModalBtns = document.querySelectorAll('.close');
|
| 88 |
+
|
| 89 |
+
// 調試:檢查模態框是否正確獲取
|
| 90 |
+
console.log('玩家選擇模態框:', playerModal);
|
| 91 |
+
console.log('讀取進度模態框:', loadModal);
|
| 92 |
+
console.log('關閉按鈕:', closeModalBtns);
|
| 93 |
+
|
| 94 |
+
// 開始遊戲按鈕
|
| 95 |
+
if (startGameBtn) {
|
| 96 |
+
startGameBtn.addEventListener('click', function() {
|
| 97 |
+
console.log('點擊開始遊戲按鈕');
|
| 98 |
+
if (playerModal) {
|
| 99 |
+
playerModal.style.display = 'block';
|
| 100 |
+
populatePlayerList();
|
| 101 |
+
} else {
|
| 102 |
+
console.error('找不到玩家選擇模態框');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
}
|
| 104 |
+
});
|
| 105 |
+
} else {
|
| 106 |
+
console.error('找不到開始遊戲按鈕');
|
|
|
|
| 107 |
}
|
| 108 |
+
|
| 109 |
+
// 讀取進度按鈕
|
| 110 |
+
if (loadGameBtn) {
|
| 111 |
+
loadGameBtn.addEventListener('click', function() {
|
| 112 |
+
console.log('點擊讀取進度按鈕');
|
| 113 |
+
if (loadModal) {
|
| 114 |
+
loadModal.style.display = 'block';
|
| 115 |
+
} else {
|
| 116 |
+
console.error('找不到讀取進度模態框');
|
| 117 |
+
}
|
| 118 |
+
});
|
| 119 |
+
} else {
|
| 120 |
+
console.error('找不到讀取進度按鈕');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
}
|
| 122 |
+
|
| 123 |
+
// 成就圖鑑按鈕
|
| 124 |
+
if (achievementsBtn) {
|
| 125 |
+
achievementsBtn.addEventListener('click', function() {
|
| 126 |
+
console.log('點擊成就圖鑑按鈕');
|
| 127 |
+
window.location.href = 'achievements.html';
|
| 128 |
+
});
|
| 129 |
+
} else {
|
| 130 |
+
console.error('找不到成就圖鑑按鈕');
|
|
|
|
|
|
|
| 131 |
}
|
| 132 |
+
|
| 133 |
+
// 結束遊戲按鈕
|
| 134 |
+
if (exitGameBtn) {
|
| 135 |
+
exitGameBtn.addEventListener('click', function() {
|
| 136 |
+
console.log('點擊結束遊戲按鈕');
|
| 137 |
+
if (confirm('確定要結束遊戲嗎?')) {
|
| 138 |
+
window.close();
|
| 139 |
+
}
|
| 140 |
+
});
|
| 141 |
+
} else {
|
| 142 |
+
console.error('找不到結束遊戲按鈕');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
}
|
| 144 |
+
|
| 145 |
+
// 關閉模態框
|
| 146 |
+
closeModalBtns.forEach(function(btn) {
|
| 147 |
+
btn.addEventListener('click', function() {
|
| 148 |
+
console.log('點擊關閉模態框按鈕');
|
| 149 |
+
const modal = btn.closest('.modal');
|
| 150 |
+
if (modal) {
|
| 151 |
+
modal.style.display = 'none';
|
| 152 |
+
}
|
| 153 |
+
});
|
| 154 |
+
});
|
| 155 |
+
|
| 156 |
+
// 點擊模態框外部關閉
|
| 157 |
+
window.addEventListener('click', function(event) {
|
| 158 |
+
if (event.target.classList.contains('modal')) {
|
| 159 |
+
event.target.style.display = 'none';
|
| 160 |
+
}
|
| 161 |
+
});
|
| 162 |
+
|
| 163 |
+
// 處理玩家選擇
|
| 164 |
+
function populatePlayerList() {
|
| 165 |
+
const playerList = document.getElementById('player-list');
|
| 166 |
+
if (!playerList) {
|
| 167 |
+
console.error('找不到玩家列表元素');
|
| 168 |
+
return;
|
| 169 |
+
}
|
| 170 |
|
| 171 |
+
playerList.innerHTML = '';
|
|
|
|
| 172 |
|
| 173 |
+
if (playerData.length === 0) {
|
| 174 |
+
playerList.innerHTML = '<p>正在載入玩家資料...</p>';
|
| 175 |
+
return;
|
| 176 |
+
}
|
| 177 |
|
| 178 |
+
playerData.forEach(function(player) {
|
| 179 |
+
const playerDiv = document.createElement('div');
|
| 180 |
+
playerDiv.className = 'player-option';
|
| 181 |
+
playerDiv.innerHTML = `
|
| 182 |
+
<h3>${player.name}</h3>
|
| 183 |
+
<p>職業:${player.profession}</p>
|
| 184 |
+
<p>ID:${player.id}</p>
|
| 185 |
+
`;
|
| 186 |
|
| 187 |
+
playerDiv.addEventListener('click', function() {
|
| 188 |
+
console.log('選擇玩家:', player);
|
| 189 |
+
localStorage.setItem('currentPlayerId', player.id);
|
| 190 |
+
localStorage.setItem('currentPlayerName', player.name);
|
| 191 |
+
localStorage.setItem('currentPlayerProfession', player.profession);
|
| 192 |
+
|
| 193 |
+
// 檢查是否有存檔
|
| 194 |
+
const existingProgress = localStorage.getItem(`gameProgress_${player.id}`);
|
| 195 |
+
if (existingProgress) {
|
| 196 |
+
if (confirm(`發現 ${player.name} 的存檔,是否繼續之前的進度?`)) {
|
| 197 |
+
window.location.href = 'kingdom_map.html';
|
| 198 |
+
} else {
|
| 199 |
+
// 重新開始遊戲
|
| 200 |
+
localStorage.removeItem(`gameProgress_${player.id}`);
|
| 201 |
+
window.location.href = 'prologue.html';
|
| 202 |
+
}
|
| 203 |
+
} else {
|
| 204 |
+
// 新遊戲
|
| 205 |
+
window.location.href = 'prologue.html';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
}
|
| 207 |
+
});
|
| 208 |
+
|
| 209 |
+
playerList.appendChild(playerDiv);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
});
|
| 211 |
}
|
| 212 |
+
|
| 213 |
+
// 處理存檔碼載入
|
| 214 |
+
const loadSaveBtn = document.getElementById('load-save-btn');
|
| 215 |
+
const saveCodeInput = document.getElementById('save-code-input');
|
| 216 |
+
|
| 217 |
+
if (loadSaveBtn && saveCodeInput) {
|
| 218 |
+
loadSaveBtn.addEventListener('click', function() {
|
| 219 |
+
const saveCode = saveCodeInput.value.trim();
|
| 220 |
+
if (!saveCode) {
|
| 221 |
+
alert('請輸入存檔碼');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
return;
|
| 223 |
}
|
| 224 |
+
|
| 225 |
+
const result = loadFromSaveCode(saveCode);
|
| 226 |
+
if (result.success) {
|
| 227 |
+
alert('存檔載入成功!');
|
| 228 |
+
window.location.href = 'kingdom_map.html';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
} else {
|
| 230 |
+
alert('存檔載入失敗:' + result.error);
|
|
|
|
|
|
|
| 231 |
}
|
| 232 |
});
|
| 233 |
}
|
| 234 |
+
|
| 235 |
+
// 處理存檔碼生成
|
| 236 |
+
const generateSaveBtn = document.getElementById('generate-save-btn');
|
| 237 |
+
const saveCodeOutput = document.getElementById('save-code-output');
|
| 238 |
+
|
| 239 |
+
if (generateSaveBtn && saveCodeOutput) {
|
| 240 |
+
generateSaveBtn.addEventListener('click', function() {
|
| 241 |
+
const currentPlayerId = localStorage.getItem('currentPlayerId');
|
| 242 |
+
if (!currentPlayerId) {
|
| 243 |
+
alert('請先開始遊戲');
|
| 244 |
+
return;
|
| 245 |
+
}
|
| 246 |
+
|
| 247 |
+
const saveCode = generateSaveCode(currentPlayerId);
|
| 248 |
+
if (saveCode) {
|
| 249 |
+
saveCodeOutput.value = saveCode;
|
| 250 |
+
alert('存檔碼已生成,請複製保存');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
} else {
|
| 252 |
+
alert('沒有找到遊戲進度');
|
| 253 |
}
|
| 254 |
});
|
| 255 |
}
|
| 256 |
+
});
|
| 257 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|