fix: Optimize player spawn, camera zoom, and performance to reduce lag

This commit is contained in:
Kacper Kostka (aider)
2025-04-05 18:06:39 +02:00
parent 85a96f153f
commit ad90b9320f
3 changed files with 15 additions and 10 deletions

View File

@@ -94,6 +94,10 @@ function spawnPlayer() {
// Hide HUD elements
document.querySelector('.controls').style.display = 'none';
// Resize canvas to full screen first
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Set zoom level to 50%
PIXEL_SIZE = 2;
@@ -105,9 +109,8 @@ function spawnPlayer() {
worldOffsetY = player.y - (canvas.height / PIXEL_SIZE / 2);
worldMoved = true;
// Resize canvas to full screen
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Clear chunk cache to force redraw at new zoom level
chunkCanvasCache.clear();
// Remove the event listener for the spawn button to prevent multiple spawns
document.getElementById('spawn-player-btn').removeEventListener('click', spawnPlayer);
@@ -128,8 +131,10 @@ function simulationLoop(timestamp) {
// Update physics with timestamp for rate limiting
updatePhysics(timestamp);
// Render
render();
// Render - skip rendering if FPS is too low to prevent death spiral
if (fps > 10 || timestamp % 3 < 1) {
render();
}
// Memory management: Clean up chunk cache for chunks that are far away
if (timestamp % 5000 < 16) { // Run every ~5 seconds