Luna 2021-07-09 12:56:23 -07:00
commit 893d2b0e04
7 changed files with 200 additions and 0 deletions

13
index.html Normal file
View File

@ -0,0 +1,13 @@
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Text Adventure</h1>
<div id="userText">
<p></p>
</div>
<p id="command">></p><input type="text" id="userInput" name="inputBox">
<script src="verbhandler.js"></script>
<script src="index.js"></script>
<script src="inventory.js"></script>
</body>

65
index.js Normal file
View File

@ -0,0 +1,65 @@
const userInput = document.getElementById("userInput");
var locations;
var currentLocation;
let gameOver;
async function parseJson() {
const result = await fetch("./locations.json")
locations = await result.json()
}
async function main() {
await parseJson();
currentLocation = Object.keys(locations)[0]
printBold(locations[currentLocation].onEntry)
}
main()
userInput.onkeypress = function (event) {
if (event.keyCode == 13) {
printText(userInput.value)
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") {
printBold("You Won! Refresh to play again!")
document.getElementById("userInput").disabled = true;
}
}
function isgameOver() {
if (gameOver === true) {
printBold("Game Over. Refresh to play again!")
document.getElementById("userInput").disabled = true;
}
}

16
inventory.js Normal file
View File

@ -0,0 +1,16 @@
inventory = []
function checkInventory() {
if (userInput.value.toLowerCase() === "inventory"){
if (inventory.length === 0) {
printBold("You have nothing.")
} else {
printBold(inventory.join(", "));
}
}
}
function pickUp() {
}

44
locations.json Normal file
View File

@ -0,0 +1,44 @@
{
"Computer Room": {
"objects": {
"Computer": {
"interactable": true
},
"Northern Door": {
"interactable": true,
"gotoroom": "hallway"
}
},
"verbs": {
"go north": {
},
"use computer": {
}
},
"onEntry": "You are in a dark room. With only a COMPUTER, and a monitor dimly illuminating the room. Type LOOK to see your surroundings."
},
"hallway": {
"objects": {
"Northern Door": {
"interactable": true
},
"Western Door": {
"interactable": false
},
"Vase": {
"interactable": true,
"contains": {
"key": {
"grabable": true
}
}
}
},
"onEntry": "You have entered a brightly lit hallway. In the hallway you notice a VASE and two doors. One to the east and one to the north."
}
}

31
style.css Normal file
View File

@ -0,0 +1,31 @@
body {
text-align: center;
background-color: rgb(60, 60, 60);
}
h1 {
background-color: white;
}
#userText {
margin-left: 25%;
justify-content: center;
background-color: grey;
text-align: left;
height: 35%;
width: 50%;
overflow-y: scroll;
}
#userInput {
width: 50%;
padding: 10px;
border:none;
margin-top: 10px;
margin-right: 13px;
}
#command {
display:inline;
font-weight: bolder;
}

View File

@ -0,0 +1,8 @@
{
"folders": [
{
"path": "."
}
],
"settings": {}
}

23
verbhandler.js Normal file
View File

@ -0,0 +1,23 @@
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()
} else {
verbHandler()
}
}
function verbHandler() {
let verbs = Object.keys(locations[currentLocation].verbs)
if (verbs.includes(userInput.value.toLowerCase())) {
// Something
} else {
printBold("Invalid Action.")
}
}