38 lines
No EOL
1.3 KiB
JavaScript
38 lines
No EOL
1.3 KiB
JavaScript
import { grid, cols, rows } from './state.js';
|
|
import { config } from './config.js';
|
|
import { createCell } from './physics.js';
|
|
|
|
const dxs = [-1, 1, 0, 0];
|
|
const dys = [0, 0, -1, 1];
|
|
|
|
export function updateGrass(x, y, cell) {
|
|
// Spread along surface dirt/sand
|
|
if (Math.random() < (config.grass?.growChance || 0.03)) {
|
|
const dir = Math.floor(Math.random() * 4);
|
|
const nx = x + dxs[dir];
|
|
const ny = y + dys[dir];
|
|
|
|
if (nx >= 0 && nx < cols && ny >= 0 && ny < rows && grid[nx][ny]) {
|
|
const target = grid[nx][ny];
|
|
if (target.type === 'dirt' || target.type === 'sand') {
|
|
// Only colonize dirt if it has air/empty space nearby (surface grass)
|
|
const aboveDirt = ny - 1;
|
|
if (aboveDirt >= 0 && !grid[nx][aboveDirt]) {
|
|
grid[nx][ny] = createCell('grass');
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Sprout blade feelers upwards into open air
|
|
if (Math.random() < (config.grass?.growChance || 0.03) * 0.4) {
|
|
const r = Math.random();
|
|
const dx = r < 0.3 ? -1 : (r > 0.7 ? 1 : 0);
|
|
const nx = x + dx;
|
|
const ny = y - 1;
|
|
if (nx >= 0 && nx < cols && ny >= 0 && !grid[nx][ny]) {
|
|
grid[nx][ny] = createCell('grass');
|
|
}
|
|
}
|
|
} |