SandGame/grass.js

31 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';
export function updateGrass(x, y, cell) {
// Static, but infects dirt
if (Math.random() < config.grass.growChance * 0.1) {
const neighbors = [[0, 1], [0, -1], [1, 0], [-1, 0]];
const n = neighbors[Math.floor(Math.random() * neighbors.length)];
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 === 'dirt') {
grid[nx][ny].type = 'grass';
grid[nx][ny].color = '#00FF00';
grid[nx][ny].updated = true;
}
}
// Organic Growth (Feeler branches)
if (Math.random() < config.grass.growChance) {
const growOptions = [
{ dx: 0, dy: -1 }, // Up
{ dx: -1, dy: -1 }, // Up-Left
{ dx: 1, dy: -1 } // Up-Right
];
const opt = growOptions[Math.floor(Math.random() * growOptions.length)];
const nx = x + opt.dx, ny = y + opt.dy;
if (nx >= 0 && nx < cols && ny >= 0 && ny < rows && !grid[nx][ny]) {
grid[nx][ny] = { type: 'grass', color: '#00FF00', updated: true };
}
}
}