80d96cff75
- Implement Doors and Alleys spatial navigation system - Add Tír na nÓg pixel-art aesthetic and smooth transitions - Support keyboard (WASD/Arrows) and mouse-based navigation - Restructure HTML into a navigable 2D grid of rooms Co-authored-by: micmug <185775013+micmug@users.noreply.github.com>
93 lines
2.6 KiB
JavaScript
93 lines
2.6 KiB
JavaScript
/**
|
|
* Tír na nÓg - Spatial Navigation
|
|
* Logic for moving through the grid of rooms using doors and keys.
|
|
*/
|
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
const world = document.getElementById('world');
|
|
const doors = document.querySelectorAll('.door');
|
|
const rooms = document.querySelectorAll('.room');
|
|
|
|
let currentPos = { x: 0, y: 0 };
|
|
const gridSize = { width: 2, height: 2 };
|
|
|
|
/**
|
|
* Updates the viewport position based on current x, y coordinates
|
|
*/
|
|
function updateView() {
|
|
const translateX = -currentPos.x * 100;
|
|
const translateY = -currentPos.y * 100;
|
|
world.style.transform = `translate(${translateX}vw, ${translateY}vh)`;
|
|
|
|
// Update active class for rooms
|
|
rooms.forEach(room => {
|
|
const id = `room-${currentPos.x}-${currentPos.y}`;
|
|
if (room.id === id) {
|
|
room.classList.add('active');
|
|
} else {
|
|
room.classList.remove('active');
|
|
}
|
|
});
|
|
|
|
console.log(`Moved to room: ${currentPos.x}, ${currentPos.y}`);
|
|
}
|
|
|
|
/**
|
|
* Handles navigation to a specific target coordinate
|
|
*/
|
|
function navigateTo(targetStr) {
|
|
const [x, y] = targetStr.split(',').map(Number);
|
|
if (!isNaN(x) && !isNaN(y)) {
|
|
currentPos.x = x;
|
|
currentPos.y = y;
|
|
updateView();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Moves the position relative to current pos
|
|
*/
|
|
function moveRelative(dx, dy) {
|
|
const newX = currentPos.x + dx;
|
|
const newY = currentPos.y + dy;
|
|
|
|
if (newX >= 0 && newX < gridSize.width && newY >= 0 && newY < gridSize.height) {
|
|
currentPos.x = newX;
|
|
currentPos.y = newY;
|
|
updateView();
|
|
}
|
|
}
|
|
|
|
// Door Click Listeners
|
|
doors.forEach(door => {
|
|
door.addEventListener('click', (e) => {
|
|
const target = e.target.getAttribute('data-target');
|
|
navigateTo(target);
|
|
});
|
|
});
|
|
|
|
// Keyboard Listeners (WASD + Arrows)
|
|
document.addEventListener('keydown', (e) => {
|
|
switch(e.key.toLowerCase()) {
|
|
case 'arrowup':
|
|
case 'w':
|
|
moveRelative(0, -1);
|
|
break;
|
|
case 'arrowdown':
|
|
case 's':
|
|
moveRelative(0, 1);
|
|
break;
|
|
case 'arrowleft':
|
|
case 'a':
|
|
moveRelative(-1, 0);
|
|
break;
|
|
case 'arrowright':
|
|
case 'd':
|
|
moveRelative(1, 0);
|
|
break;
|
|
}
|
|
});
|
|
|
|
console.log('Tír na nÓg navigation initialized');
|
|
});
|