66 lines
No EOL
2.1 KiB
JavaScript
66 lines
No EOL
2.1 KiB
JavaScript
import { grid, cols, rows } from './state.js';
|
|
import { config } from './config.js';
|
|
import { move, swap } from './actions.js';
|
|
import { createCell } from './physics.js';
|
|
|
|
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]) {
|
|
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;
|
|
}
|
|
} else {
|
|
// Escapes into sky at y < 0
|
|
grid[x][y] = null;
|
|
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]));
|
|
|
|
if (leftFree && rightFree) {
|
|
const dir = Math.random() < 0.5 ? -1 : 1;
|
|
const nx = x + dir;
|
|
executeSteamMove(x, y, nx, y);
|
|
} else if (leftFree) {
|
|
executeSteamMove(x, y, leftIdx, y);
|
|
} else if (rightFree) {
|
|
executeSteamMove(x, y, rightIdx, y);
|
|
}
|
|
|
|
// 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);
|
|
}
|
|
} |