SandGame/plant.js

48 lines
No EOL
1.5 KiB
JavaScript

import { grid, cols, rows } from './state.js';
import { config } from './config.js';
import { createCell } from './physics.js';
const dxs = [0, 0, -1, 1];
const dys = [1, -1, 0, 0];
export function updatePlant(x, y, cell) {
// Woodification (stalk formation under weight)
let weight = 0;
const requiredWeight = 5;
for (let i = 1; i <= requiredWeight; i++) {
if (y - i < 0) break;
const above = grid[x][y - i];
if (above && (above.type === 'plant' || above.type === 'wood')) {
weight++;
} else {
break;
}
}
if (weight >= requiredWeight && Math.random() < config.plant.woodificationChance) {
grid[x][y] = createCell('wood');
return;
}
// Roots: infects touching dirt into plant
if (Math.random() < config.plant.growChance * 0.15) {
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] && grid[nx][ny].type === 'dirt') {
grid[nx][ny] = createCell('plant');
return;
}
}
// Organic upward branch growth towards light
if (Math.random() < config.plant.growChance) {
const r = Math.random();
const dx = r < 0.2 ? -1 : (r > 0.8 ? 1 : 0);
const nx = x + dx;
const ny = y - 1;
if (nx >= 0 && nx < cols && ny >= 0 && !grid[nx][ny]) {
grid[nx][ny] = createCell('plant');
}
}
}