SandGame/dirt.js

28 lines
939 B
JavaScript
Raw Normal View History

2026-10-22 14:05:51 +00:00
import { grid, cols, rows } from './state.js';
import { move } from './actions.js';
export function updateDirt(x, y, cell) {
const below = y + 1;
if (below >= rows) return;
const belowCell = grid[x][below];
if (!belowCell) {
move(x, y, x, below);
} else if (belowCell.type === 'water' || belowCell.type === 'acid') {
// Fall through water or acid (slower)
if (Math.random() < 0.2) {
grid[x][below] = cell;
grid[x][y] = belowCell;
cell.updated = true;
belowCell.updated = true;
}
} else {
// Try sliding left or right
const left = x > 0 && !grid[x - 1][below];
const right = x < cols - 1 && !grid[x + 1][below];
if (left && right) move(x, y, Math.random() < 0.5 ? x - 1 : x + 1, below);
else if (left) move(x, y, x - 1, below);
else if (right) move(x, y, x + 1, below);
}
}