From 893d2b0e04c76fe13a0d6488e9fddf219ab2394f Mon Sep 17 00:00:00 2001 From: Luna Date: Fri, 9 Jul 2021 12:56:23 -0700 Subject: [PATCH] v1 --- index.html | 13 ++++++++ index.js | 65 ++++++++++++++++++++++++++++++++++++ inventory.js | 16 +++++++++ locations.json | 44 ++++++++++++++++++++++++ style.css | 31 +++++++++++++++++ textadventure.code-workspace | 8 +++++ verbhandler.js | 23 +++++++++++++ 7 files changed, 200 insertions(+) create mode 100644 index.html create mode 100644 index.js create mode 100644 inventory.js create mode 100644 locations.json create mode 100644 style.css create mode 100644 textadventure.code-workspace create mode 100644 verbhandler.js diff --git a/index.html b/index.html new file mode 100644 index 0000000..e8b1884 --- /dev/null +++ b/index.html @@ -0,0 +1,13 @@ + + + + +

Text Adventure

+
+

+
+

>

+ + + + \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..3581d0f --- /dev/null +++ b/index.js @@ -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; + } +} \ No newline at end of file diff --git a/inventory.js b/inventory.js new file mode 100644 index 0000000..5f3dd0c --- /dev/null +++ b/inventory.js @@ -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() { + +} \ No newline at end of file diff --git a/locations.json b/locations.json new file mode 100644 index 0000000..79e0c9f --- /dev/null +++ b/locations.json @@ -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." + } +} \ No newline at end of file diff --git a/style.css b/style.css new file mode 100644 index 0000000..15dc785 --- /dev/null +++ b/style.css @@ -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; +} \ No newline at end of file diff --git a/textadventure.code-workspace b/textadventure.code-workspace new file mode 100644 index 0000000..876a149 --- /dev/null +++ b/textadventure.code-workspace @@ -0,0 +1,8 @@ +{ + "folders": [ + { + "path": "." + } + ], + "settings": {} +} \ No newline at end of file diff --git a/verbhandler.js b/verbhandler.js new file mode 100644 index 0000000..dec11ec --- /dev/null +++ b/verbhandler.js @@ -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.") + } +}