textadventure/verbhandler.js

41 lines
1.3 KiB
JavaScript
Raw Normal View History

2021-07-09 19:56:23 +00:00
function commandInput() {
let input = userInput.value.toLocaleLowerCase()
if (input === "inventory") {
checkInventory()
} else if (input === "look") {
look()
} else if (input === "location") {
listLocation()
} else if (input === 'win') {
win()
2021-07-10 00:54:28 +00:00
} else if (input === 'commands') {
listCommands()
} else if (input.startsWith("pick up ") || input.startsWith("grab ")) {
pickUp()
2021-07-09 19:56:23 +00:00
} else {
verbHandler()
}
}
function verbHandler() {
2021-07-10 00:54:28 +00:00
let visibleVerbs = Object.keys(locations[currentLocation].visibleVerbs)
let hiddenVerbs = Object.keys(locations[currentLocation].hiddenVerbs)
if (visibleVerbs.includes(userInput.value.toLowerCase())) {
let verb = locations[currentLocation].visibleVerbs[userInput.value.toLowerCase()]
if (typeof verb === "string") {
locations[currentLocation].visibleVerbs[verb]()
} else {
verb()
}
} else if (hiddenVerbs.includes(userInput.value.toLowerCase())) {
let verb = locations[currentLocation].hiddenVerbs[userInput.value.toLowerCase()]
if (typeof verb === "string") {
locations[currentLocation].hiddenVerbs[verb]()
} else {
verb()
}
2021-07-09 19:56:23 +00:00
} else {
2021-07-10 00:54:28 +00:00
printBold("Invalid Action.")
2021-07-09 19:56:23 +00:00
}
}