feat: Add player entity with WASD/space controls and spawn functionality
This commit is contained in:
36
js/input.js
36
js/input.js
@@ -4,6 +4,42 @@ let isDragging = false;
|
||||
let lastMouseX, lastMouseY;
|
||||
let currentMouseX, currentMouseY;
|
||||
|
||||
// Keyboard state tracking
|
||||
const keyState = {};
|
||||
let player = null;
|
||||
|
||||
// Handle keyboard input for player movement
|
||||
window.addEventListener('keydown', (e) => {
|
||||
keyState[e.code] = true;
|
||||
|
||||
// Prevent default behavior for game control keys
|
||||
if (['KeyW', 'KeyA', 'KeyS', 'KeyD', 'Space'].includes(e.code)) {
|
||||
e.preventDefault();
|
||||
}
|
||||
});
|
||||
|
||||
window.addEventListener('keyup', (e) => {
|
||||
keyState[e.code] = false;
|
||||
});
|
||||
|
||||
function updatePlayerMovement() {
|
||||
if (!player) return;
|
||||
|
||||
// Reset movement flag
|
||||
player.stopMoving();
|
||||
|
||||
// Handle movement
|
||||
if (keyState['KeyA']) {
|
||||
player.moveLeft();
|
||||
}
|
||||
if (keyState['KeyD']) {
|
||||
player.moveRight();
|
||||
}
|
||||
if (keyState['KeyW'] || keyState['Space']) {
|
||||
player.jump();
|
||||
}
|
||||
}
|
||||
|
||||
function setTool(tool) {
|
||||
currentTool = tool;
|
||||
document.querySelectorAll('.tools button').forEach(btn => btn.classList.remove('active'));
|
||||
|
||||
Reference in New Issue
Block a user