about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorelioat <elioat@tilde.institute>2024-11-02 10:47:41 -0400
committerelioat <elioat@tilde.institute>2024-11-02 10:47:41 -0400
commit55a0c1442ab7b3fa6347f6f4844a169f0e80ad7f (patch)
tree5857ae7a72687d02398e684c6af11ff8e408669c
parent5951c7d3d477c6498ee4a55006f92a84ff7e37f2 (diff)
downloadtour-55a0c1442ab7b3fa6347f6f4844a169f0e80ad7f.tar.gz
*
-rw-r--r--html/broughlike/broughlike.js76
-rw-r--r--html/broughlike/config.js2
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,
id='n31' href='#n31'>31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161