Переглянути джерело

Added hooks for room exits

tags/v0.1.0
Fen Dweller 6 роки тому
джерело
коміт
08865498f4
Не вдалося знайти GPG ключ що відповідає даному підпису Ідентифікатор GPG ключа: E80B35A6F11C3656
2 змінених файлів з 48 додано та 20 видалено
  1. +1
    -1
      satiate.js
  2. +47
    -19
      world.js

+ 1
- 1
satiate.js Переглянути файл

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

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

window.addEventListener("load", init);

+ 47
- 19
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;
}
]
}
}
}


Завантаження…
Відмінити
Зберегти