forked from verkaufer/TextAdventureGame
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
505 lines (377 loc) · 16.3 KB
/
script.js
File metadata and controls
505 lines (377 loc) · 16.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
//jquery effects code
$(document).ready(function(){
$("#expand").click(function () {
$("#invBox").slideToggle("fast");
});
//following is necessary to remove disable attribute when loading the page
$('#northButton').removeAttr('disabled');
$('#southButton').removeAttr('disabled');
$('#eastButton').removeAttr('disabled');
$('#westButton').removeAttr('disabled');
});
//end jQuery effects code
var score = 0; //initialize player score variable
var vistedLocs = []; //create array to record where player has visited. Updated via addVisited()
var inventory = []; //initialize the inventory for the user
var currentLocation = "home"; //current location of the user. Initialized as home.
//the init() function is called from the body when it loads. It will set the initial text inside the output textarea
function init(){
var txtBox = document.getElementById("storyText");
txtBox.value = "You wake up on plush carpet in a sparsely decorated room. You don't recognize where you are and feel uneased by how quiet it is. Looking around, you see 4 different ways out of this room. Perhaps someone else is here...";
}
function updateScore(setPoints){
var addPoints; // value of points to be added to player's score
if(setPoints === null){ //if the passed parameter is null, set default addPoints to 5
addPoints = 5;
}
else{
addPoints = setPoints;
}
score = score + addPoints; //add the defined number of points to player's score
document.getElementById("score").innerHTML = score; //write this to the document div
}
function addVisited(location){ //add location user just visited to the visitedLocations array
vistedLocs.push(location);
}
// the below code was sourced from Extreme Web Designs.com
function hasVisited(location){ //checks to see if user has already visited this location
if(vistedLocs.indexOf(location) !== -1){ //find if user has visted the passed location var already
return true;
}
else{
return false;
}
}
//this function is just a cleaner way for me to remember to update the location of the user when they go to any location
function updateLocation(location){
currentLocation = location;
}
function updateDisplay(message){ //fuction to add new text to textbox
var msg = message;
var txtBox = document.getElementById("storyText"); //set txtBox to contents of textbox
txtBox.value = msg + "\n\n" + txtBox.value; //append new message to textbox
}
/**********************************************************************************
/ Inventory functions
***********************************************************************************/
function itemObj(_id, _name, _description){
this.id = _id;
this.name = _name;
this.description = _description;
this.toString = function(){
var returnVariable = "";
returnVariable = this.description + "\n You earned " + this.points +" points.";
return returnVariable;
};
}
//updateInventory will take the item name and either add it to the inventory array if action = add or delete if action = delete
function updateInventory(item, action){
switch(action){
case "add":
inventory.push(item);
break;
case "delete":
//taken and modified from StackOverflow.com
//this loop search through the array inventory looking for the value of "item". When found, it deletes the item.
for(var i in inventory){
if(inventory[i] === item){
array.splice(i,1);
break;
}
}
break;
default:
setMsg = "Error encountered. Please try again.";
updateDisplay(setMsg);
break;
}
}
//updates #invBox div with new item
function updateInventoryDisplay(newItem, action){
if(action === "add" && newItem === "map"){
$("#invBox").append("<span id='"+newItem+"' class='item' onClick='popupMap()'>"+newItem+"</span>");
}
else if(action === "add"){
$("#invBox").append("<span id='"+newItem+"' class='item'>"+newItem+"</span>");
}
else{ //delete the item
$("#"+newItem+"").remove();
}
}
//checks if user has the item passed to the function
function hasItem(item){
if(inventory.indexOf(item) != -1){ //find if user has item passed to function
return true;
}
else{
return false;
}
}
/**********************************************************************************
/ End Inventory functions
***********************************************************************************/
//called to enable all the directions
function enableAllDirections(){
$('#northButton').removeAttr('disabled');
$('#southButton').removeAttr('disabled');
$('#eastButton').removeAttr('disabled');
$('#westButton').removeAttr('disabled');
}
//disables a direction one at a time via jQuery
function disableDirection(dir){
switch(dir){
case "north":
$('#northButton').attr('disabled', 'disabled');
break;
case "south":
$('#southButton').attr('disabled', 'disabled');
break;
case "east":
$('#eastButton').attr('disabled', 'disabled');
break;
case "west":
$('#westButton').attr('disabled', 'disabled');
break;
default:
$("#northButton");
break;
}
}
//enables individual directions. This is necessary for a couple of the specific item usage commands
function enableDirection(dir){
switch(dir){
case "north":
$('#northButton').removeAttr('disabled');
break;
case "south":
$('#southButton').removeAttr('disabled');
break;
case "east":
$('#eastButton').removeAttr('disabled');
break;
case "west":
$('#westButton').removeAttr('disabled');
break;
default:
$("#northButton");
break;
}
}
/*
Following 4 functions are called when the user presses buttons representing north, south, east, or west. Text is then input into textarea.
*/
function btn_goNorth(){
var direction = "north";
if(locationMatrix[currentLocation][direction] !== -1){ //if the direction they want to go is valid (based on current location and direction)
updateDisplay(locationMatrix[currentLocation][direction]); //update the display with the js closure data
if(!hasVisited(locationMatrix[currentLocation][direction].name)){ //have they visited this place before?
updateScore(locationMatrix[currentLocation][direction].points); //if not, give them the points from the prototype
}
addVisited(locationMatrix[currentLocation][direction].name); //add the location to visited array
currentLocation = locationMatrix[currentLocation][direction].name; //this successfully changes the current location to what "name" is set as in the playerLocation prototype.
checkNavButtons(currentLocation); //then run through the nav buttons to see which directions are valid
}
}
function btn_goSouth(){
var direction = "south";
if(locationMatrix[currentLocation][direction] !== -1){
updateDisplay(locationMatrix[currentLocation][direction]);
if(!hasVisited(locationMatrix[currentLocation][direction].name)){ //have they visited this place before?
updateScore(locationMatrix[currentLocation][direction].points); //if not, give them the points from the prototype
}
addVisited(locationMatrix[currentLocation][direction].name); //add the location to visited array
currentLocation = locationMatrix[currentLocation][direction].name; //this successfully changes the current location to what "name" is set as in the playerLocation prototype.
checkNavButtons(currentLocation);
}
}
function btn_goEast(){
var direction = "east";
if(locationMatrix[currentLocation][direction] !== -1){
updateDisplay(locationMatrix[currentLocation][direction]);
if(!hasVisited(locationMatrix[currentLocation][direction].name)){ //have they visited this place before?
updateScore(locationMatrix[currentLocation][direction].points); //if not, give them the points from the prototype
}
addVisited(locationMatrix[currentLocation][direction].name); //add the location to visited array
currentLocation = locationMatrix[currentLocation][direction].name; //this successfully changes the current location to what "name" is set as in the playerLocation prototype.
checkNavButtons(currentLocation);
}
}
function btn_goWest(){
var direction = "west";
if(locationMatrix[currentLocation][direction] !== -1){
updateDisplay(locationMatrix[currentLocation][direction]);
if(!hasVisited(locationMatrix[currentLocation][direction].name)){ //have they visited this place before?
updateScore(locationMatrix[currentLocation][direction].points); //if not, give them the points from the prototype
}
addVisited(locationMatrix[currentLocation][direction].name); //add the location to visited array
currentLocation = locationMatrix[currentLocation][direction].name; //this successfully changes the current location to what "name" is set as in the playerLocation prototype.
checkNavButtons(currentLocation);
}
}
//prints out the inventory for user in the textbox if they want to. This is also displayed in the dropdown below the textbox.
function listInventory(){
for(i=0; i<inventory.length;i++){
updateDisplay("-"+inventory[i]);
}
updateDisplay("Inventory:");
}
//called when the player wants to take the paper from the window
function takePaper(){
if(currentLocation === "windowWall" && !hasItem("map")){
var setMsg = "You take the paper and read it. It seems to be a crudely drawn map of some location. Maybe it's for this house?";
updateDisplay(setMsg);
//add map to inventory
var item = "map";
var action = "add";
updateInventory(item, action);
updateInventoryDisplay(item, action);
}
else{
var setMsg = "You can't do that here!";
updateDisplay(setMsg);
}
}
//called when they want to take the painting off the wall
function takePainting(){
if(currentLocation === "painting" && !hasItem("painting")){ //only if they are standing by the painting and don't have the painting item
var setMsg = "You take the crooked painting off the wall. There was a tunnel behind it!";
updateScore(15);
updateDisplay(setMsg);
currentLocation = "holeInWall";
enableDirection("east");
//add painting to inventory
var item = "painting";
var action = "add";
updateInventory(item, action);
updateInventoryDisplay(item, action);
}
else{
setMsg = "You can't do that here!";
updateDisplay(setMsg);
}
}
function takeKey(){
if(currentLocation === "tunnel" && !hasItem("key")){ //only if they are in the tunnel and don't have the key
var setMsg = "You take the skeleton key. It has the word 'escape' engraved on the handle and looks quite old. You should probably take this now. It doesn't seem like you'll be able to get back in here!";
updateScore(20);
updateDisplay(setMsg);
var item = "key";
var action = "add";
updateInventory(item, action);
updateInventoryDisplay(item, action);
}
else{
setMsg = "You can't do that here! There aren't any keys to pick up, doofus!";
updateDisplay(setMsg);
}
}
function useKey(){
if(currentLocation === "southArtHallway" && hasItem("key")){ //only if they are in the south hallway and have the key
var setMsg = "You put the key into the door and unlock it. The door slowly swings open and nothing else happens. Whelp. Guess you have to go forward.";
updateScore(25);
updateDisplay(setMsg);
currentLocation = "unlockedDoor";
enableDirection("south");
}
else{
setMsg = "You can't do that here, doofus!";
updateDisplay(setMsg);
}
}
function hangPainting(){
if(currentLocation === "emptyPainting" && hasItem("painting")){ //only if they have the painting and they're standing in front of the empty painting spot
var setMsg = "You hang the painting and watch it slowly sink down the wall. The hook must have been connected to some kind of lever. You hear a door unlock at the end of the hall.";
updateScore(35);
updateDisplay(setMsg);
currentLocation = "hungPainting";
enableDirection("south");
}
else {
setMsg = "Can't do that here. What are you even thinking trying to pull that kind of stunt?";
updateDisplay(setMsg);
}
}
//I know this is ugly, but I wanted something simple.
//Pops up a window that displays the map image if the user has the map
function popupMap(){
window.open( "game_map.png", "myWindow",
"status = 1, height = 300, width = 300, resizable = 0" );
}
//lists out a manual for commands for the user. Called via a command in the text input. The data is displayed in the textbox.
function listCommands(){
var setMsg = "Here are some valid commands: \n\n Directions can be written as the full word, North, or as the first letter, N. Action commands to go to certain areas follow the syntax 'enter ____'. \nTaking items requires using take ____ syntax. \nView inventory by typing inventory or clicking the plus icon. ";
updateDisplay(setMsg);
}
//this is for the command box for users who want to enter commands via text instead of the directional buttons
function enterCommand(){
var validCommands = ["north","south","east","west","n","s","e","w","enter cellar","commands", "take paper", "view painting", "exit cellar", "take painting", "take key", "inventory", "use key", "hang painting"];
var inputCmd = document.getElementById("commandBox").value; //set command to the value that was entered in commandBox
if(validCommands.indexOf(inputCmd.toLowerCase()) != -1){ //convert string to lowercase, then determine if command is valid by searching validCommands array
switch(inputCmd){
case "n": //north
btn_goNorth();
break;
case "north":
btn_goNorth();
break;
case "s": //south
btn_goSouth();
break;
case "south":
btn_goSouth();
break;
case "e": //east
btn_goEast();
break;
case "east":
btn_goEast();
break;
case "w": //west
btn_goWest();
break;
case "west":
btn_goWest();
break;
case "enter cellar": //enter ___ command for cellar. Calls enterCellar()
enterCellar();
break;
case "commands":
listCommands();
break;
case "take paper":
takePaper();
break;
case "view painting":
viewPainting();
break;
case "exit cellar":
exitCellar();
break;
case "take painting":
takePainting();
break;
case "take key":
takeKey();
break;
case "inventory":
listInventory();
break;
case "use key":
useKey();
break;
case "hang painting":
hangPainting();
break;
default:
setMsg="Invalid command. If you need help, enter 'commands' without quotes in the input box.";
updateDisplay(setMsg);
break;
}
}
else{ //invalid command
var setMsg = "This is not a valid command. For help with commands, please type 'commands' without quotations into the input box.";
updateDisplay(setMsg);
}
//now let's clear out the command box
document.getElementById("commandBox").value = "";
}