addhtmlfeaturesmain.js
1    "use strict"; 
2    var rooms; 
3    var player; 
4    var display = getDisplay(); 
5    var passcode = {}; 
6     
7    var houseImages = 
8      { 
9        outside: { 
10         src: "http://coding.stephenjwolf.com/roomadventure/roomimages/house.jpg", 
11         caption: "Photo by Rowan Heuvel", 
12         link: "https://unsplash.com/photos/bjej8BY1JYQ", 
13         linkInfo: "Courtesy: unsplash.com" 
14       }, 
15       exitSign: { 
16         src: "http://coding.stephenjwolf.com/roomadventure/roomimages/exit.jpg", 
17         caption: "Photo by Elliott Stallion", 
18         link: "https://unsplash.com/photos/wweHSdXdAgA", 
19         linkInfo: "Courtesy: unsplash.com" 
20       } 
21     }; 
22    
23   startGame(); 
24    
25
function startGame() 26 { 27 //reset the global rooms and player objects 28 updateColors(); 29 rooms = getRooms(); 30 player = getPlayer(); 31 32 //wrap in floors handler ----> 33 34 var successful = false; 35 var attempts = 0; 36 37 while (!successful && attempts < 1000) 38 { 39 rooms = randomizeRoomLayout(rooms); 40 rooms = randomizeKeys(rooms, true); 41 randomizePlayerStart(rooms); 42 successful = checkKeys(rooms, player.currentRoom.name, false); 43 attempts++; 44 } 45 46 if (!successful) 47 { 48 throw "I got stuck and couldn't unlock everything. You might want to restart and let me try again."; 49 } 50 51 rooms = randomizeBrokenThings(rooms); 52 randomizeItems(rooms); 53 randomizePoints(rooms); 54 55 //<---- wrap in floors handler 56 57 passcode = getPasscode(rooms); 58 randomizeExits(rooms); 59 60 var text = "<h3>Welcome to the Room Adventure!</h3>"; 61 text += "You are in a house"; 62 text += " where many things are broken."; 63 text += "<p>Go from room to room"; 64 text += " to find the items you need"; 65 text += " to fix what's broken.</p>"; 66 text += "<p>Earn points for fixing things."; 67 text += " There are " + player.itemsLeftToFix; 68 text += " things that need to be fixed.</p>"; 69 text += "<p>Along the way, you will find pieces"; 70 text += " of a passcode. Find the exit, and"; 71 text += " enter the correct passcode to win!</p>"; 72 text += "<span style='color: slateblue;'>"; 73 text += "You start in the "; 74 text += player.currentRoom.name + ".</span>"; 75 text += "<h3>Good luck!</h3>"; 76 77 display.clear(); 78 display.info(text); 79 80 var button = "<button id='start' onclick='getName()'>Start Game</button>"; 81 display.navigation(button); 82 display.image(houseImages.outside); 83 display.broken("Type your name in the box below, then press Start Game."); 84 var nameBox = "<input id='playerName' value='" + player.name + "'>"; 85 display.found(nameBox); 86 } 87
88
function getName() 89 { 90 var nameBox = document.getElementById("playerName"); 91 if (nameBox && nameBox.value !== "") 92 { 93 player.name = nameBox.value; 94 } 95 moveToRoom(); 96 } 97
98
function getPlayer() 99 { 100 var player = 101 { 102 name: "Lica", 103 score: 0, 104 currentRoom: rooms["living room"], 105 inventory: [], 106 keys: [], 107 itemsLeftToFix: 13, 108 maxScore: 0, 109 pathTaken: [] 110 }; 111 return player; 112 } 113
114
function movePlayer(direction) 115 { 116 var exits = player.currentRoom.exits; 117 var roomName = exits[direction]; 118 var roomToGoTo = rooms[roomName]; 119 120 if (roomToGoTo.locked) 121 { 122 var text = "<h3>You cannot go to the " + direction + ".</h3>"; 123 text += "<h3>That way is locked.</h3>"; 124 display.broken(text); 125 126 var penalty = 2; 127 text = "You lose " + penalty + " points. <p>Did you try using a key?</p>"; 128 display.found(text); 129 player.score -= penalty; 130 display.score(); 131 updateColors("red"); 132 133 var button = "<button class='text' onclick='moveToRoom()'>OK</button>"; 134 display.navigation(button); 135 } 136 else 137 { 138 player.currentRoom = roomToGoTo; 139 moveToRoom(); 140 } 141 } 142
143
function moveToRoom() 144 { 145 display.clear(); 146 display.image(player.currentRoom.image); 147 player.pathTaken.push(player.currentRoom); 148 createNavigationButtons(player); 149 display.description(player.currentRoom); 150 showItemInRoom(); 151 showBrokenThing(); 152 display.score(); 153 showInventory(); 154 updateColors(player.currentRoom.color); 155 } 156
157
function isThereAnItem() 158 { 159 var text = ""; 160 var item = player.currentRoom.itemFound; 161 if (item) 162 { 163 text += "You found the " + item + "!"; 164 player.inventory.push(item); 165 player.currentRoom.itemFound = null; 166 } 167 else 168 { 169 text += "There's nothing to take. You lose 5 points."; 170 player.score -= 5; 171 updateColors("red"); 172 } 173 174 var foundItem = document.getElementById("foundItem"); 175 if (foundItem) 176 { 177 foundItem.innerHTML = text; 178 } 179 else 180 { 181 display.found(text); 182 } 183 184 display.score(); 185 showInventory(); 186 } 187
188
function isThereAKey() 189 { 190 var text = ""; 191 var keys = player.currentRoom.keys; 192 193 if (keys && keys.length > 0) 194 { 195 var index = Math.floor(Math.random() * keys.length); 196 var key = keys[index]; 197 198 text += "You found the " + key + "!"; 199 player.keys.push(key); 200 player.currentRoom.keys.splice(index, 1); 201 } 202 else 203 { 204 //I don't want the player to lose points for looking for a key, so I'm changing the text and commenting out the part that takes off points. 205 text += "There is no key here."; 206 207 /* 208 text += "There's nothing to take. <p>You lose 5 points."; 209 player.score -= 5; 210 updateColors("red"); //optional add-on feature 211 */ 212 } 213 214 var foundKey = document.getElementById("foundKey"); 215 if (foundKey) 216 { 217 foundKey.innerHTML = text; 218 } 219 else 220 { 221 display.found(text); 222 } 223 224 display.score(); 225 showInventory(); 226 } 227
228
function useKey(keyName) 229 { 230 var text = ""; 231 var locks = []; 232 var unlocked = []; 233 var notUnlocked = []; 234 235 //get the set of rooms with locks, the number of doors unlocked, and the doors not unlocked 236 for (var exit in player.currentRoom.exits) 237 { 238 var exitName = player.currentRoom.exits[exit]; 239 var room = rooms[exitName]; 240 if (room.locked && room.locked.length > 0) 241 { 242 locks.push(exit); 243 var index = room.locked.indexOf(keyName); 244 if (index === -1) 245 { 246 notUnlocked.push(exit); 247 } 248 else 249 { 250 unlocked.push(exit); 251 room.locked = null; 252 } 253 } 254 } 255 256 if (locks.length === 0) 257 { 258 text += "<h3>No doors were locked here.</h3>"; 259 text += "You lose 5 points."; 260 player.score -= 5; 261 updateColors("red"); 262 } 263 264 /* 265 //simple grammar version 266 if (unlocked.length > 0) 267 { 268 text += "<strong>Exit(s) unlocked with the " + keyName + ": </strong>"; 269 text += unlocked.join(", "); 270 } 271 272 if (notUnlocked.length > 0) 273 { 274 text += "<strong>Exit(s) not unlocked with the " + keyName + ": </strong>"; 275 text += notUnlocked.join(", "); 276 } 277 */ 278 279 //this sets up the report for any doors that were unlocked 280 if (unlocked.length === 1) 281 { 282 text += "<h3>The " + keyName + " unlocked the " + unlocked[0] + " exit.</h3>"; 283 } 284 else if (unlocked.length > 1) 285 { 286 text += "<h3>The " + keyName + " unlocked the "; 287 288 var exitSlice = unlocked.slice(0, unlocked.length - 1); 289 text += exitSlice.join(", "); 290 if (unlocked.length > 2) 291 { 292 text += ","; 293 } 294 text += " and "; 295 text += unlocked[unlocked.length - 1]; 296 text += " exits.</h3>"; 297 } 298 299 //this sets up the report for any doors that could not be unlocked by the key that was used 300 if (notUnlocked.length === 1) 301 { 302 text += "<h3>The " + keyName + " could not unlock the " + notUnlocked[0] + " exit.</h3>"; 303 } 304 else if (notUnlocked.length > 1) 305 { 306 text += "<h3>The " + keyName + " could not unlock the "; 307 308 var exitSlice = notUnlocked.slice(0, notUnlocked.length - 1); 309 text += exitSlice.join(", "); 310 if (notUnlocked.length > 2) 311 { 312 text += ","; 313 } 314 text += " and "; 315 text += notUnlocked[notUnlocked.length - 1]; 316 text += " exits.</h3>"; 317 } 318 319 display.broken(text); 320 display.found(""); 321 var button = "<button class='text' onclick='moveToRoom()'>OK</button>"; 322 display.navigation(button); 323 } 324
325
function fixBrokenThing(fixWith) 326 { 327 //helper variable 328 var index = player.inventory.indexOf(fixWith); 329 var brokenThing = player.currentRoom.brokenThing; 330 var text = ""; 331 332 if (!brokenThing) 333 { 334 text += "There's nothing to fix in here! "; 335 text += "You lose 10 points. "; 336 player.score -= 10; 337 updateColors("red"); 338 } 339 340 //test: if fixWith is NOT in inventory 341 else if (index === -1) 342 { 343 text += "You're not carrying a " + fixWith + ". "; 344 text += "You lose 5 points."; 345 player.score -= 5; 346 updateColors("red"); 347 348 } 349 else if (fixWith !== player.currentRoom.fixWith) 350 { 351 text += "The " + fixWith + " won't fix "; 352 text += "the " + brokenThing + ". "; 353 text += "You lose 15 points."; 354 player.score -= 15; 355 updateColors("red"); 356 } 357 358 else //the item IS in the inventory 359 { 360 if (playerIsTired()) //optional feature 361 { 362 text += "You try to fix the " + brokenThing; 363 text += " but you feel fatigued and couldn't."; 364 text += " You lose 5 points. Try again. "; 365 player.score -= 5; 366 updateColors("red"); 367 } 368 else 369 { 370 text += "You fixed the " + brokenThing; 371 text += " with the " + fixWith + "!"; 372 text += " You earn "; 373 text += player.currentRoom.points; 374 text += " points."; 375 376 player.currentRoom.brokenThing = null; 377 378 //Feature: Getting More Descriptive 379 if (player.currentRoom.altDescription) 380 { 381 player.currentRoom.description = 382 player.currentRoom.altDescription; 383 display.description(player.currentRoom); 384 } 385 386 player.score += player.currentRoom.points; 387 updateColors("darkgreen"); 388 player.itemsLeftToFix--; 389 player.inventory.splice(index, 1); 390 if (player.currentRoom.passcodeHint) 391 { 392 text += "<p>You found a piece of the passcode!</p>"; 393 text += "{ " + player.currentRoom.passcodeHint + " }"; 394 395 if (player.keys) 396 { 397 player.keys.push(player.currentRoom.passcodeHint); 398 } 399 else 400 { 401 player.inventory.push(player.currentRoom.passcodeHint); 402 } 403 } 404 } 405 } 406 display.broken(text); 407 display.score(); 408 showInventory(); 409 410 } 411
412
//Feature: Making the Player Tired 413 function playerIsTired() 414 { 415 var items = player.inventory.length; 416 var fixes = player.itemsLeftToFix; 417 var steps = 0; 418 419 if (player.pathTaken) 420 { 421 steps = player.pathTaken.length; 422 } 423 var tiredness = items + steps - fixes; 424 var effort = Math.min(tiredness, 25); 425 var threshold = Math.floor(Math.random() * effort); 426 427 return threshold > 15; 428 } 429
430
function alertGameWon() 431 { 432 var text = "<h3>Congratulations, " + player.name + "!</h3>"; 433 text += "You entered the correct passcode "; 434 text += "and escaped the house! "; 435 436 text += "<p>You earn " + passcode.reward + " points!</p>"; 437 player.score += passcode.reward; 438 439 text += "You finished the game with a score of "; 440 text += player.score + " points! "; 441 text += "Play again soon!"; 442 display.info(text); 443 444 var path = "<h3>Here's how you traversed the house</h3>"; 445 var steps = player.pathTaken.length; 446 for (var i = 0; i < steps; i++) 447 { 448 var room = player.pathTaken[i]; 449 path += room.name; 450 if (i < steps - 1) 451 { 452 path += " &rarr; "; // HTML right arrow 453 } 454 } 455 display.broken(path); 456 display.found(""); 457 display.image(houseImages.outside); 458 updateColors("darkgreen"); 459 } 460
461
function checkPlayAgain() 462 { 463 //create the replay button 464 var buttons = "<button id='replay' onclick='startGame()'>Replay Game</button>"; 465 466 //optional: add a second 'leave game' button 467 var url = "http://coding.stephenjwolf.com"; 468 buttons += "<a href='" + url + "' target='_blank'><button id='leave'>Learn to Code</button></a>"; 469 470 display.navigation(buttons); 471 }