-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
268 lines (249 loc) · 8.39 KB
/
script.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
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
var running = null;
var input = document.getElementsByTagName("input")[0]
var output = document.getElementsByTagName("textarea")[0];
var rooms = {};
var inventory = {};
var commands = ["go", "quit", "look", "take", "lock", "unlock", "help"]
var currentRoom;
function start(){
running = !running;
if (running){
input.value = "";
input.readOnly = false;
input.focus();
document.getElementsByTagName("button")[0].innerHTML = "Quit Game";
input.addEventListener("keyup", splitInput);
createRooms();
createExits();
createItems();
printWelcome();
} else {
input.readOnly = true;
output.innerHTML = "";
input.focus();
document.getElementsByTagName("button")[0].innerHTML = "New Game";
input.value += "Thank you for playing. Good bye.";
}
}
function addEndLine(){
output.innerHTML += "\n";
output.innerHTML += "--------------------";
output.scrollTop = output.scrollHeight;
}
function printWelcome(){
output.innerHTML += "Welcome to Stop the Virus!\n";
output.innerHTML += "Stop the Virus is a new adventure game.\n";
output.innerHTML += "Type 'help' if you need help.";
addEndLine();
printLocationInformation();
}
function createRooms(){
rooms["outside"] = new Room("outside the Robert Koch Institute");
rooms["mainhall"] = new Room("inside the building in the main hall");
rooms["secretary"] = new Room("in the secretary's office");
rooms["lab"] = new Room("in the vaccine laboratory");
rooms["office"] = new Room("in Prof. Drosten's office");
rooms["freezer"] = new Room("in the freezer");
currentRoom = rooms.outside;
}
function createExits(){
rooms.outside.exits["south"] = new Exit(rooms.mainhall, true);
rooms.mainhall.exits["north"] = new Exit(rooms.outside, true);
rooms.mainhall.exits["east"] = new Exit(rooms.secretary, true);
rooms.secretary.exits["east"] = new Exit(rooms.outside, false);
rooms.secretary.exits["south"] = new Exit(rooms.office, false);
rooms.secretary.exits["west"] = new Exit(rooms.mainhall, true);
rooms.office.exits["north"] = new Exit(rooms.secretary, false);
rooms.office.exits["east"] = new Exit(rooms.freezer, false);
rooms.office.exits["west"] = new Exit(rooms.lab, false);
rooms.freezer.exits["north"] = new Exit(rooms.lab, false);
rooms.lab.exits["east"] = new Exit(rooms.outside, false);
rooms.lab.exits["south"] = new Exit(rooms.freezer, false);
}
function createItems(){
rooms.mainhall.items["keycard"] = new Item(2, "A plastic key card in a metal casing.");
rooms.secretary.items["key"] = new Item(2, "The heavy master key, it opens all doors.");
rooms.secretary.items["baseballbat"] = new Item(70, "Someone is a baseball fan, apparently.");
rooms.office.items["nobeltrophy"] = new Item(42, "Won for discovering the cure for a deadly virus.");
rooms.office.items["researchpapers"] = new Item(1, "Recent documents on the new virus mutation.");
rooms.freezer.items["gastank"] = new Item(210, "A gas tank full of nitrogen. Caution: very cold!");
rooms.freezer.items["vaccine"] = new Item(1, "The cure for the new virus. Keep it cool!");
rooms.lab.items["coolerbox"] = new Item(15, "Portable, keeps beverages cool. It's empty :(");
rooms.lab.items["medikit"] = new Item(30, "A set of tools used for medical examination.");
rooms.lab.items["storageshelf"] = new Item(999, "Immobile shelf, secured with an electrical lock.");
}
function Exit(room, state){
this.direction = room;
this.state = state;
}
function Room(text){
this.description = text;
this.items = {};
this.exits = {};
}
function Item( weight, text){
this.weight = weight;
this.description = text;
}
function splitInput(){
if (event.keyCode === 13 && input.value.length !== 0){
event.preventDefault();
let splitStrings = input.value.toLowerCase().trim().split(" ");
input.value = "";
if (commands.includes(splitStrings[0])){
commandCheck(splitStrings.shift(), splitStrings.shift());
} else {
output.innerHTML += "\n";
output.innerHTML += "That is an unknown command."
addEndLine();
}
}
}
function commandCheck(first, second){
switch (first){
case "quit":
(second == undefined || second.length === 0) ? start() : output.innerHTML += "\nQuit what?\n--------------------";
break;
case "help":
printHelp();
break;
case "go":
go(second);
break;
case "look":
look();
break;
case "unlock":
unlock(second);
break;
case "lock":
lock(second);
break;
case "take":
take(second);
break;
default:
output.innerHTML += "\n";
output.innerHTML += "I don't know what you mean...";
addEndLine();
}
}
function printLocationInformation(){
output.innerHTML += "\n";
output.innerHTML += "You are " + currentRoom.description;
output.innerHTML += "\n";
output.innerHTML += "\n";
output.innerHTML += "There are exits to the " + Object.keys(currentRoom.exits);
addEndLine();
}
function printHelp(){
output.innerHTML += "\n";
output.innerHTML += "You are lost. You are alone. You are looking for\n";
output.innerHTML += "the vaccine that will save you from the killer virus.\n";
output.innerHTML += "\n";
output.innerHTML += "Your command words are:\n";
output.innerHTML += commands;
addEndLine();
}
function go(where){
if (where == undefined || where.length === 0) {
output.innerHTML += "\n";
output.innerHTML += "Go where?";
addEndLine();
} else if (Object.keys(currentRoom.exits).includes(where) ){
if (currentRoom.exits[where].state){
currentRoom = currentRoom.exits[where].direction;
printLocationInformation();
} else {
output.innerHTML += "\n";
output.innerHTML += "This door is locked.";
addEndLine();
}
} else {
output.innerHTML += "\n";
output.innerHTML += "There is no door.";
addEndLine();
}
}
function look(){
output.innerHTML += "\n";
output.innerHTML += "You see the following Items in the room: "
output.innerHTML += "\n";
output.innerHTML += Object.keys(currentRoom.items);
addEndLine();
}
function take(item){
if (item == undefined || item.length === 0) {
output.innerHTML += "\n";
output.innerHTML += "Take what?";
addEndLine();
} else if (Object.keys(currentRoom.items).length == 0){
output.innerHTML += "\n";
output.innerHTML += "Nothing to take here."
addEndLine();
} else {
if (Object.keys(currentRoom.items).includes(item)){
inventory[item] = currentRoom.items[item];
delete currentRoom.items[item];
output.innerHTML += "\n";
output.innerHTML += "You took " + item + " and put it in your inventory.";
addEndLine();
} else {
output.innerHTML += "\n";
output.innerHTML += "No such item here."
addEndLine();
}
}
}
function lock(where){
if (where == undefined || where.length === 0) {
output.innerHTML += "\n";
output.innerHTML += "Lock what?";
addEndLine();
} else if (Object.keys(currentRoom.exits).includes(where)){
if (!currentRoom.exits[where].state){
output.innerHTML += "\n";
output.innerHTML += "This door is already locked.";
addEndLine();
} else if (Object.keys(inventory).includes("key")){
currentRoom.exits[where].state = !currentRoom.exits[where].state
output.innerHTML += "\n";
output.innerHTML += "This door is locked now.";
addEndLine();
} else {
output.innerHTML += "\n";
output.innerHTML += "You need a key to do that.";
addEndLine();
}
} else {
output.innerHTML += "\n";
output.innerHTML += "There is no such door.";
addEndLine();
}
}
function unlock(where){
if (where == undefined || where.length === 0) {
output.innerHTML += "\n";
output.innerHTML += "Unlock what?";
addEndLine();
} else if (Object.keys(currentRoom.exits).includes(where) ){
if (currentRoom.exits[where].state){
output.innerHTML += "\n";
output.innerHTML += "This door is already open.";
addEndLine();
} else if (Object.keys(inventory).includes("key")) {
currentRoom.exits[where].state = !currentRoom.exits[where].state
output.innerHTML += "\n";
output.innerHTML += "This door is unlocked now.";
addEndLine();
} else {
output.innerHTML += "\n";
output.innerHTML += "You need a key to do that.";
addEndLine();
}
} else {
output.innerHTML += "\n";
output.innerHTML += "There is no such door.";
addEndLine();
}
}