textadventure/index.js

65 lines
1.6 KiB
JavaScript
Raw Normal View History

2021-07-09 19:56:23 +00:00
const userInput = document.getElementById("userInput");
var currentLocation;
let gameOver;
2021-07-09 20:57:51 +00:00
function main() {
2021-07-09 19:56:23 +00:00
currentLocation = Object.keys(locations)[0]
printBold(locations[currentLocation].onEntry)
}
main()
userInput.onkeypress = function (event) {
if (event.keyCode == 13) {
2021-07-10 02:59:41 +00:00
printText(">" + " " + userInput.value)
2021-07-09 19:56:23 +00:00
commandInput()
userInput.value = ""
}
}
function printText(text) {
let p = document.createElement("p");
const div = document.getElementById("userText");
div.appendChild(p)
p.innerHTML = text
let scroll = document.getElementById("userText");
scroll.scrollTop = scroll.scrollHeight;
}
function printBold(text) {
printText(text.bold())
}
function look() {
if (userInput.value.toLowerCase() === "look") {
printBold(Object.keys(locations[currentLocation].objects).join(", "))
}
}
function listLocation() {
if (userInput.value.toLowerCase() === "location") {
printBold(currentLocation)
}
}
function win() {
if (userInput.value.toLowerCase() === "win") {
2021-07-09 20:11:13 +00:00
printBold("You Won! Refresh to play again!")
2021-07-09 19:56:23 +00:00
document.getElementById("userInput").disabled = true;
}
}
function isgameOver() {
if (gameOver === true) {
printBold("Game Over. Refresh to play again!")
document.getElementById("userInput").disabled = true;
}
2021-07-10 00:54:28 +00:00
}
function listCommands() {
if (userInput.value.toLowerCase() === "commands") {
2021-07-10 03:47:57 +00:00
//printBold(Object.keys(locations[currentLocation].visibleVerbs).join(", "))
printBold("Commands, Look, Inventory, Location.")
2021-07-10 00:54:28 +00:00
}
2021-07-09 19:56:23 +00:00
}