Browse Source

Added hooks for room exits

tags/v0.1.0
Fen Dweller 6 years ago
parent
commit
08865498f4
No known key found for this signature in database GPG Key ID: E80B35A6F11C3656
2 changed files with 48 additions and 20 deletions
  1. +1
    -1
      satiate.js
  2. +47
    -19
      world.js

+ 1
- 1
satiate.js View File

@@ -32,7 +32,7 @@ function init() {
initWorld("demo", state); initWorld("demo", state);
initAudio(); initAudio();


moveToRoom("Home", state, false);
goToRoom("Home", state);
} }


window.addEventListener("load", init); window.addEventListener("load", init);

+ 47
- 19
world.js View File

@@ -36,10 +36,17 @@ function removeActionDescription() {
descHolder.textContent = ""; descHolder.textContent = "";
} }


function moveToRoom(dest, state, announce=true) {
function moveToRoom(src, exit, dest, state) {
console.log(state)
const room = state.world[dest]; 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) { if (room.hooks) {
for (let hook of 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); updateRoom(dest, state);
} }
@@ -109,7 +129,7 @@ function updateRoom(dest, state) {


button.addEventListener("click", () => { button.addEventListener("click", () => {
// todo: log // todo: log
moveToRoom(exit.target, state);
moveToRoom(room, exit, exit.target, state);
}) })
}); });
} }
@@ -167,30 +187,30 @@ worlds = {
"id": "Home", "id": "Home",
"name": "Home", "name": "Home",
"desc": "Where the wifi autoconnects", "desc": "Where the wifi autoconnects",
"move": (self, state) => {
"move": (room, state) => {
print(["You go back to your living room"]); print(["You go back to your living room"]);
}, },
"actions": [ "actions": [
{ {
"name": "Squint", "name": "Squint",
"desc": "Squint in a very aggressive manner", "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?"]); print(["You stare at the wall and notice a secret door. But where is the key?"]);
} }
}, },
{ {
"name": "Find Keys", "name": "Find Keys",
"desc": "Find your keys", "desc": "Find your keys",
"execute": (self, state) => {
"execute": (room, state) => {
state.player.items.keys.push("Locked Room"); state.player.items.keys.push("Locked Room");
print(["You found your keys under the couch cushions"]); print(["You found your keys under the couch cushions"]);
}, },
"show": [ "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"); return !state.player.items.keys.includes("Locked Room");
} }
] ]
@@ -201,20 +221,20 @@ worlds = {
"target": "Locked Room", "target": "Locked Room",
"desc": "It's locked!", "desc": "It's locked!",
"conditions": [ "conditions": [
(self, state) => {
(room, state) => {
return state.player.items.keys.includes("Locked Room"); return state.player.items.keys.includes("Locked Room");
} }
], ],
"show": [ "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": [ "hooks": [
(self, state) => {
(room, state) => {
print(["This is a test of the hooks"]); print(["This is a test of the hooks"]);
return true; return true;
} }
@@ -224,14 +244,22 @@ worlds = {
"id": "Locked Room", "id": "Locked Room",
"name": "Locked Room", "name": "Locked Room",
"desc": "Super seecret", "desc": "Super seecret",
"move": (self, state) => {
"move": (room, state) => {
print(["You enter the locked room. wowie!"]); print(["You enter the locked room. wowie!"]);
}, },
"exits": { "exits": {
"down": { "down": {
"target": "Home", "target": "Home",
"desc": "Back to 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;
}
]
} }
} }
} }


Loading…
Cancel
Save