htmlFeaturesMain.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 rooms = getRooms(); 29 player = getPlayer(); 30 rooms = randomizeRoomLayout(rooms); //optional feature 31 rooms = randomizeBrokenThings(rooms); //optional feature (requires randomizeItems) 32 randomizePlayerStart(rooms); //optional feature 33 randomizeItems(rooms); //optional feature 34 randomizePoints(rooms); //optional feature 35 passcode = getPasscode(rooms); //optional feature 36 randomizeExits(rooms); //optional feature (part of passcode feature) 37 38 //This explains the game to a new player 39 var text = "<h3>Welcome to the Room Adventure!</h3>"; 40 text += "You are " + player.name; 41 text += " and you are in a house"; 42 text += " where many things are broken."; 43 text += "<p>Go from room to room"; 44 text += " to find the items you need"; 45 text += " to fix what's broken.</p>"; 46 text += "<p>Earn points for fixing things."; 47 text += " There are " + player.itemsLeftToFix; 48 text += " things that need to be fixed.</p>"; 49 text += "<p>Along the way, you will find pieces"; 50 text += " of a passcode. Find the exit, and"; 51 text += " enter the correct passcode to win!</p>"; 52 text += "<span style='color: slateblue;'>"; 53 text += "You start in the "; 54 text += player.currentRoom.name + ".</span>"; 55 text += "<h3>Good luck!</h3>"; 56 57 display.clear(); 58 display.info(text); 59 60 var button = "<button id='start' onclick='moveToRoom()'>Start Game</button>"; 61 display.navigation(button); 62 display.image(houseImages.outside); 63 } 64
65
function getPlayer() 66 { 67 var player = 68 { 69 name: "Lica", 70 score: 0, 71 currentRoom: rooms["living room"], 72 inventory: [], 73 itemsLeftToFix: 13, 74 maxScore: 0, 75 pathTaken: [] 76 }; 77 return player; 78 } 79
80
function movePlayer(direction) 81 { 82 var exits = player.currentRoom.exits; 83 var roomName = exits[direction]; 84 var roomToGoTo = rooms[roomName]; 85 player.currentRoom = roomToGoTo; 86 moveToRoom(); 87 } 88
89
function moveToRoom() 90 { 91 display.clear(); 92 display.image(player.currentRoom.image); 93 player.pathTaken.push(player.currentRoom); 94 createNavigationButtons(player); 95 display.description(player.currentRoom); 96 showItemInRoom(); 97 showBrokenThing(); 98 display.score(); 99 showInventory(); 100 } 101
102
function isThereAnItem() 103 { 104 var item = player.currentRoom.itemFound; 105 if (item) 106 { 107 display.found("You found the " + item + "!"); 108 player.inventory.push(item); 109 player.currentRoom.itemFound = null; 110 } 111 else 112 { 113 display.found("There's nothing to take. You lose 5 points."); 114 player.score -= 5; 115 } 116 117 showInventory(); 118 } 119
120
function fixBrokenThing(fixWith) 121 { 122 //helper variable 123 var index = player.inventory.indexOf(fixWith); 124 var brokenThing = player.currentRoom.brokenThing; 125 var text = ""; 126 127 if (!brokenThing) 128 { 129 text += "There's nothing to fix in here! "; 130 text += "You lose 10 points. "; 131 player.score -= 10; 132 } 133 134 //test: if fixWith is NOT in inventory 135 else if (index === -1) 136 { 137 text += "You're not carrying a " + fixWith + ". "; 138 text += "You lose 5 points."; 139 player.score -= 5; 140 141 } 142 else if (fixWith !== player.currentRoom.fixWith) 143 { 144 text += "The " + fixWith + " won't fix "; 145 text += "the " + brokenThing + ". "; 146 text += "You lose 15 points."; 147 player.score -= 15; 148 } 149 150 else //the item IS in the inventory 151 { 152 if (playerIsTired()) //optional feature 153 { 154 text += "You try to fix the " + brokenThing; 155 text += " but you feel fatigued and couldn't."; 156 text += " You lose 5 points. Try again. "; 157 player.score -= 5; 158 } 159 else 160 { 161 text += "You fixed the " + brokenThing; 162 text += " with the " + fixWith + "!"; 163 text += " You earn "; 164 text += player.currentRoom.points; 165 text += " points."; 166 167 player.currentRoom.brokenThing = null; 168 169 //Feature: Getting More Descriptive 170 if (player.currentRoom.altDescription) 171 { 172 player.currentRoom.description = 173 player.currentRoom.altDescription; 174 display.description(player.currentRoom); 175 } 176 177 player.score += player.currentRoom.points; 178 player.itemsLeftToFix--; 179 player.inventory.splice(index, 1); 180 if (player.currentRoom.passcodeHint) 181 { 182 text += "<p>You found a piece of the passcode!</p>"; 183 text += "{ " + player.currentRoom.passcodeHint + " }"; 184 player.inventory.push(player.currentRoom.passcodeHint); 185 } 186 } 187 } 188 display.broken(text); 189 display.score(); 190 showInventory(); 191 } 192
193
//Feature: Making the Player Tired 194 function playerIsTired() 195 { 196 var items = player.inventory.length; 197 var fixes = player.itemsLeftToFix; 198 var steps = 0; 199 200 if (player.pathTaken) 201 { 202 steps = player.pathTaken.length; 203 } 204 var tiredness = items + steps - fixes; 205 var effort = Math.min(tiredness, 25); 206 var threshold = Math.floor(Math.random() * effort); 207 208 return threshold > 15; 209 } 210
211
function alertGameWon() 212 { 213 var text = "<h3>Congratulations, " + player.name + "!</h3>"; 214 text += "You entered the correct passcode "; 215 text += "and escaped the house! "; 216 217 text += "<p>You earn " + passcode.reward + " points!</p>"; 218 player.score += passcode.reward; 219 220 text += "You finished the game with a score of "; 221 text += player.score + " points! "; 222 text += "Play again soon!"; 223 display.info(text); 224 225 var path = "<h3>Here's how you traversed the house</h3>"; 226 var steps = player.pathTaken.length; 227 for (var i = 0; i < steps; i++) 228 { 229 var room = player.pathTaken[i]; 230 path += room.name; 231 if (i < steps - 1) 232 { 233 path += " &rarr; "; // HTML right arrow 234 } 235 } 236 display.broken(path); 237 display.found(""); 238 display.image(houseImages.outside); 239 } 240
241
function checkPlayAgain() 242 { 243 //create the replay button 244 var buttons = "<button id='replay' onclick='startGame()'>Replay Game</button>"; 245 246 //optional: add a second 'leave game' button 247 var url = "http://coding.stephenjwolf.com"; 248 buttons += "<a href='" + url + "' target='_blank'><button id='leave'>Learn to Code</button></a>"; 249 250 display.navigation(buttons); 251 }