forked from zero-to-mastery/ZTM-Quest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainMenuScene.js
More file actions
142 lines (116 loc) · 4.62 KB
/
mainMenuScene.js
File metadata and controls
142 lines (116 loc) · 4.62 KB
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
import { k } from '../kplayCtx';
import { openSettingsModal } from '../utils/menuModals';
import { showCharacterSelectModal } from '../utils/characterSelect';
import { getGameState } from '../utils/gameState';
export function mainMenuScene() {
const mainMenuElement = document.getElementById('main-menu');
if (!mainMenuElement) {
return;
}
if (localStorage.getItem('showCharacterSelect') === 'true') {
localStorage.removeItem('showCharacterSelect');
mainMenuElement.classList.add('hidden');
mainMenuElement.style.display = 'none';
showCharacterSelectModal();
return;
}
const startNewGameBtn = document.getElementById('start-new-game-btn');
const continueGameBtn = document.getElementById('continue-game-btn');
const achievementsBtn = document.getElementById(
'main-menu-achievements-btn'
);
const statsBtn = document.getElementById('main-menu-stats-btn');
const settingsBtn = document.getElementById('main-menu-settings-btn');
const creditsBtn = document.getElementById('main-menu-credits-btn');
const gameState = getGameState();
// Check if localStorage has REAL save data (not just initialized state)
const hasSaveData = localStorage.getItem('gameStateZtmQuest') !== null;
if (continueGameBtn) {
continueGameBtn.style.display = hasSaveData ? 'block' : 'none';
}
function replaceButton(btn, handler) {
if (!btn) return;
const newBtn = btn.cloneNode(true);
btn.parentNode.replaceChild(newBtn, btn);
newBtn.addEventListener('click', handler);
return newBtn;
}
let isStarting = false;
let enterHandler;
replaceButton(startNewGameBtn, () => {
if (isStarting) return;
isStarting = true;
// Save audio settings before clearing
const audioMuted = localStorage.getItem('ztm-audio-muted');
const musicVolume = localStorage.getItem('ztm-music-volume');
// Clear ALL localStorage completely
localStorage.clear();
// Restore only audio settings
if (audioMuted) localStorage.setItem('ztm-audio-muted', audioMuted);
if (musicVolume)
localStorage.setItem('ztm-music-volume', musicVolume);
// Set flag to show character select after reload
localStorage.setItem('showCharacterSelect', 'true');
// Hard refresh to clear everything including memory cache
window.location.reload();
});
if (hasSaveData && continueGameBtn) {
replaceButton(continueGameBtn, () => {
if (isStarting) return;
isStarting = true;
closeAllMenus();
startGame(gameState.player.scene);
});
}
replaceButton(achievementsBtn, () => openModal('achievements-modal'));
replaceButton(statsBtn, () => {
openModal('stats-modal');
updateStatsDisplay();
});
replaceButton(settingsBtn, () => openSettingsModal());
replaceButton(creditsBtn, () => openModal('credits-modal'));
enterHandler = k.onKeyPress('enter', () => {
if (isStarting) return;
isStarting = true;
closeAllMenus();
showCharacterSelectModal();
});
function startGame(sceneName) {
try {
closeAllMenus();
if (enterHandler) enterHandler.cancel();
k.go(sceneName);
} catch (error) {
console.error('Error starting game:', error);
mainMenuElement.classList.remove('hidden');
mainMenuElement.style.display = 'flex';
isStarting = false;
}
}
function openModal(modalId) {
const modal = document.getElementById(modalId);
if (modal) modal.style.display = 'flex';
}
function closeAllMenus() {
const modals = document.querySelectorAll('.modal-overlay');
modals.forEach((m) => (m.style.display = 'none'));
mainMenuElement.classList.add('hidden');
mainMenuElement.style.display = 'none';
}
function updateStatsDisplay() {
const gs = getGameState();
const minutes = gs.time?.minutes || 0;
const hours = Math.floor(minutes / 60);
const mins = Math.floor(minutes % 60);
const setText = (id, value) => {
const el = document.getElementById(id);
if (el) el.textContent = value;
};
setText('stat-time', hours > 0 ? `${hours}h ${mins}m` : `${mins}m`);
setText('stat-coins', gs.player?.coins ?? 0);
setText('stat-energy', gs.player?.energy ?? 100);
setText('stat-quests', '0');
setText('stat-maps', '1');
setText('stat-achievements', '0/0');
}
}