From 3af660e56b32aad915d8039d3c015359fc9ea706 Mon Sep 17 00:00:00 2001 From: Nathan Date: Fri, 19 Dec 2025 01:20:23 +0700 Subject: [PATCH] finally --- assets/mainboard.js | 118 ++++++++++++++++++++++++++------------------ 1 file changed, 69 insertions(+), 49 deletions(-) diff --git a/assets/mainboard.js b/assets/mainboard.js index 48f1926..0b75c0a 100644 --- a/assets/mainboard.js +++ b/assets/mainboard.js @@ -1,66 +1,86 @@ document.addEventListener('DOMContentLoaded', () => { - // --- 1. LOGIKA MUSIK (FIXED) --- const musicBtn = document.getElementById('musicBtn'); const bgMusic = document.getElementById('bgMusic'); + + let isMusicPlaying = false; + musicBtn.innerText = '🔇'; - if (musicBtn && bgMusic) { - // Cek status awal: Kalau lagu pause, icon harus mute - if (bgMusic.paused) { - musicBtn.textContent = '🔇'; + if (bgMusic) { + bgMusic.play() + .then(() => { + console.log("Auto-play sukses"); + isMusicPlaying = true; + musicBtn.innerText = '🔊'; + }) + .catch((error) => { + console.log("Auto-play diblokir browser, menunggu klik user."); + isMusicPlaying = false; + musicBtn.innerText = '🔇'; + }); + } + + musicBtn.addEventListener('click', () => { + if (isMusicPlaying) { + bgMusic.pause(); + musicBtn.innerText = '🔇'; + isMusicPlaying = false; } else { - musicBtn.textContent = '🔊'; + bgMusic.play(); + musicBtn.innerText = '🔊'; + isMusicPlaying = true; } + }); - musicBtn.addEventListener('click', () => { - if (bgMusic.paused) { - // Kalau lagi mati, nyalakan - bgMusic.play().then(() => { - musicBtn.textContent = '🔊'; - }).catch((e) => { - console.log("Browser memblokir audio:", e); - }); - } else { - // Kalau lagi nyala, matikan - bgMusic.pause(); - musicBtn.textContent = '🔇'; - } - }); - } else { - console.error("Element Audio/Tombol tidak ditemukan!"); - } - - // --- 2. LOGOUT --- - const logoutBtn = document.getElementById("logoutBtn"); - if (logoutBtn) { - logoutBtn.addEventListener("click", () => { - window.location.href = "logout.php"; - }); - } - - // --- 3. LEADERBOARD --- - const leaderboardBtn = document.getElementById("leaderboardBtn"); - if (leaderboardBtn) { - leaderboardBtn.addEventListener("click", () => { - window.location.href = "Leaderboard.php"; - }); - } }); -// --- FUNGSI GLOBAL (Bisa dipanggil dari onclick HTML) --- +// --- INIT AUDIO CLICK --- +// Pastikan path-nya sesuai dengan folder kamu +const sfxClick = new Audio('music/music-click.mp3'); -// NAVIGATION -function selectStage(stage) { - console.log("Pindah ke stage:", stage); // Debugging - window.location.href = "gameboard-" + stage + ".php"; +// Fungsi helper biar rapi +function playClick() { + sfxClick.currentTime = 0; + sfxClick.play().catch(() => {}); +} + +function selectStage(stage) { + playClick(); + // Kasih delay dikit (300ms) biar suara 'Ceklik' kedengeran sebelum pindah + setTimeout(() => { + window.location.href = "gameboard-" + stage + ".php"; + }, 300); } -// OVERLAY CREDITS function openCredits() { - const overlay = document.getElementById('creditsOverlay'); - if(overlay) overlay.style.display = 'flex'; + playClick(); + document.getElementById('creditsOverlay').style.display = 'flex'; } function closeCredits() { - const overlay = document.getElementById('creditsOverlay'); - if(overlay) overlay.style.display = 'none'; + playClick(); + document.getElementById('creditsOverlay').style.display = 'none'; +} + +// Untuk Logout +const logoutBtn = document.getElementById("logoutBtn"); +if (logoutBtn) { + logoutBtn.addEventListener("click", (e) => { + e.preventDefault(); // Tahan dulu biar gak langsung pindah + playClick(); + setTimeout(() => { + window.location.href = "logout.php"; + }, 300); + }); +} + +// Untuk Leaderboard +const leaderboardBtn = document.getElementById("leaderboardBtn"); +if (leaderboardBtn) { + leaderboardBtn.addEventListener("click", (e) => { + e.preventDefault(); // Tahan dulu + playClick(); + setTimeout(() => { + window.location.href = "Leaderboard.php"; + }, 300); + }); } \ No newline at end of file