SandGame/water.js

34 lines
1.2 KiB
JavaScript
Raw Normal View History

2026-10-22 14:05:51 +00:00
import { grid, cols, rows } from './state.js';
import { config } from './config.js';
import { move } from './actions.js';
export function updateWater(x, y, cell) {
// Check for grass interaction (Growth)
const neighbors = [[0, 1], [0, -1], [1, 0], [-1, 0]];
for (let n of neighbors) {
const nx = x + n[0], ny = y + n[1];
if (nx >= 0 && nx < cols && ny >= 0 && ny < rows && grid[nx][ny] && grid[nx][ny].type === 'plant') {
if (Math.random() < 0.005) { // Slower, but faster than dirt
cell.type = 'plant';
cell.color = '#228B22';
}
return;
}
}
const below = y + 1;
if (below >= rows) return;
const belowCell = grid[x][below];
if (!belowCell) {
move(x, y, x, below);
} else if (Math.random() < config.water.flowChance) {
// Slide or flow sideways
const left = x > 0 && !grid[x - 1][y];
const right = x < cols - 1 && !grid[x + 1][y];
if (left && right) move(x, y, Math.random() < 0.5 ? x - 1 : x + 1, y);
else if (left) move(x, y, x - 1, y);
else if (right) move(x, y, x + 1, y);
}
}