// ==================== МЕНЮ ⋯ + СОХРАНЕНИЕ / СБРОС ==================== import { state } from '../core/state.js'; import { playSound } from '../audio/sound-engine.js'; import { saveGame } from '../game/save.js'; import { shareWorld } from './share.js'; import { isTgWebApp } from './start-menu.js'; let dropdownOpen = false; export function initSaveControls() { const menuBtn = document.getElementById('menuBtn'); const menuDropdown = document.getElementById('menuDropdown'); const saveItem = document.getElementById('saveItem'); const resetItem = document.getElementById('resetItem'); const marketItem = document.getElementById('marketItem'); const shareItem = document.getElementById('shareItem'); const settingsItem = document.getElementById('settingsItem'); const quitItem = document.getElementById('quitItem'); // Убить возможный старый onclick if (menuBtn) menuBtn.onclick = null; // Тогл меню if (menuBtn) { menuBtn.addEventListener('click', (e) => { e.stopPropagation(); dropdownOpen = !dropdownOpen; if (menuDropdown) menuDropdown.classList.toggle('open', dropdownOpen); }); } // Закрытие по клику вне document.addEventListener('click', () => { if (dropdownOpen) { dropdownOpen = false; if (menuDropdown) menuDropdown.classList.remove('open'); } }); if (menuDropdown) { menuDropdown.addEventListener('click', (e) => e.stopPropagation()); } // 💾 Сохранить if (saveItem) { saveItem.addEventListener('click', () => { playSound('click'); saveGame(); dropdownOpen = false; if (menuDropdown) menuDropdown.classList.remove('open'); alert('Игра сохранена!'); }); } // 🔄 Новый мир if (resetItem) { resetItem.addEventListener('click', () => { if (confirm('Вы уверены, что хотите удалить сохранение и начать новую игру?')) { playSound('click'); try { localStorage.removeItem(state.SAVE_KEY); } catch(e) {} state.inMemorySave = null; state.worldId = Math.random().toString(36).substring(2, 10); try { const newUrl = new URL(window.location.href); newUrl.searchParams.set('world', state.worldId); window.history.replaceState(null, '', newUrl.toString()); } catch(e) {} location.reload(); } else { dropdownOpen = false; if (menuDropdown) menuDropdown.classList.remove('open'); } }); } // 🏪 Рынок if (marketItem) { marketItem.addEventListener('click', () => { dropdownOpen = false; if (menuDropdown) menuDropdown.classList.remove('open'); document.dispatchEvent(new CustomEvent('toggleMarket')); }); } // 📤 Пригласить if (shareItem) { shareItem.addEventListener('click', () => { dropdownOpen = false; if (menuDropdown) menuDropdown.classList.remove('open'); shareWorld(); }); } // ⚙️ Настройки if (settingsItem) { settingsItem.addEventListener('click', () => { dropdownOpen = false; menuDropdown.classList.remove('open'); const panel = document.getElementById('settingsPanel'); if (!panel) return; // Sync controls with current state const toggle = document.getElementById('dayNightToggle'); const timeSlider = document.getElementById('timeSlider'); const wSlider = document.getElementById('weatherIntensity'); if (toggle) toggle.checked = state.dayNightCycle !== false; if (timeSlider) timeSlider.value = Math.round(state.worldTime * 100); if (wSlider) wSlider.value = Math.round(state.rainIntensity * 100); // Sync weather buttons — highlight the active type const wt = state.weatherType || (state.isRaining ? 'rain' : 'clear'); const activeId = 'weather' + wt.charAt(0).toUpperCase() + wt.slice(1); for (const bid of ['weatherClear','weatherRain','weatherSnow','weatherThunder']) { const b = document.getElementById(bid); if (b) b.style.background = (bid === activeId) ? '#3498db' : '#555'; } panel.style.display = 'block'; }); } // Закрытие настроек const settingsClose = document.getElementById('settingsClose'); if (settingsClose) { settingsClose.addEventListener('click', () => { const settingsPanel = document.getElementById('settingsPanel'); if (settingsPanel) settingsPanel.style.display = 'none'; }); } // Тогл день/ночи const dayNightToggle = document.getElementById('dayNightToggle'); if (dayNightToggle) { dayNightToggle.addEventListener('change', () => { state.dayNightCycle = dayNightToggle.checked; saveGame(); }); } // 🏠 Главное меню if (quitItem) { quitItem.addEventListener('click', () => { dropdownOpen = false; if (menuDropdown) menuDropdown.classList.remove('open'); saveGame(); try { const url = new URL(window.location.href); url.searchParams.delete('world'); window.location.href = url.toString(); } catch(e) { location.reload(); } }); } } export function updateSaveButtonVisibility() { const saveItem = document.getElementById('saveItem'); if (!saveItem) return; saveItem.style.display = (state.isMultiplayer && state.otherPlayers.size > 0) ? 'none' : 'block'; }