From 54586e4d5a8a7ce6443c610942f94f00c44f9f8a Mon Sep 17 00:00:00 2001 From: Fen Dweller Date: Fri, 9 Mar 2018 08:49:08 -0500 Subject: [PATCH] Rooms have objects, and objects have actions, and actions can be executed. Too many burgers --- feast.js | 47 +++++++++++++++++++++++++++++++++++++++++++---- world.js | 1 + 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/feast.js b/feast.js index 65ff3c3..4659d57 100644 --- a/feast.js +++ b/feast.js @@ -1,6 +1,8 @@ let currentRoom = null; let dirButtons = []; +let actionButtons = []; let mode = "explore"; +let actions = []; let player = { name: "Fen", @@ -11,6 +13,22 @@ let player = { maxFullness: 200 }; +function Object(name="Potato") { + this.name = name; + this.actions = []; +} + +function Burger() { + Object.call(this, "Burger"); + this.actions.push({ + "name": "Punch Burger", + "action": function() { + player.health += 10; + update(["You punch the hamburger."]); + } + }); +} + function updateExplore() { for (let i = 0; i < dirButtons.length; i++) { let button = dirButtons[i]; @@ -26,6 +44,11 @@ function updateExplore() { button.innerHTML = currentRoom.exits[i].name; } } + + for (let i = 0; i < actionButtons.length && i < actions.length; i++) { + actionButtons[i].innerHTML = actions[i].name; + actionButtons[i].addEventListener("click", function() { actions[i].action(); }); + } } function updateCombat() { @@ -56,16 +79,32 @@ function move(direction) { if (target == null) { alert("Tried to move to an empty room!"); return; - } else { - currentRoom = target; - update(["You move to " + currentRoom.name,currentRoom.description]); - updateDisplay(); } + + moveTo(target); +} + +function moveTo(room) { + actions = []; + currentRoom = room; + + currentRoom.objects.forEach(function (object) { + object.actions.forEach(function (action) { + actions.push(action); + }) + }) + + update(["You move to " + currentRoom.name,currentRoom.description]); + + updateDisplay(); } window.addEventListener('load', function(event) { loadCompass(); + actionButtons = Array.from( document.querySelectorAll(".action-button")); currentRoom = createWorld(); + currentRoom.objects.push(new Burger()); + moveTo(currentRoom); updateDisplay(); }); diff --git a/world.js b/world.js index de63b32..9d4c940 100644 --- a/world.js +++ b/world.js @@ -71,6 +71,7 @@ function Location(name="Nowhere",desc="Nada") { this.name = name; this.description = desc; this.exits = [null,null,null,null,null,null,null,null]; + this.objects = []; } function opposite(direction) {