From 08865498f44bc4cf64ddd2e33bdf4e5724cc8d07 Mon Sep 17 00:00:00 2001 From: Fen Dweller Date: Sun, 6 Jan 2019 12:46:13 -0600 Subject: [PATCH] Added hooks for room exits --- satiate.js | 2 +- world.js | 66 ++++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 48 insertions(+), 20 deletions(-) diff --git a/satiate.js b/satiate.js index 284f200..8de413f 100644 --- a/satiate.js +++ b/satiate.js @@ -32,7 +32,7 @@ function init() { initWorld("demo", state); initAudio(); - moveToRoom("Home", state, false); + goToRoom("Home", state); } window.addEventListener("load", init); diff --git a/world.js b/world.js index 0a9fb70..d09c617 100644 --- a/world.js +++ b/world.js @@ -36,10 +36,17 @@ function removeActionDescription() { descHolder.textContent = ""; } -function moveToRoom(dest, state, announce=true) { +function moveToRoom(src, exit, dest, state) { + console.log(state) const room = state.world[dest]; - console.log(room); + if (exit.hooks) { + for (let hook of exit.hooks) { + if (!hook(room, exit, state)) { + return; + } + } + } if (room.hooks) { for (let hook of room.hooks) { @@ -49,8 +56,21 @@ function moveToRoom(dest, state, announce=true) { } } - if (announce) - state.world[dest].move(state.world[dest], state); + state.world[dest].move(state.world[dest], state); + + updateRoom(dest, state); +} + +function goToRoom(dest, state) { + const room = state.world[dest]; + + if (room.hooks) { + for (let hook of room.hooks) { + if (!hook(room, state)) { + return; + } + } + } updateRoom(dest, state); } @@ -109,7 +129,7 @@ function updateRoom(dest, state) { button.addEventListener("click", () => { // todo: log - moveToRoom(exit.target, state); + moveToRoom(room, exit, exit.target, state); }) }); } @@ -167,30 +187,30 @@ worlds = { "id": "Home", "name": "Home", "desc": "Where the wifi autoconnects", - "move": (self, state) => { + "move": (room, state) => { print(["You go back to your living room"]); }, "actions": [ { "name": "Squint", "desc": "Squint in a very aggressive manner", - "execute": (self, state) => { - state.player.rooms[self.id].squinted = true; + "execute": (room, state) => { + state.player.rooms[room.id].squinted = true; print(["You stare at the wall and notice a secret door. But where is the key?"]); } }, { "name": "Find Keys", "desc": "Find your keys", - "execute": (self, state) => { + "execute": (room, state) => { state.player.items.keys.push("Locked Room"); print(["You found your keys under the couch cushions"]); }, "show": [ - (self, state) => { - return state.player.rooms[self.id].squinted; + (room, state) => { + return state.player.rooms[room.id].squinted; }, - (self, state) => { + (room, state) => { return !state.player.items.keys.includes("Locked Room"); } ] @@ -201,20 +221,20 @@ worlds = { "target": "Locked Room", "desc": "It's locked!", "conditions": [ - (self, state) => { + (room, state) => { return state.player.items.keys.includes("Locked Room"); } ], "show": [ - (self, state) => { - console.log(self); - return state.player.rooms[self.id].squinted; + (room, state) => { + console.log(room); + return state.player.rooms[room.id].squinted; } ] } }, "hooks": [ - (self, state) => { + (room, state) => { print(["This is a test of the hooks"]); return true; } @@ -224,14 +244,22 @@ worlds = { "id": "Locked Room", "name": "Locked Room", "desc": "Super seecret", - "move": (self, state) => { + "move": (room, state) => { print(["You enter the locked room. wowie!"]); }, "exits": { "down": { "target": "Home", "desc": "Back to home", - "move": "You dab" + "hooks": [ + (room, exit, state) => { + print(["Potato"]); + console.log(room); + console.log(exit); + console.log(state); + return true; + } + ] } } }