SandGame/steam.js

66 lines
2.1 KiB
JavaScript
Raw Permalink 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 { createCell } from './physics.js';
2026-10-22 14:05:51 +00:00
export function updateSteam(x, y, cell) {
const above = y - 1;
// Check if we can move up
if (above >= 0) {
const cellAbove = grid[x][above];
if (!cellAbove) {
move(x, y, x, above);
// Turbulence while rising
if (Math.random() < 0.4) {
const dir = Math.random() < 0.5 ? -1 : 1;
const nx = x + dir;
if (nx >= 0 && nx < cols && !grid[nx][above]) {
2026-10-22 14:05:51 +00:00
move(x, above, nx, above);
}
}
return;
} else if (cellAbove.type === 'water' || cellAbove.type === 'acid' || cellAbove.type === 'oil' || cellAbove.type === 'dirt' || cellAbove.type === 'sand') {
swap(x, y, x, above);
return;
2026-10-22 14:05:51 +00:00
}
} else {
// Escapes into sky at y < 0
grid[x][y] = null;
2026-10-22 14:05:51 +00:00
return;
}
// If blocked up, try sideways
const leftIdx = x - 1;
const rightIdx = x + 1;
const leftFree = leftIdx >= 0 && (!grid[leftIdx][y] || isPermeable(grid[leftIdx][y]));
const rightFree = rightIdx < cols && (!grid[rightIdx][y] || isPermeable(grid[rightIdx][y]));
2026-10-22 14:05:51 +00:00
if (leftFree && rightFree) {
const dir = Math.random() < 0.5 ? -1 : 1;
const nx = x + dir;
executeSteamMove(x, y, nx, y);
2026-10-22 14:05:51 +00:00
} else if (leftFree) {
executeSteamMove(x, y, leftIdx, y);
2026-10-22 14:05:51 +00:00
} else if (rightFree) {
executeSteamMove(x, y, rightIdx, y);
2026-10-22 14:05:51 +00:00
}
// Condense into water
if (Math.random() < config.steam.condenseChance) {
grid[x][y] = createCell('water');
}
}
function isPermeable(cell) {
return cell && (cell.type === 'water' || cell.type === 'acid' || cell.type === 'oil' || cell.type === 'dirt' || cell.type === 'sand');
}
function executeSteamMove(x1, y1, x2, y2) {
if (grid[x2][y2]) {
swap(x1, y1, x2, y2);
} else {
move(x1, y1, x2, y2);
2026-10-22 14:05:51 +00:00
}
}