84 lines
2.7 KiB
JavaScript
84 lines
2.7 KiB
JavaScript
|
|
import { grid, cols, rows } from './state.js';
|
||
|
|
import { config } from './config.js';
|
||
|
|
import { move, swap } from './actions.js';
|
||
|
|
|
||
|
|
export function updateSteam(x, y, cell) {
|
||
|
|
const above = y - 1;
|
||
|
|
|
||
|
|
// Check if we can move up
|
||
|
|
let canMoveUp = false;
|
||
|
|
if (above >= 0) {
|
||
|
|
const cellAbove = grid[x][above];
|
||
|
|
if (!cellAbove) {
|
||
|
|
canMoveUp = true;
|
||
|
|
} else if (cellAbove.type === 'water' || cellAbove.type === 'acid' || cellAbove.type === 'dirt') {
|
||
|
|
swap(x, y, x, above);
|
||
|
|
// Add turbulence to the liquid falling through
|
||
|
|
cellAbove.vx = (cellAbove.vx || 0) + (Math.random() - 0.5);
|
||
|
|
cellAbove.vy = (cellAbove.vy || 0) + 0.5;
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (canMoveUp) {
|
||
|
|
move(x, y, x, above);
|
||
|
|
// Random jitter for organic look while rising
|
||
|
|
if (Math.random() < 0.5) {
|
||
|
|
const dir = Math.random() < 0.5 ? -1 : 1;
|
||
|
|
const nx = x + dir;
|
||
|
|
if (nx >= 0 && nx < cols) {
|
||
|
|
const neighbor = grid[nx][above];
|
||
|
|
if (!neighbor) {
|
||
|
|
move(x, above, nx, above);
|
||
|
|
} else if (neighbor.type === 'water' || neighbor.type === 'acid' || neighbor.type === 'dirt') {
|
||
|
|
swap(x, above, nx, above);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// If blocked up, try sideways
|
||
|
|
const leftIdx = x - 1;
|
||
|
|
const rightIdx = x + 1;
|
||
|
|
|
||
|
|
let leftFree = false;
|
||
|
|
let rightFree = false;
|
||
|
|
|
||
|
|
if (leftIdx >= 0) {
|
||
|
|
const c = grid[leftIdx][y];
|
||
|
|
if (!c || c.type === 'water' || c.type === 'acid' || c.type === 'dirt') leftFree = true;
|
||
|
|
}
|
||
|
|
if (rightIdx < cols) {
|
||
|
|
const c = grid[rightIdx][y];
|
||
|
|
if (!c || c.type === 'water' || c.type === 'acid' || c.type === 'dirt') rightFree = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
let movedSideways = false;
|
||
|
|
if (leftFree && rightFree) {
|
||
|
|
const dir = Math.random() < 0.5 ? -1 : 1;
|
||
|
|
const nx = x + dir;
|
||
|
|
const target = grid[nx][y];
|
||
|
|
if (target) swap(x, y, nx, y);
|
||
|
|
else move(x, y, nx, y);
|
||
|
|
movedSideways = true;
|
||
|
|
} else if (leftFree) {
|
||
|
|
const target = grid[leftIdx][y];
|
||
|
|
if (target) swap(x, y, leftIdx, y);
|
||
|
|
else move(x, y, leftIdx, y);
|
||
|
|
movedSideways = true;
|
||
|
|
} else if (rightFree) {
|
||
|
|
const target = grid[rightIdx][y];
|
||
|
|
if (target) swap(x, y, rightIdx, y);
|
||
|
|
else move(x, y, rightIdx, y);
|
||
|
|
movedSideways = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Condense logic
|
||
|
|
if ((above < 0 || (!movedSideways && grid[x][above])) && Math.random() < config.steam.condenseChance) {
|
||
|
|
cell.type = 'water';
|
||
|
|
cell.color = '#0000FF';
|
||
|
|
} else if (above < 0) {
|
||
|
|
grid[x][y] = null;
|
||
|
|
}
|
||
|
|
}
|