fix: Add missing dirtyChunks variable and update rendering logic

This commit is contained in:
Kacper Kostka (aider)
2025-04-05 15:37:55 +02:00
parent 60a1757ab1
commit 15fb106246
4 changed files with 125 additions and 54 deletions

View File

@@ -1,5 +1,12 @@
// Physics simulation functions
function updatePhysics() {
function updatePhysics(timestamp) {
// Check if we should update physics based on the update rate
if (timestamp && lastPhysicsTime && timestamp - lastPhysicsTime < physicsUpdateRate) {
return;
}
lastPhysicsTime = timestamp || 0;
// Get visible chunks
const visibleChunks = getVisibleChunks();
@@ -12,6 +19,7 @@ function updatePhysics() {
if (!isVisible) continue;
const chunk = getOrCreateChunk(chunkX, chunkY);
let chunkModified = false;
// Process from bottom to top, right to left for correct gravity simulation
for (let y = CHUNK_SIZE - 1; y >= 0; y--) {
@@ -29,28 +37,42 @@ function updatePhysics() {
const worldX = chunkX * CHUNK_SIZE + x;
const worldY = chunkY * CHUNK_SIZE + y;
if (type === SAND) {
updateSand(worldX, worldY);
} else if (type === WATER) {
updateWater(worldX, worldY);
} else if (type === DIRT) {
updateDirt(worldX, worldY);
} else if (type === GRASS) {
updateGrass(worldX, worldY);
} else if (type === SEED) {
updateSeed(worldX, worldY);
} else if (type === GRASS_BLADE) {
updateGrassBlade(worldX, worldY);
} else if (type === FLOWER) {
updateFlower(worldX, worldY);
} else if (type === TREE_SEED) {
updateTreeSeed(worldX, worldY);
} else if (type === FIRE) {
updateFire(worldX, worldY);
} else if (type === LAVA) {
updateLava(worldX, worldY);
// Use a lookup table for faster element updates
const updateFunctions = {
[SAND]: updateSand,
[WATER]: updateWater,
[DIRT]: updateDirt,
[GRASS]: updateGrass,
[SEED]: updateSeed,
[GRASS_BLADE]: updateGrassBlade,
[FLOWER]: updateFlower,
[TREE_SEED]: updateTreeSeed,
[FIRE]: updateFire,
[LAVA]: updateLava
};
const updateFunction = updateFunctions[type];
if (updateFunction) {
const wasModified = updateFunction(worldX, worldY);
if (wasModified) {
chunkModified = true;
}
}
}
}
// Mark chunk as dirty if it was modified
if (chunkModified) {
dirtyChunks.add(getChunkKey(chunkX, chunkY));
}
}
// Adaptive physics rate based on FPS
if (fps < 30 && physicsUpdateRate < 32) {
// If FPS is low, update physics less frequently
physicsUpdateRate = Math.min(32, physicsUpdateRate + 2);
} else if (fps > 50 && physicsUpdateRate > 16) {
// If FPS is high, update physics more frequently
physicsUpdateRate = Math.max(16, physicsUpdateRate - 2);
}
}