SandGame/plant.js
2026-10-22 09:05:51 -05:00

57 lines
No EOL
1.9 KiB
JavaScript

import { grid, cols, rows } from './state.js';
import { config } from './config.js';
export function updatePlant(x, y, cell) {
// Woodification (Stalk formation)
// Only turn to wood if supporting a column of plants (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) {
if (Math.random() < config.plant.woodificationChance) {
cell.type = 'wood';
cell.color = '#8B4513';
cell.updated = true;
return;
}
}
// Roots (Infects dirt)
if (Math.random() < config.plant.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 = 'plant';
grid[nx][ny].color = '#228B22';
grid[nx][ny].updated = true;
}
}
// Organic Growth (Towards Sun)
if (Math.random() < config.plant.growChance) {
const growOptions = [
{ dx: 0, dy: -1 }, // Up
{ dx: 0, dy: -1 }, // Up
{ dx: 0, dy: -1 }, // Up
{ 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: 'plant', color: '#228B22', updated: true };
}
}
}