SandGame/fire.js

63 lines
2.1 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 updateFire(x, y, cell) {
// Check lifespan
if (cell.lifespan !== undefined) {
cell.lifespan--;
if (cell.lifespan <= 0) {
grid[x][y] = null;
return;
}
} else if (Math.random() < config.fire.burnChance) {
grid[x][y] = null;
return;
}
// Extinguish if trapped
let trapped = true;
const checkNeighbors = [[0, 1], [0, -1], [1, 0], [-1, 0]];
for (let n of checkNeighbors) {
const nx = x + n[0], ny = y + n[1];
if (nx >= 0 && nx < cols && ny >= 0 && ny < rows) {
if (!grid[nx][ny] || grid[nx][ny].type === 'wood' || grid[nx][ny].type === 'plant' || grid[nx][ny].type === 'explosive') {
trapped = false;
break;
}
}
}
if (trapped) {
grid[x][y] = null;
return;
}
// Interact with neighbors
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]) {
if (grid[nx][ny].type === 'water') {
grid[x][y] = null; // Extinguished
grid[nx][ny] = { type: 'steam', color: '#E0FFFF', updated: true };
return;
}
if (grid[nx][ny].type === 'plant') {
if (Math.random() < config.plant.burnChance) {
grid[nx][ny] = { type: 'fire', color: '#FF5500', updated: true };
}
}
if (grid[nx][ny].type === 'wood') {
if (Math.random() < config.wood.burnChance) {
grid[nx][ny] = { type: 'fire', color: '#FF5500', updated: true };
}
}
}
}
// Move randomly (rise)
const dx = Math.floor(Math.random() * 3) - 1;
const dy = Math.floor(Math.random() * 2) - 1;
const nx = x + dx, ny = y + dy;
if (nx >= 0 && nx < cols && ny >= 0 && ny < rows && !grid[nx][ny]) move(x, y, nx, ny);
}