From 2768352165dea9aa273a500885d4cea71becbdcd Mon Sep 17 00:00:00 2001 From: elioat Date: Thu, 28 Dec 2023 18:11:21 -0500 Subject: cooking with fire --- js/game-frame/README.md | 4 +- js/game-frame/game.js | 95 ++++++++++++++++++++++++++++++++++++++++++++++-- js/game-frame/index.html | 6 ++- 3 files changed, 99 insertions(+), 6 deletions(-) diff --git a/js/game-frame/README.md b/js/game-frame/README.md index cd787f2..91ae7e6 100644 --- a/js/game-frame/README.md +++ b/js/game-frame/README.md @@ -4,8 +4,8 @@ My *hope* is to make this a sort of generic starting point to prototype little e Needs to support: -- 2d, grid-based movement -- collectable items +- [x] 2d, grid-based movement +- [x] collectable items - configurable map - panning camera to follow the character across a map larger than the default viewport diff --git a/js/game-frame/game.js b/js/game-frame/game.js index 935263d..f63b835 100644 --- a/js/game-frame/game.js +++ b/js/game-frame/game.js @@ -4,6 +4,9 @@ const ctx = canvas.getContext('2d'); canvas.width = 512; canvas.height = 512; + + + // Create the player const player = { x: 0, @@ -14,10 +17,24 @@ const player = { color: 'blue', spriteUrl: "chickadee.svg" }; + +// If you wanna have a sprite, you need to create an image object const playerSprite = new Image(); playerSprite.src = player.spriteUrl; +// Empty player inventory +player.inventory = []; + +// Ability to collect items +player.collectItem = function(item) { + this.inventory.push(item); +} + + + + +// MAP const TILE_SIZE = 64; // Create the map const map = [ @@ -31,10 +48,49 @@ const map = [ [{ walkable: true, color: 'pink' }, { walkable: false, color: 'grey' }, { walkable: true, color: 'pink' }, { walkable: true, color: 'pink' }, { walkable: true, color: 'pink' }, { walkable: true, color: 'pink' }, { walkable: true, color: 'pink' }, { walkable: true, color: 'pink' }] ]; -function displayCoordinates() { - document.getElementById("coordinates").innerHTML = "x: " + player.x + " y: " + player.y; + + + +// ITEMS +// Define the Item object +function Item(name, x, y, type, color) { + this.name = name; + this.x = x; + this.y = y; + this.type = type; + this.color = color; } +// Create some bespoke items +let items = [ + new Item('helmet', 16, 16, 'clothes', 'red'), + new Item('banana', 24, 128, 'food', 'yellow'), + new Item('bucket', 128, 256, 'object', 'green'), + new Item('big key', 216, 216, 'key', 'cyan'), +]; + + + + + +// Funtion to display the player's stats, useful for debugging +function displayStats() { + document.getElementById("stats").innerHTML = ""; + for (let stat in player) { + if (typeof player[stat] === "string" || typeof player[stat] === "number") { + document.getElementById("stats").innerHTML += stat + ": " + player[stat] + "
"; + } + } + document.getElementById("stats").innerHTML += "Inventory: "; + for (let i = 0; i < player.inventory.length; i++) { + document.getElementById("stats").innerHTML += player.inventory[i].name + ", "; + } +} + + + + + // Game loop function gameLoop() { @@ -63,9 +119,24 @@ function gameLoop() { // context.drawImage(playerSprite, player.x, player.y, player.width, player.height); + // Draw items + items.forEach(item => { + ctx.fillStyle = item.color; + ctx.fillRect(item.x, item.y, 8, 8); + }); + + for (let i = 0; i < items.length; i++) { + if (player.x === items[i].x && player.y === items[i].y) { + player.collectItem(items[i]); + items.splice(i, 1); // Remove the item from the map + i--; + } + } + + // Call the game loop again next frame requestAnimationFrame(gameLoop); - displayCoordinates(); + displayStats(); } // Start the game loop @@ -101,4 +172,22 @@ window.addEventListener('keydown', (e) => { player.x = newX; player.y = newY; } +}); + + +canvas.addEventListener('click', function(event) { + // Calculate the mouse position + var rect = canvas.getBoundingClientRect(); + var mouseX = Math.round(event.clientX - rect.left); + var mouseY = Math.round(event.clientY - rect.top); + + // Calculate the map coordinates + var mapX = Math.floor(mouseX / TILE_SIZE); + var mapY = Math.floor(mouseY / TILE_SIZE); + + // Write the map coordinates to the DOM element + document.getElementById('coordinates').innerText = 'Map grid: x ' + mapX + ', y ' + mapY; + + // Write the canvas coordinates to the DOM element + document.getElementById('canvasCoordinates').innerText = 'Canvas: x ' + mouseX + ', y ' + mouseY; }); \ No newline at end of file diff --git a/js/game-frame/index.html b/js/game-frame/index.html index 6585662..a18d1d2 100644 --- a/js/game-frame/index.html +++ b/js/game-frame/index.html @@ -8,7 +8,11 @@
-

+ +

-- cgit 1.4.1-2-gfad0 n21'>21 22 23 24 25 26 27 28 29 30 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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360