display.js
1    "use strict"; 
2    function showItemInRoom() 
3    { 
4      if (player.currentRoom.itemFound) 
5      { 
6        var button = "<button class='item' onclick='isThereAnItem()'>You see something useful in here.</button>"; 
7        display.found(button); 
8      } 
9    } 
10    
11
function showBrokenThing() 12 { 13 //helper variables to make the code easier to read 14 var brokenThing = player.currentRoom.brokenThing; 15 16 //test: Is there a broken thing? 17 if (brokenThing) 18 { 19 //get ready to announce there's a broken thing 20 var text = "There is a broken "; 21 text += brokenThing + " in this room. "; 22 display.broken(text); 23 } 24 } 25
26
function showInventory(disable) 27 { 28 var text = "<h3>Inventory</h3>"; 29 var length = player.inventory.length; 30 for (var i = 0; i < length; i++) 31 { 32 text += "<button class='inventory' "; 33 34 if (!disable) 35 { 36 //pay very close attention to punctuation here 37 //watch the single and double quotes! 38 text += "onclick='fixBrokenThing(\""; 39 text += player.inventory[i] + "\")'"; 40 } 41 text += ">"; 42 text += player.inventory[i]; 43 text += "</button>"; 44 } 45 display.inventory(text); 46 } 47
48
function createNavigationButtons(player) 49 { 50 var buttons = "<h3>Navigation</h3>"; 51 var toggle = false; 52 53 for (var exit in player.currentRoom.exits) 54 { 55 var button = "<button id='" + exit + "' "; 56 57 if (toggle) //add 'even' class to every other button 58 { 59 button += "class='even' "; 60 } 61 else 62 { 63 button += "class='odd' "; 64 } 65 toggle = !toggle; //flip the toggle switch 66 67 button += "onclick='movePlayer(this.id)'>"; 68 69 var exits = player.currentRoom.exits; //<--new 70 var roomName = exits[exit]; 71 var room = rooms[roomName]; 72 73 //show room names only if player has visited before and if we're tracking the player 74 if (player.pathTaken && player.pathTaken.indexOf(room) !== -1) 75 { 76 button += room.name + "<br>(" + exit + ")"; 77 } 78 else 79 { 80 button += exit; 81 } 82 83 button += "</button>"; 84 buttons += button; //add to buttons string 85 } 86 87 if (player.currentRoom.exitHouse) //<--new 88 { 89 //this is all on one line from var to </p>"; 90 var button = "<p><button id='exit' onclick='enterPasscode();'>Exit House</button></p>"; 91 buttons += button; 92 } 93 94 display.navigation(buttons); 95 } 96
97
function getDisplay() 98 { 99 var display = 100 {
101 info:
function (text) 102 { 103 var element = document.getElementById("info"); 104 element.innerHTML = text; 105 }, 106
107 found:
function (text) 108 { 109 var element = document.getElementById("found"); 110 element.innerHTML = text; 111 }, 112
113 broken:
function (text) 114 { 115 var element = document.getElementById("broken"); 116 element.innerHTML = text; 117 }, 118
119 navigation:
function (text) 120 { 121 var element = document.getElementById("navigation"); 122 element.innerHTML = text; 123 }, 124
125 gameTitle:
function (text) 126 { 127 var element = document.getElementById("gameTitle"); 128 element.innerHTML = text; 129 }, 130
131 inventory:
function (text) 132 { 133 var element = document.getElementById("inventory"); 134 element.innerHTML = text; 135 },
136 score:
function (clear) 137 { 138 var element = document.getElementById("score"); 139 140 if (clear) 141 { 142 element.innerHTML = ""; 143 return; 144 } 145 player.score = Math.max(0, player.score); 146 element.innerHTML = "Score: " + player.score; 147 148 //if there is a maxScore tally, then show the max 149 if (player.maxScore > 0) 150 { 151 element.innerHTML += " / Max: " + player.maxScore; 152 } 153 },
154 description:
function (room) 155 { 156 var element = document.getElementById("description"); 157 158 if (!room) 159 { 160 element.innerHTML = ""; 161 return; 162 } 163 164 var innerText = "<h3>You are in the "; 165 innerText += room.name + ".</h3>"; 166 innerText += "<p>" + room.description + "</p>"; 167 element.innerHTML = innerText; 168 },
169 image:
function (source) 170 { 171 //if no image exists, set up a blank one 172 if (!source) 173 { 174 source = {src: "", caption: ""}; 175 } 176 177 document.getElementById("image").src = source.src; 178 179 //set up the caption 180 var cap = source.caption; 181 182 //if image has a link, include it with the caption 183 if (source.link) 184 { 185 cap += "<br><a target='_blank' "; 186 cap += "href='" + source.link + "'>"; 187 cap += source.linkInfo + "</a>"; 188 } 189 190 document.getElementById("caption").innerHTML = cap; 191 },
192 clear:
function ()
193 { 194
this.info(""); 195 this.found(""); 196 this.broken(""); 197 this.navigation(""); 198 this.inventory(""); 199 this.image(); 200 this.description(); 201 this.score(true); 202 } 203 }; 204 return display; 205 } 206