//-----------------DATA STRUCTURES AND GLOBAL VARIABLES----------------- //global int: stores the current node the player is on var curr = 0; //global int: stores the previous node we were in var prev = 0; //array of the nodes visited in the order visited var pathArray = [0]; //struct which stores information for a single question function questionNode(questionText, storyNodeDest){ //string: stores text to display on screen this.questionText = questionText; //int: array index of node that this question will lead to this.storyNodeDest = storyNodeDest; } //struct which stores information for one story node //one story node can have up to four questions. function StoryNode(storyText, qNode1, qNode2, qNode3, qNode4){ //string: stores text to display on screen this.storyText = storyText; //questionNode: struct for data of question 1 this.qNode1 = qNode1; //questionNode: struct for data of question 2 this.qNode2 = qNode2; //questionNode: struct for data of question 3 this.qNode3 = qNode3; //questionNode: struct for data of question 4 this.qNode4 = qNode4; } //array where construction and storage of story nodes takes place. //the index of the array corresponds to the story node number. 0 is always the start. //questions can be the empty string for them not to be displayed. //if all questions are empty string, then the story node is a finish condition. var StoryNodes = [ new StoryNode( //node 0 (root) "Here you will begin your journey for the Pot of Gold... Along your journey, there will be multiple paths you can take. Make your choices carefully, and you may find the treasure you desire.", new questionNode("Head into the Forest", 1), new questionNode("Cross the Plains", 2), new questionNode("Climb the Hill", 3), new questionNode("Rest here and wait a while", 4) ), new StoryNode( //node 1 "The smell of fresh pine pierces your nose as you step further into the Forest. You are knowingly trespassing on Elvish land, so you keep low and sneak through the bushes.", new questionNode("I see something by that tree over there. Let me go check it out.", 8), new questionNode("Sit down to rest and cool off under the shade.", 11), new questionNode("", 0), new questionNode("", 0) ), new StoryNode( //node 2 "Walking through the wide-open Plains, the wind brings fresh air to your lungs and the sweet scent of wildflowers to your nose. You spot a bush in the wide open, as well as a large hole.", new questionNode("Investigate the bush.", 5), new questionNode("That hole looks deep; stick your foot in to see if you can reach the bottom.", 9), new questionNode("", 0), new questionNode("", 0) ), new StoryNode( //node 3 "On top of the Hill, you have an astonishing view of the land. The gentle rolling hills remind you of comfort and simpler times. The ground is soft where you stand; you can also head down into the Plains from here.", new questionNode("Start digging a hole", 6), new questionNode("Go down the side of the hill.", 12), new questionNode("", 0), new questionNode("", 0) ), new StoryNode( //node 4 "You set up your sleeping bag and take a rest. As you awaken, all of your belongings seem to have been stolen by bandits. You no longer have the motivation to continue your quest.", new questionNode("", 0), new questionNode("", 0), new questionNode("", 0), new questionNode("", 0) ), new StoryNode( //node 5 "You found a hastily scrawled note. In barely legible text, it reads: \"Make careful decisions now. One wrong move and you could get lost.\"", new questionNode("Break a branch off a tree to mark where you are in case you need to track back.", 7), new questionNode("Keep digging through the bush to see if you can find anything else.", 6), new questionNode("", 0), new questionNode("", 0) ), new StoryNode( //node 6 "What's this? You found a map that shows a red X inside the Forest.", new questionNode("Break a branch off the tree to remember where this spot was.", 7), new questionNode("Head to the X in the Forest.", 8), new questionNode("", 0), new questionNode("", 0) ), new StoryNode( //node 7 "BZZZZZZZZ. You broke a beehive off the tree and got swarmed by killer bees!", new questionNode("", 0), new questionNode("", 0), new questionNode("", 0), new questionNode("", 0) ), new StoryNode( //node 8 "You spot a pile of loose dirt. Treasure could be buried here. You do not have a shovel to dig with, however.", new questionNode("Dig it with your hands", 10), new questionNode("Break a branch off a tree to dig.", 7), new questionNode("", 0), new questionNode("", 0) ), new StoryNode( //node 9 "As you slowly lower yourself into the hole, the dirt gives way, and you fall in completely. You are trapped in the hole, with seemingly no way out.", new questionNode("", 0), new questionNode("", 0), new questionNode("", 0), new questionNode("", 0) ), new StoryNode( //node 10 (winner) "The dirt seems plenty loose enough to dig with your hands. You scoop aside handful by handful, until a small glint hits your eye. You dig more, and the sparkles grow in number. Revealing the object fully... TREASURE! Congratulations, you found the Pot of Gold!", new questionNode("", 0), new questionNode("", 0), new questionNode("", 0), new questionNode("", 0) ), new StoryNode( //node 11 "As you were resting, a gang of bandits kidnap you, and are now holding you for randsom!", new questionNode("", 0), new questionNode("", 0), new questionNode("", 0), new questionNode("", 0) ), new StoryNode( //node 12 "You were unable to keep your balance and fell down the hill. You hit your head hard, and are knocked unconcious.", new questionNode("", 0), new questionNode("", 0), new questionNode("", 0), new questionNode("", 0) ) ]; //-----------------------------FUNCTIONS-------------------------------------- //function that displays the root node elements to screen, starting the story. function startStory(){ //display the root node to the screen. storyBody.innerHTML = "

