feat: Add player entity with WASD/space controls and spawn functionality

This commit is contained in:
Kacper Kostka (aider)
2025-04-05 18:03:10 +02:00
parent d86baa8f99
commit db5b49ee7f
6 changed files with 274 additions and 1 deletions

View File

@@ -40,6 +40,9 @@ window.onload = function() {
document.getElementById('triangle-btn').addEventListener('click', () => setTool(TRIANGLE));
document.getElementById('eraser-btn').addEventListener('click', () => setTool(EMPTY));
// Add player spawn button
document.getElementById('spawn-player-btn').addEventListener('click', spawnPlayer);
// Navigation controls
document.getElementById('move-left').addEventListener('click', () => moveWorld(-CHUNK_SIZE/2, 0));
document.getElementById('move-right').addEventListener('click', () => moveWorld(CHUNK_SIZE/2, 0));
@@ -86,6 +89,30 @@ function resizeCanvas() {
canvas.height = window.innerHeight - document.querySelector('.controls').offsetHeight;
}
// Function to spawn player
function spawnPlayer() {
// Hide HUD elements
document.querySelector('.controls').style.display = 'none';
// Set zoom level to 50%
PIXEL_SIZE = 2;
// Create player at specified coordinates
player = createEntity(ENTITY_TYPES.PLAYER, 229, 61);
// Focus camera on player
worldOffsetX = player.x - (canvas.width / PIXEL_SIZE / 2);
worldOffsetY = player.y - (canvas.height / PIXEL_SIZE / 2);
worldMoved = true;
// Resize canvas to full screen
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Remove the event listener for the spawn button to prevent multiple spawns
document.getElementById('spawn-player-btn').removeEventListener('click', spawnPlayer);
}
function simulationLoop(timestamp) {
// Calculate FPS
const deltaTime = timestamp - lastFrameTime;
@@ -93,6 +120,11 @@ function simulationLoop(timestamp) {
fps = Math.round(1000 / deltaTime);
document.getElementById('fps').textContent = `FPS: ${fps}`;
// Update player movement if player exists
if (player) {
updatePlayerMovement();
}
// Update physics with timestamp for rate limiting
updatePhysics(timestamp);