File size: 7,390 Bytes
f732e14
 
 
 
 
 
4938ff5
f732e14
 
4938ff5
 
 
f732e14
 
4938ff5
f732e14
4938ff5
f732e14
 
 
 
 
 
aaab911
f732e14
67b0887
f732e14
67b0887
e86d811
f732e14
aaab911
f732e14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3775b38
67b0887
f732e14
67b0887
3775b38
67b0887
 
 
 
f732e14
 
 
67b0887
 
 
 
 
 
 
 
b96428a
 
 
 
67b0887
 
 
 
 
 
 
 
 
 
b96428a
 
 
67b0887
 
 
 
 
 
 
 
 
 
 
 
b96428a
67b0887
 
6871120
67b0887
 
 
23e674e
67b0887
 
 
 
 
dc9ab84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67b0887
 
 
3775b38
67b0887
 
 
 
 
 
 
 
 
e86d811
67b0887
 
 
 
 
 
 
 
 
 
 
3775b38
67b0887
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b96428a
 
 
 
67b0887
b96428a
 
 
 
 
f732e14
 
67b0887
b96428a
 
 
67b0887
b96428a
 
 
 
 
 
 
 
 
 
f732e14
b96428a
 
f732e14
 
 
67b0887
f732e14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// 玩家資料
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';
            }
        });
    }
});