import { player, grid, drawGrid, updatePlayerPosition, updatePlayerStatus, alertGameOver } from './game.js';
let commandQueue = [];
let processingCommands = false;
export function handleCommand(command) {
const [action, itemOrSteps] = command.split(' ');
if (action === 'get' || action === 'use' || action === 'scan' || isMovementCommand(action)) {
commandQueue.push({ action, itemOrSteps });
}
if (!processingCommands) {
processCommandQueue();
}
}
function processCommandQueue() {
if (commandQueue.length === 0) {
processingCommands = false;
return;
}
processingCommands = true;
const { action, itemOrSteps } = commandQueue.shift();
if (action === 'get') {
handleGetCommand(itemOrSteps, processCommandQueue);
} else if (action === 'use') {
handleUseCommand(itemOrSteps, processCommandQueue);
} else if (action === 'scan') {
handleScanCommand(processCommandQueue);
} else if (isMovementCommand(action)) {
const steps = parseInt(itemOrSteps, 10) || 1;
handleMovement(action, steps, processCommandQueue);
}
}
function handleGetCommand(item, callback) {
const { x, y } = player.position;
const cell = grid[x][y];
if (cell && cell.type === item) {
player.inventory.push(cell.type);
grid[x][y] = null; // remove the item from the grid
console.log(`Picked up ${item}`);
flashPlayer(callback);
} else {
player.health -= 1;
console.log(`Tried to get ${item} but it's not there, health is now ${player.health}`);
if (player.health <= 0) {
alertGameOver();
return;
}
flashPlayer(callback);
}
}
function handleUseCommand(item, callback) {
const itemIndex = player.inventory.indexOf(item);
if (itemIndex > -1) {
player.inventory.splice(itemIndex, 1); // remove the item from the inventory
if (item === 'potion') {
player.health += 1; // MOAR HEALTH (by 1)
console.log(`Used ${item}, health is now ${player.health}`);
updatePlayerStatus();
flashPlayer(callback);
} else if (item === 'battery') {
player.power = Math.min(player.power + 2, (10 + player.level)); // Increase power by 2, up to a max of 10 + the current level. This may be tooooo gigantic a number
console.log(`Used ${item}, power is now ${player.power}`);
updatePlayerStatus();
flashPlayer(callback);
} else {
callback();
}
} else {
player.health -= 1;
console.log(`Tried to use ${item} but it's not in inventory. Ouch! Health is now ${player.health}`);
if (player.health <= 0) {
alertGameOver();
return;
}
flashPlayer(callback);
}
}
function handleScanCommand(callback) {
if (player.power > 0) {
player.didScan = true;
player.power -= 3;
drawGrid(grid); // redraw the grid to show the revealed traps
updatePlayerStatus();
} else {
player.health -= 1;
console.log(`Tried to scan but power is depleted, ouch! Health is now ${player.health}`);
if (player.health <= 0) {
alertGameOver();
return;
}
}
callback(); // continue processing commands
}
function handleMovement(direction, steps, callback) {
let stepCount = 0;
const moveInterval = setInterval(() => {
if (stepCount >= steps) {
clearInterval(moveInterval);
callback();
return;
}
const { x, y } = player.position;
if (direction === 'up' && y > 0) {
updatePlayerPosition(x, y - 1);
} else if (direction === 'down' && y < grid[0].length - 1) {
updatePlayerPosition(x, y + 1);
} else if (direction === 'left' && x > 0) {
updatePlayerPosition(x - 1, y);
} else if (direction === 'right' && x < grid.length - 1) {
updatePlayerPosition(x + 1, y);
}
console.log(`Moved ${direction}, new position is (${player.position.x}, ${player.position.y})`);
player.steps += 1;
if (player.steps % 8 === 0) {
player.power = Math.max(player.power - 1, 0); // decrease power but, don't let it go below 0
if (player.power <= 0) {
player.health -= 1;
console.log(`Power depleted, health is now ${player.health}`);
if (player.health <= 0) {
alertGameOver();
return;
}
}
console.log(`Power decreased, power is now ${player.power}`);
}
drawGrid(grid);
updatePlayerStatus();
stepCount++;
}, 500); // this interval controls the speed of the animation
}
function flashPlayer(callback) {
player.flashing = true;
drawGrid(grid);
setTimeout(() => {
player.flashing = false;
drawGrid(grid);
updatePlayerStatus();
if (callback) callback();
}, 250); // Flash duration
}
function isMovementCommand(action) {
return ['up', 'down', 'left', 'right'].includes(action);
}