diff options
author | elioat <elioat@tilde.institute> | 2024-07-07 15:15:43 -0400 |
---|---|---|
committer | elioat <elioat@tilde.institute> | 2024-07-07 15:15:43 -0400 |
commit | 2a8c3236e0c4b61e1e9e655960b23db315da6ad9 (patch) | |
tree | b177774b419e49347431d8b9308585d7593ce684 /js/puzzle-dungeon | |
parent | f0aeda1c7f11d0b3de7acecb52a068390fbf7347 (diff) | |
download | tour-2a8c3236e0c4b61e1e9e655960b23db315da6ad9.tar.gz |
*
Diffstat (limited to 'js/puzzle-dungeon')
-rw-r--r-- | js/puzzle-dungeon/commandHandler.js | 37 | ||||
-rw-r--r-- | js/puzzle-dungeon/game.js | 66 | ||||
-rw-r--r-- | js/puzzle-dungeon/index.html | 2 |
3 files changed, 70 insertions, 35 deletions
diff --git a/js/puzzle-dungeon/commandHandler.js b/js/puzzle-dungeon/commandHandler.js index f480d6f..21193e5 100644 --- a/js/puzzle-dungeon/commandHandler.js +++ b/js/puzzle-dungeon/commandHandler.js @@ -6,7 +6,7 @@ let processingCommands = false; export function handleCommand(command) { const [action, itemOrSteps] = command.split(' '); - if (action === 'get' || action === 'use' || isMovementCommand(action)) { + if (action === 'get' || action === 'use' || action === 'scan' || isMovementCommand(action)) { commandQueue.push({ action, itemOrSteps }); } @@ -28,6 +28,8 @@ function processCommandQueue() { 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); @@ -39,7 +41,7 @@ function handleGetCommand(item, callback) { 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 + grid[x][y] = null; // remove the item from the grid console.log(`Picked up ${item}`); flashPlayer(callback); } else { @@ -56,21 +58,23 @@ function handleGetCommand(item, 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 + player.inventory.splice(itemIndex, 1); // remove the item from the inventory if (item === 'potion') { - player.health += 1; // Increase health by 1 + 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); // Increase power by 2, up to a max of 10 + 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, health is now ${player.health}`); + console.log(`Tried to use ${item} but it's not in inventory. Ouch! Health is now ${player.health}`); if (player.health <= 0) { alertGameOver(); return; @@ -79,6 +83,23 @@ function handleUseCommand(item, 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; @@ -103,7 +124,7 @@ function handleMovement(direction, steps, callback) { player.steps += 1; if (player.steps % 8 === 0) { - player.power = Math.max(player.power - 1, 0); // Decrease endurance but not below 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}`); @@ -118,7 +139,7 @@ function handleMovement(direction, steps, callback) { drawGrid(grid); updatePlayerStatus(); stepCount++; - }, 500); // Adjust the interval time for speed of animation + }, 500); // this interval controls the speed of the animation } function flashPlayer(callback) { diff --git a/js/puzzle-dungeon/game.js b/js/puzzle-dungeon/game.js index 449f03f..d80c28f 100644 --- a/js/puzzle-dungeon/game.js +++ b/js/puzzle-dungeon/game.js @@ -7,9 +7,11 @@ export let player = { level: 0, steps: 0, par: 0, + didScan: false, flashing: false }; let targetPosition = { x: 0, y: 0 }; +export let levelPar; function createGrid(rows, cols) { let grid = []; @@ -23,7 +25,6 @@ function createGrid(rows, cols) { return grid; } -export let levelPar; function generatePar(level) { let par; if (level < 6) { @@ -31,7 +32,7 @@ function generatePar(level) { } else { par = Math.floor(Math.random() * 7) + 1; } - document.getElementById('par').textContent = `Par: ${player.par} of ${par}`; + document.getElementById('par').textContent = `Par: ${par}`; return par; } @@ -47,7 +48,7 @@ function generateCollectables(grid, numCollectables) { do { x = Math.floor(Math.random() * grid.length); y = Math.floor(Math.random() * grid[0].length); - } while (x === 0 && y === 0); // Ensure no items at (0,0) + } while (x === 0 && y === 0); // don't put items at (0,0) const type = collectableTypes[Math.floor(Math.random() * collectableTypes.length)]; grid[x][y] = { type, color: collectableColors[type] }; } @@ -59,19 +60,30 @@ function generateDamagingSpaces(grid, numSpaces) { do { x = Math.floor(Math.random() * grid.length); y = Math.floor(Math.random() * grid[0].length); - } while (x === 0 && y === 0); // Ensure no damaging spaces at (0,0) + } while (x === 0 && y === 0); // don't put damaging stuff at (0,0) grid[x][y] = { type: 'damage', color: 'tomato' }; } } +function generateTraps(grid, numTraps) { + for (let i = 0; i < numTraps; i++) { + let x, y; + do { + x = Math.floor(Math.random() * grid.length); + y = Math.floor(Math.random() * grid[0].length); + } while (x === 0 && y === 0); // don't be an asshole + grid[x][y] = { type: 'trap', color: 'rgba(0,0,0,0)' }; + } +} + function generateTargetBlock(grid) { let x, y; do { x = Math.floor(Math.random() * grid.length); y = Math.floor(Math.random() * grid[0].length); - } while (x === 0 && y === 0); // Ensure no target block at (0,0) - grid[x][y] = { type: 'target', color: 'lightgreen' }; // Target block represented by 'X' - targetPosition = { x, y }; // Store the target block position + } while (x === 0 && y === 0); // don't make it too easy + grid[x][y] = { type: 'target', color: 'lightgreen' }; // x marks the spot! + targetPosition = { x, y }; } export function drawGrid(grid) { @@ -99,6 +111,7 @@ export function drawGrid(grid) { if (cell.type === 'battery') char = 'b'; if (cell.type === 'damage') char = '!'; if (cell.type === 'target') char = '#'; + if (cell.type === 'trap' && player.didScan) char = 't'; ctx.fillText(char, x * cellSize + cellSize / 2, y * cellSize + cellSize / 2); } }); @@ -115,20 +128,22 @@ export function drawGrid(grid) { } export function initializeGame() { - grid = createGrid(10, 10); // Reset grid + grid = createGrid(10, 10); // reset grid levelPar = generatePar(player.level); - generateCollectables(grid, 10); // Adjust the number as needed - generateDamagingSpaces(grid, Math.floor(Math.random() * 7) + 1); // Random number of damaging spaces, no more than 7 - generateTargetBlock(grid); // Add target block + generateCollectables(grid, 10); // how many collectables? + generateDamagingSpaces(grid, Math.floor(Math.random() * 7) + 1); // random number of damaging spaces, no more than 7 + generateTargetBlock(grid); + generateTraps(grid, Math.floor(Math.random() * 3) + 1); // random number of traps, no more than 3 if (player.level === 0) { - player.position = { x: 0, y: 0 }; // Reset player position - player.inventory = []; // Clear inventory - player.health = 10; // Reset health - player.power = 10; // Reset endurance + player.inventory = []; + player.health = 10; + player.power = 10; } - player.steps = 0; // Reset steps + player.position = { x: 0, y: 0 }; // player always starts at 0,0 + player.steps = 0; // steps are counted per-level + player.didScan = false; // scans are counted per-level resizeCanvas(); drawGrid(grid); updatePlayerStatus(); @@ -175,6 +190,14 @@ function checkForDamageOrTarget() { } flashPlayer(); } + } else if (cell && cell.type === 'trap') { + const item = player.inventory.pop(); + if (item) { + console.log(`Stepped on trap, removed ${item} from inventory`); + } else { + player.power -= Math.floor(Math.random() * 3) + 1; + console.log('Stepped on trap, drained some power'); + } } else if (x === targetPosition.x && y === targetPosition.y) { player.level += 1; console.log(`Reached target block, level is now ${player.level}`); @@ -188,13 +211,4 @@ export function alertGameOver() { initializeGame(); } -function flashPlayer(callback) { - player.flashing = true; - drawGrid(grid); - setTimeout(() => { - player.flashing = false; - drawGrid(grid); - updatePlayerStatus(); - if (callback) callback(); - }, 250); // Flash duration -} + diff --git a/js/puzzle-dungeon/index.html b/js/puzzle-dungeon/index.html index 2caa203..f8aa7ad 100644 --- a/js/puzzle-dungeon/index.html +++ b/js/puzzle-dungeon/index.html @@ -54,7 +54,7 @@ document.getElementById('commandForm').addEventListener('submit', function(event) { event.preventDefault(); player.par = player.par + 1; - console.log(`Player command count, ${player.par}, ${levelPar}`); + console.log(`Par: ${levelPar}`); const commands = document.getElementById('commands').value; if (player.par === levelPar) { alertGameOver(); |