diff options
Diffstat (limited to 'js/puzzle-dungeon/commandHandler.js')
-rw-r--r-- | js/puzzle-dungeon/commandHandler.js | 152 |
1 files changed, 131 insertions, 21 deletions
diff --git a/js/puzzle-dungeon/commandHandler.js b/js/puzzle-dungeon/commandHandler.js index 12140c2..b6a8246 100644 --- a/js/puzzle-dungeon/commandHandler.js +++ b/js/puzzle-dungeon/commandHandler.js @@ -1,28 +1,138 @@ -const createCommandHandler = () => { - const commands = {}; +// commandHandler.js +import { player, grid, drawGrid, updatePlayerPosition, updatePlayerStatus, alertGameOver } from './game.js'; - const registerCommand = (name, callback) => { - commands[name] = callback; - }; +let commandQueue = []; +let processingCommands = false; - const executeCommand = (parsedCommand) => { - const { command, args } = parsedCommand; - if (commands[command]) { - commands[command](...args); +export function handleCommand(command) { + const [action, itemOrSteps] = command.split(' '); + + if (action === 'get' || action === 'use' || 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 (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; // Increase health by 1 + console.log(`Used ${item}, health is now ${player.health}`); + flashPlayer(callback); + } else if (item === 'food') { + player.endurance = Math.min(player.endurance + 2, 10); // Increase endurance by 2, up to a max of 10 + console.log(`Used ${item}, endurance is now ${player.endurance}`); + flashPlayer(callback); } else { - console.error(`Unknown command: ${command}`); + callback(); + } + } else { + player.health -= 1; + console.log(`Tried to use ${item} but it's not in inventory, health is now ${player.health}`); + if (player.health <= 0) { + alertGameOver(); + return; + } + flashPlayer(callback); + } +} + +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.endurance = Math.max(player.endurance - 1, 0); // Decrease endurance but not below 0 + if (player.endurance <= 0) { + player.health -= 1; + console.log(`Endurance depleted, health is now ${player.health}`); + if (player.health <= 0) { + alertGameOver(); + return; + } + } + console.log(`Endurance decreased, endurance is now ${player.endurance}`); } - }; - const executeCommands = (parsedCommands) => { - parsedCommands.forEach(cmd => executeCommand(cmd)); - }; + drawGrid(grid); + updatePlayerStatus(); + stepCount++; + }, 500); // Adjust the interval time for speed of animation +} - return { - registerCommand, - executeCommand, - executeCommands - }; -}; +function flashPlayer(callback) { + player.flashing = true; + drawGrid(grid); + setTimeout(() => { + player.flashing = false; + drawGrid(grid); + updatePlayerStatus(); + if (callback) callback(); + }, 250); // Flash duration +} -export default createCommandHandler; +function isMovementCommand(action) { + return ['up', 'down', 'left', 'right'].includes(action); +} |