From 37b9b5ad35f2d49f02914b4df82c2432883d6412 Mon Sep 17 00:00:00 2001 From: elioat Date: Thu, 28 Dec 2023 20:04:48 -0500 Subject: * --- js/game-frame/game.js | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'js/game-frame/game.js') diff --git a/js/game-frame/game.js b/js/game-frame/game.js index f63b835..fabc6bf 100644 --- a/js/game-frame/game.js +++ b/js/game-frame/game.js @@ -70,6 +70,23 @@ let items = [ ]; +// Pushable items +// Define the PushableItem class +function PushableItem(x, y, width, height, color) { + this.x = x; + this.y = y; + this.width = width; + this.height = height; + this.color = color; +} + +// Create some pushable items +let pushableItems = [ + new PushableItem(24, 48, player.width, player.height, 'green'), + new PushableItem(48, 24, player.width, player.height, 'green'), +]; + + @@ -92,6 +109,9 @@ function displayStats() { + + + // Game loop function gameLoop() { // Clear the canvas @@ -134,6 +154,40 @@ function gameLoop() { } + + // Draw pushable items + pushableItems.forEach(item => { + ctx.fillStyle = item.color; + ctx.fillRect(item.x, item.y, item.width, item.height); + }); + + // Handle pushable items + // FIXME: this doesn't work correctly + for (let i = 0; i < pushableItems.length; i++) { + if (player.x === pushableItems[i].x && player.y === pushableItems[i].y) { + // Calculate the tile coordinates + const tileX = Math.floor(pushableItems[i].x / TILE_SIZE); + const tileY = Math.floor(pushableItems[i].y / TILE_SIZE); + + // Calculate the new position + let newX = pushableItems[i].x + player.step; + let newY = pushableItems[i].y + player.step; + + // Check if the new tile is walkable + if (map[tileY] && map[tileY][tileX] && map[tileY][tileX].walkable) { + // Update the item's position + pushableItems[i].x = newX; + pushableItems[i].y = newY; + } + } + } + + + + + + + // Call the game loop again next frame requestAnimationFrame(gameLoop); displayStats(); -- cgit 1.4.1-2-gfad0