about summary refs log tree commit diff stats
path: root/js/game-frame/game.js
diff options
context:
space:
mode:
authorelioat <elioat@tilde.institute>2023-12-28 18:11:21 -0500
committerelioat <elioat@tilde.institute>2023-12-28 18:11:21 -0500
commit2768352165dea9aa273a500885d4cea71becbdcd (patch)
tree25b2c620f8f7b3b1010969ca123cc2ffca6d3b9a /js/game-frame/game.js
parentc49f8d5dde2cc34036be6ee953ffef92a688fd28 (diff)
downloadtour-2768352165dea9aa273a500885d4cea71becbdcd.tar.gz
cooking with fire
Diffstat (limited to 'js/game-frame/game.js')
-rw-r--r--js/game-frame/game.js95
1 files changed, 92 insertions, 3 deletions
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] + "<br>";
+    }
+  }
+  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