Files

79 lines
3.1 KiB
JavaScript

(() => {
const map = document.querySelector('.slx-world-map');
const Audio = window.AudioContext || window.webkitAudioContext;
if (!map) return;
const stage = map.querySelector('.slx-world-stage');
const standing = stage.querySelector('.slx-map-avatar:not(.slx-map-avatar-selected)');
const selected = stage.querySelector('.slx-map-avatar-selected');
if (!standing || !selected) return;
let landed = false;
const move = (x,y) => {
const avatar = landed ? selected : standing;
avatar.style.left = x;
avatar.style.top = y;
avatar.hidden = false;
};
stage.addEventListener('pointermove', event => {
if (landed || event.pointerType === 'touch') return;
const bounds = stage.getBoundingClientRect();
move(`${event.clientX-bounds.left}px`, `${event.clientY-bounds.top}px`);
stage.classList.add('has-avatar-pointer');
});
stage.addEventListener('pointerleave', () => {
standing.hidden = true;
stage.classList.remove('has-avatar-pointer');
});
addEventListener('pageshow', () => { landed = false; standing.hidden = selected.hidden = true; stage.classList.remove('has-avatar-pointer'); });
const toggle = map.querySelector('[data-map-sound]');
let enabled = true;
try { enabled = localStorage.getItem('slx-map-sound') !== 'off'; } catch {}
const reflect = () => toggle.setAttribute('aria-pressed', String(enabled));
reflect();
toggle.hidden = !Audio;
toggle.addEventListener('click', () => {
enabled = !enabled;
reflect();
try { localStorage.setItem('slx-map-sound', enabled ? 'on' : 'off'); } catch {}
});
let audio, navigating = false;
map.addEventListener('click', event => {
const link = event.target.closest('.slx-world-marker');
if (!link || event.defaultPrevented || event.button !== 0 ||
event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;
event.preventDefault();
if (navigating) return;
navigating = true;
landed = true;
standing.hidden = true;
move(link.style.getPropertyValue('--x'), link.style.getPropertyValue('--y'));
let timeout;
const go = () => {
if (!navigating) return;
clearTimeout(timeout);
navigating = false;
location.assign(link.href);
};
timeout = setTimeout(go, enabled && Audio ? 420 : 280);
if (!enabled || !Audio) return;
try {
audio ||= new Audio();
audio.resume().then(() => {
if (!navigating) return;
const now = audio.currentTime;
const tone = audio.createOscillator();
const gain = audio.createGain();
tone.frequency.setValueAtTime(1090, now);
tone.frequency.exponentialRampToValueAtTime(510, now + .12);
gain.gain.setValueAtTime(.001, now);
gain.gain.exponentialRampToValueAtTime(.17, now + .014);
gain.gain.exponentialRampToValueAtTime(.001, now + .14);
tone.connect(gain).connect(audio.destination);
tone.onended = () => setTimeout(go, 130);
tone.start(now);
tone.stop(now + .15);
}).catch(() => {});
} catch { /* The navigation timer still runs without audio. */ }
});
})();