-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
131 lines (110 loc) · 3.41 KB
/
game.js
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
//intro function
function gameIntro() {
var para = document.querySelector ("#descrip");
para.textContent = "You decide to visit your friend Roderick (owner of the House of Usher), because his sister Madeline is gravely ill. Your goal is to get out of the house alive and find all the Edgar Allan Poe references."
var prompt = (document.querySelector("label"));
prompt.textContent = "Enter your name and press Enter."
var inputBox = document.querySelector("#input");
var listener = function(event) {
if (event.keyCode === 13) {
event.target.removeEventListener("keyup", listener);
customizePlayer(this.value);
gameStart();
}
}
inputBox.addEventListener ("keyup", listener);
};
//start game
var gameStart = function() {
var inputBox = document.querySelector("#input");
inputBox.addEventListener("keyup", function(event){
if (event.keyCode === 13) {
gameStep(this.value);
}
});
var prompt = (document.querySelector("label"));
prompt.textContent = "Type an action and press Enter."
displayActions();
healthPoints(0);
displayScene ();
}
//run one part of game
function gameStep (input){
var command = interpret (input);
var result = execute (command);
report (result);
};
//function that customizes player
function customizePlayer(input) {
player.name = input;
};
//interpret the input
function interpret (input){
var cmd = {}, tokens = input.trim().toLowerCase().split(" ");
cmd.action = tokens.shift();
cmd.target = tokens.join(" ");
return cmd;
};
//executing user's command
function execute (command){
console.log (" execute");
player[command.action](command.target);
};
//display result on page
function report (result){
displayActions();
displayInventory();
displayScene();
};
//clearing content before putting new stuff in
function clearContent(node) {
while (node.hasChildNodes()) {
node.removeChild(node.firstChild);
}
}
//function for displaying feedback
function displayFeedback (message) {
var message = message;
var para = document.querySelector ("#feedback");
clearContent(document.querySelector("#feedback"));
para.textContent = "\n" + message;
};
//function for displaying actions
function displayActions() {
var field, action, actionList;
actionList = document.querySelector("#help > ul");
clearContent(actionList);
for (field in player) {
if (player[field] instanceof Function) {
action = document.createElement("li");
action.textContent = field;
actionList.appendChild(action);
}
}
}
//function for displaying inventory
function displayInventory() {
var i, item, inventory;
inventory = document.querySelector("#inventory > ul");
clearContent(inventory);
for (i in player.items) {
item = document.createElement ("li");
item.textContent = player.items[i];
inventory.appendChild(item);
}
}
//function for displaying scene
function displayScene() {
var para = document.querySelector ("#descrip");
para.textContent = "You are in the " + player.currLoc.name + "-" + "\n" +
player.currLoc.description + "." + "\n" +
"The items you can take are: " + player.currLoc.items
};
//function for ending game
function gameEnd () {
addEventListener("keydown", function(event) {
if (event.keyCode == 13)
window.close();
});
}
window.onload = gameIntro;