diff options
author | elioat <elioat@tilde.institute> | 2024-11-02 10:47:41 -0400 |
---|---|---|
committer | elioat <elioat@tilde.institute> | 2024-11-02 10:47:41 -0400 |
commit | 55a0c1442ab7b3fa6347f6f4844a169f0e80ad7f (patch) | |
tree | 5857ae7a72687d02398e684c6af11ff8e408669c /html/broughlike | |
parent | 5951c7d3d477c6498ee4a55006f92a84ff7e37f2 (diff) | |
download | tour-55a0c1442ab7b3fa6347f6f4844a169f0e80ad7f.tar.gz |
*
Diffstat (limited to 'html/broughlike')
-rw-r--r-- | html/broughlike/broughlike.js | 76 | ||||
-rw-r--r-- | html/broughlike/config.js | 2 |
2 files changed, 75 insertions, 3 deletions
diff --git a/html/broughlike/broughlike.js b/html/broughlike/broughlike.js index 0397f9b..585ae66 100644 --- a/html/broughlike/broughlike.js +++ b/html/broughlike/broughlike.js @@ -10,6 +10,8 @@ function initializeCanvasContext() { return { ctx, canvas, tileSize }; } +const DEBUG = false; + let highScore = localStorage.getItem('highScore') || 0; const player = { x: 0, @@ -295,7 +297,7 @@ function generateWalls() { function generateItems() { items = []; const numItems = Math.floor(Math.random() * (CONFIG.ITEMS_MAX - CONFIG.ITEMS_MIN + 1)) + CONFIG.ITEMS_MIN; - for (let i = 0; i < numItems; i++) { + for (let i = 0; i < numItems;) { let itemX, itemY; do { itemX = Math.floor(Math.random() * CONFIG.GRID_SIZE); @@ -310,6 +312,7 @@ function generateItems() { const itemType = Math.random() < 0.5 ? 'diamond' : 'pentagon'; // 50% chance for each type if (isReachable(player.x, player.y, itemX, itemY)) items.push({ x: itemX, y: itemY, type: itemType }); + i++; // Only increment i if the item is reachable and actually placed on the board, this avoids levels with fewer items than ITEMS_MIN } } @@ -680,6 +683,8 @@ function resetGame() { generateEnemies(); generateItems(); render(); + if (DEBUG) + autoPlay(); } function checkPlayerAtExit() { @@ -702,6 +707,8 @@ function checkPlayerAtExit() { generateEnemies(); generateItems(); render(); + if (DEBUG) + autoPlay(); } } @@ -790,4 +797,69 @@ generateExit(); generateWalls(); generateEnemies(); generateItems(); -render(); \ No newline at end of file +render(); + + + + + +function autoPlay() { + + const playerAtExit = () => player.x === exit.x && player.y === exit.y; + const playerCanMove = (dx, dy) => isValidMove(player.x + dx, player.y + dy); + const playerCanAttack = (enemy) => Math.abs(enemy.x - player.x) + Math.abs(enemy.y - player.y) === 10; + + const movePlayerTowardsExit = () => { + const path = findPath(player, exit); + if (path.length > 1) { + const nextStep = path[1]; + const dx = nextStep.x - player.x; + const dy = nextStep.y - player.y; + if (playerCanMove(dx, dy)) { + movePlayer(dx, dy); + } + } + }; + + const attackEnemies = () => { + const enemiesInRange = enemies.filter(enemy => playerCanAttack(enemy)); + if (enemiesInRange.length > 0) { + const randomIndex = Math.floor(Math.random() * enemiesInRange.length); + const targetEnemy = enemiesInRange[randomIndex]; + const dx = targetEnemy.x - player.x; + const dy = targetEnemy.y - player.y; + movePlayer(dx, dy); + } + }; + + const collectItem = () => { + const item = items.find(item => item.x === player.x && item.y === player.y); + if (item) { + handleItemCollection(); + } + }; + + const play = () => { + if (playerAtExit()) { + return; + } + + movePlayerTowardsExit(); + + enemies.forEach(enemy => { + if (playerCanAttack(enemy)) { + attackEnemies(enemy); + } + }); + + collectItem(); + + setTimeout(play, 1000); + }; + + play(); +} + + +if (DEBUG) + autoPlay(); \ No newline at end of file diff --git a/html/broughlike/config.js b/html/broughlike/config.js index 5cf77f4..bd95035 100644 --- a/html/broughlike/config.js +++ b/html/broughlike/config.js @@ -13,7 +13,7 @@ export const COLORS = { }; export const CONFIG = { - GRID_SIZE: 6, + GRID_SIZE: 6, // 6 is about the smallest allowable level size before the path finding algorithms start to break PLAYER_HEALTH: 12, PLAYER_MAX_HEALTH: 16, PLAYER_BASE_DAMAGE: 1, |