" + StoryNodes[0].storyText; question1.innerHTML = StoryNodes[0].qNode1.questionText; question2.innerHTML = StoryNodes[0].qNode2.questionText; question3.innerHTML = StoryNodes[0].qNode3.questionText; question4.innerHTML = StoryNodes[0].qNode4.questionText; //hide the "Start Story" button. document.getElementById("startStoryBtn").style.display = "none"; // show undo button document.getElementById("undoStoryBtn").style.display = "inline-block"; // show restart button document.getElementById("restart").style.display = "inline-block"; // hide title document.getElementById("title").style.display = "none"; } //gets and displays the next node. //takes 1 parameter: //qNum: number that corresponds to the question selected. async function getNode(qNum){ $(".question").fadeOut(); await sleep(500); //stores destination node derived from question choice var nodeDest = 999; //get the destination node based on question choice. switch(qNum){ case 1: nodeDest = StoryNodes[curr].qNode1.storyNodeDest; break; case 2: nodeDest = StoryNodes[curr].qNode2.storyNodeDest; break; case 3: nodeDest = StoryNodes[curr].qNode3.storyNodeDest; break; case 4: nodeDest = StoryNodes[curr].qNode4.storyNodeDest; break; } //move to the next node. //update our current node. curr = nodeDest; //add this node to the path. pathArray.push(curr); prev = pathArray[pathArray.length - 2]; //display the text to the screen. storyBody.innerHTML = ""; pathArray.forEach(displayStory); // show results button if(question1.innerHTML == "" && question2.innerHTML == "" && question3.innerHTML == "" && question4.innerHTML == ""){ document.getElementById("results").style.display = "inline-block"; } } //displays the story text and questions for the given node. //takes 1 parameter: //nodeIndex: int that represents node to be displayed. function displayStory(nodeIndex){ storyBody.innerHTML += "

" + StoryNodes[nodeIndex].storyText; //only display the questions of our current node. if(nodeIndex == curr){ question1.innerHTML = StoryNodes[curr].qNode1.questionText; question2.innerHTML = StoryNodes[curr].qNode2.questionText; question3.innerHTML = StoryNodes[curr].qNode3.questionText; question4.innerHTML = StoryNodes[curr].qNode4.questionText; $(".question").fadeIn(); } } //removes the last node in the pathArray and redisplays the story. function undoStory(){ //do not undo if we are in the first node. if(pathArray.length != 1){ //remove the last element. pathArray.pop(); curr = pathArray[pathArray.length - 1]; //redisplay the story. storyBody.innerHTML = ""; pathArray.forEach(displayStory); } // check if pathArray is still at end of path, if not remove results button for (var i = 0; i