sandsim/js/physics.js

57 lines
2.3 KiB
JavaScript

// Physics simulation functions
function updatePhysics() {
// Get visible chunks
const visibleChunks = getVisibleChunks();
// Increment fire update counter
fireUpdateCounter++;
// Process each visible chunk
for (const { chunkX, chunkY, isVisible } of visibleChunks) {
// Skip physics calculations for chunks that are not visible
if (!isVisible) continue;
const chunk = getOrCreateChunk(chunkX, chunkY);
// Process from bottom to top, right to left for correct gravity simulation
for (let y = CHUNK_SIZE - 1; y >= 0; y--) {
// Alternate direction each row for more natural flow
const startX = y % 2 === 0 ? 0 : CHUNK_SIZE - 1;
const endX = y % 2 === 0 ? CHUNK_SIZE : -1;
const step = y % 2 === 0 ? 1 : -1;
for (let x = startX; x !== endX; x += step) {
const index = y * CHUNK_SIZE + x;
const type = chunk[index];
if (type === EMPTY || type === STONE || type === WOOD || type === LEAF) continue;
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);
}
}
}
}
}