SandGame/acid.js

66 lines
2.8 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, swap } from './actions.js';
import { explode } from './explosive.js';
export function updateAcid(x, y, cell) {
// Dissolve 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]) {
const t = grid[nx][ny].type;
if (t === 'rock' || t === 'wood' || t === 'dirt' || t === 'plant') {
if (config.acid.dissolve[t] && Math.random() < config.acid.dissolveChance) {
grid[nx][ny] = { type: 'steam', color: '#E0FFFF', updated: true }; // Turn dissolved block into steam
grid[x][y] = null; // Consume acid
return;
}
} else if (t === 'explosive') {
if (config.acid.explodeTNT) {
explode(nx, ny);
grid[x][y] = null;
return;
}
} else if (t === 'water') {
if (config.acid.dissolve.water && Math.random() < 0.01) {
grid[nx][ny].type = 'acid'; // Convert water
grid[nx][ny].color = '#32CD32';
grid[nx][ny].updated = true;
}
}
}
}
// Movement (same as water)
const below = y + 1;
if (below >= rows) return;
const belowCell = grid[x][below];
if (!belowCell) {
move(x, y, x, below);
} else if (belowCell.type === 'water' && !config.acid.dissolve.water) {
// Sink through water if not infecting (Acid is denser)
if (Math.random() < config.acid.sinkChance) {
swap(x, y, x, below);
}
} else {
// Slide sideways (can displace water if not dissolving it)
const canMoveLeft = x > 0 && (!grid[x - 1][y] || (grid[x - 1][y].type === 'water' && !config.acid.dissolve.water));
const canMoveRight = x < cols - 1 && (!grid[x + 1][y] || (grid[x + 1][y].type === 'water' && !config.acid.dissolve.water));
if (canMoveLeft && canMoveRight) {
const dir = Math.random() < 0.5 ? -1 : 1;
const target = grid[x + dir][y];
if (target && target.type === 'water') swap(x, y, x + dir, y);
else move(x, y, x + dir, y);
} else if (canMoveLeft) {
const target = grid[x - 1][y];
if (target && target.type === 'water') swap(x, y, x - 1, y);
else move(x, y, x - 1, y);
} else if (canMoveRight) {
const target = grid[x + 1][y];
if (target && target.type === 'water') swap(x, y, x + 1, y);
else move(x, y, x + 1, y);
}
}
}