|
|
@@ -11,6 +11,17 @@ dirs = { |
|
|
"descend": "Down" |
|
|
"descend": "Down" |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function initWorld(state) { |
|
|
|
|
|
initRoomState(state); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function initRoomState(state) { |
|
|
|
|
|
state.player.rooms = {}; |
|
|
|
|
|
Object.entries(world).forEach(([key, val]) => { |
|
|
|
|
|
state.player.rooms[key] = {}; |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
function moveToRoom(dest, state) { |
|
|
function moveToRoom(dest, state) { |
|
|
updateRoom(dest, state); |
|
|
updateRoom(dest, state); |
|
|
} |
|
|
} |
|
|
@@ -18,15 +29,19 @@ function moveToRoom(dest, state) { |
|
|
function updateRoom(dest, state) { |
|
|
function updateRoom(dest, state) { |
|
|
const room = world[dest]; |
|
|
const room = world[dest]; |
|
|
|
|
|
|
|
|
|
|
|
if (!state.player.rooms[dest.id]) { |
|
|
|
|
|
state.player.rooms[dest.id] = {}; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
const areaName = document.querySelector("#area-name"); |
|
|
const areaName = document.querySelector("#area-name"); |
|
|
const areaDesc = document.querySelector("#area-desc"); |
|
|
const areaDesc = document.querySelector("#area-desc"); |
|
|
|
|
|
|
|
|
areaName.textContent = room.name; |
|
|
areaName.textContent = room.name; |
|
|
areaDesc.textContent = room.desc; |
|
|
areaDesc.textContent = room.desc; |
|
|
|
|
|
|
|
|
const holder = document.querySelector("#move-holder"); |
|
|
|
|
|
|
|
|
const moveHolder = document.querySelector("#move-holder"); |
|
|
|
|
|
|
|
|
holder.innerHTML = ""; |
|
|
|
|
|
|
|
|
moveHolder.innerHTML = ""; |
|
|
|
|
|
|
|
|
Object.entries(dirs).forEach(([dir, name]) => { |
|
|
Object.entries(dirs).forEach(([dir, name]) => { |
|
|
const button = document.createElement("button"); |
|
|
const button = document.createElement("button"); |
|
|
@@ -35,64 +50,129 @@ function updateRoom(dest, state) { |
|
|
button.classList.add("disabled"); |
|
|
button.classList.add("disabled"); |
|
|
button.setAttribute("disabled", "true"); |
|
|
button.setAttribute("disabled", "true"); |
|
|
button.textContent = dirs[dir]; |
|
|
button.textContent = dirs[dir]; |
|
|
holder.appendChild(button); |
|
|
|
|
|
|
|
|
moveHolder.appendChild(button); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
Object.entries(room.exits).forEach(([dir, exit]) => { |
|
|
|
|
|
const button = document.querySelector("#move-" + dir); |
|
|
|
|
|
const dest = world[exit.target]; |
|
|
|
|
|
|
|
|
if (room.exits) { |
|
|
|
|
|
Object.entries(room.exits).forEach(([dir, exit]) => { |
|
|
|
|
|
const button = document.querySelector("#move-" + dir); |
|
|
|
|
|
const dest = world[exit.target]; |
|
|
|
|
|
|
|
|
// don't even show an exit if this fails! |
|
|
|
|
|
|
|
|
// don't even show an exit if this fails! |
|
|
|
|
|
|
|
|
if (exit.show) { |
|
|
|
|
|
if (!exit.show.every(cond => cond(state))) { |
|
|
|
|
|
return; |
|
|
|
|
|
|
|
|
if (exit.show) { |
|
|
|
|
|
if (!exit.show.every(cond => cond(room, state))) { |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
button.textContent = dest.name; |
|
|
|
|
|
|
|
|
button.textContent = dest.name; |
|
|
|
|
|
|
|
|
// if any condition fails, don't enable/add a listener |
|
|
|
|
|
if (exit.conditions) { |
|
|
|
|
|
if (!exit.conditions.every(cond => cond(state))) { |
|
|
|
|
|
return; |
|
|
|
|
|
|
|
|
// if any condition fails, don't enable/add a listener |
|
|
|
|
|
if (exit.conditions) { |
|
|
|
|
|
if (!exit.conditions.every(cond => cond(room,state))) { |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
button.classList.remove("disabled"); |
|
|
|
|
|
button.removeAttribute("disabled"); |
|
|
|
|
|
|
|
|
button.classList.remove("disabled"); |
|
|
|
|
|
button.removeAttribute("disabled"); |
|
|
|
|
|
|
|
|
|
|
|
button.addEventListener("click", () => { |
|
|
|
|
|
// todo: log |
|
|
|
|
|
moveToRoom(exit.target, state); |
|
|
|
|
|
}) |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const actionHolder = document.querySelector("#actions"); |
|
|
|
|
|
actionHolder.innerHTML = ""; |
|
|
|
|
|
|
|
|
|
|
|
if (room.actions) { |
|
|
|
|
|
room.actions.forEach(action => { |
|
|
|
|
|
const button = document.createElement("button"); |
|
|
|
|
|
button.classList.add("action-button"); |
|
|
|
|
|
|
|
|
|
|
|
if (action.show) { |
|
|
|
|
|
if (!action.show.every(cond => cond(room, state))) { |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
button.textContent = action.name; |
|
|
|
|
|
|
|
|
|
|
|
actionHolder.appendChild(button); |
|
|
|
|
|
|
|
|
|
|
|
if (action.conditions) { |
|
|
|
|
|
if (!action.conditions.every(cond => cond(room, state))) { |
|
|
|
|
|
button.classList.add("disabled"); |
|
|
|
|
|
button.setAttribute("disabled", "true"); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
button.addEventListener("click", () => { |
|
|
|
|
|
action.execute(room, state); |
|
|
|
|
|
updateRoom(room.id, state); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
button.addEventListener("click", () => { |
|
|
|
|
|
// todo: log |
|
|
|
|
|
moveToRoom(exit.target, state); |
|
|
|
|
|
}) |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
world = { |
|
|
world = { |
|
|
"Home": { |
|
|
"Home": { |
|
|
|
|
|
"id": "Home", |
|
|
"name": "Home", |
|
|
"name": "Home", |
|
|
"desc": "Where the wifi autoconnects", |
|
|
"desc": "Where the wifi autoconnects", |
|
|
|
|
|
"actions": [ |
|
|
|
|
|
{ |
|
|
|
|
|
"name": "Squint", |
|
|
|
|
|
"desc": "Squint in a very aggressive manner", |
|
|
|
|
|
"execute": (self, state) => { |
|
|
|
|
|
state.player.rooms[self.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) => { |
|
|
|
|
|
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; |
|
|
|
|
|
} |
|
|
|
|
|
] |
|
|
|
|
|
} |
|
|
|
|
|
], |
|
|
"exits": { |
|
|
"exits": { |
|
|
"up": { |
|
|
"up": { |
|
|
"target": "Locked Room", |
|
|
"target": "Locked Room", |
|
|
"desc": "It's locked!", |
|
|
"desc": "It's locked!", |
|
|
"move": "You enter the secret locked room", |
|
|
"move": "You enter the secret locked room", |
|
|
"conditions": [ |
|
|
"conditions": [ |
|
|
state => { |
|
|
|
|
|
|
|
|
(self, state) => { |
|
|
return state.player.items.keys.includes("Locked Room"); |
|
|
return state.player.items.keys.includes("Locked Room"); |
|
|
} |
|
|
} |
|
|
], |
|
|
], |
|
|
|
|
|
|
|
|
"show": [ |
|
|
"show": [ |
|
|
state => { |
|
|
|
|
|
return state.player.items.keys.includes("Locked Room"); |
|
|
|
|
|
|
|
|
(self, state) => { |
|
|
|
|
|
console.log(self); |
|
|
|
|
|
return state.player.rooms[self.id].squinted; |
|
|
} |
|
|
} |
|
|
] |
|
|
] |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
"Locked Room": { |
|
|
"Locked Room": { |
|
|
|
|
|
"id": "Locked Room", |
|
|
"name": "Locked Room", |
|
|
"name": "Locked Room", |
|
|
"desc": "Super seecret", |
|
|
"desc": "Super seecret", |
|
|
"exits": { |
|
|
"exits": { |
|
|
|