-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.js
214 lines (194 loc) · 5.3 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
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
function prepareGame(game) {
game.output.addMessage("greeting")
game.data = {
location: "entry",
beenThere: {},
items: [
{ name: "storeKey", location : "office", moved: false, uname:"מפתח", opensAt:"vilon", door:"e" },
{ name: "mainKey", location : "dark3", moved: false, uname:"מפתח", opensAt:"exit", door:"s" },
{ name: "torch", location : "warehouse", moved: false, uname:"לפיד" },
{ name: "lighter", location : "smoke", moved: false, uname:"מצית" },
],
inhands: [],
haveLight: false,
locked: { "vilon-e":true, "exit-s":true }
};
}
function makeATurn(game) {
var cmd = game.input.trim().split(" ")[0];
switch (cmd) {
case 'לך':
case 'רוץ':
case 'צא':
go(getDirection(game.input));
break;
case 'ריסט':
game.reset();
break;
case 'תסתכל':
case 'תביט':
case 'הסתכל':
case 'הבט':
look();
break;
case 'קח':
case 'הרם':
case 'תרים':
case 'תקח':
take();
break;
case 'זרוק':
case 'עזוב':
case 'הורד':
case 'הנח':
leave();
break;
case 'הדלק':
case 'הצת':
case 'הבער':
lightFire();
break;
case 'פתח':
case 'תפתח':
openDoor();
break;
default:
dontUnderstand();
break;
}
}
function lightFire() {
if (haveItem("lighter")) {
if (game.input.indexOf("לפיד")==-1) {
game.output.addLine("השתגעת!? אין מצב!");
} else {
if (haveItem("torch")) {
game.data.haveLight=true;
game.output.addLine("וואו - עכשיו יש אור!");
}
}
} else {
game.output.addLine("איך? נראה לי שחסר לך משהו...");
}
}
function haveItem(item) {
var inhands=game.data.inhands;
for (i=0; i<inhands.length ; i++ ) {
if (inhands[i].name==item) return true;
}
return false;
}
function openDoor() {
var inhands=game.data.inhands;
for (i=0; i<inhands.length ; i++ ) {
var item = inhands[i];
if (item.opensAt==game.data.location) {
item.location="used";
game.data.locked[item.opensAt+"-"+item.door] = false;
inhands.splice(i,1);
game.output.addLine("הדלת פתוחה");
return;
}
}
game.output.addLine("אין לי מפתח מתאים");
}
function take() {
var items = game.data.items;
var itemNames = [];
for (i=0; i<items.length; i++ ) {
var item = items[i];
if (item.location==game.data.location && game.input.indexOf(item.uname) !== -1) {
item.location="hand";
item.moved=true;
game.data.inhands.push(item);
game.output.addLine("לקחתי");
return;
}
}
game.output.addLine("אין דבר כזה בחדר");
}
function leave() {
var inhands=game.data.inhands;
for (i=0; i<inhands.length ; i++ ) {
var item = inhands[i];
if (game.input.indexOf(item.uname)) {
item.location=game.data.location;
inhands.splice(i,1);
game.output.addLine("הנחתי אותו על הרצפה");
return;
}
}
game.output.addLine("לא מחזיק דבר כזה");
}
function dontUnderstand() {
game.output.addLine("לא הבנתי");
}
function getDirection(str) {
var dir = "x";
if (str.indexOf("צפו") !== -1) dir="n";
if (str.indexOf("דרו") !== -1) dir="s";
if (str.indexOf("מזרח") !== -1) dir="e";
if (str.indexOf("מערב") !== -1) dir="w";
return dir;
}
function go(dir) {
if (game.data.locked[game.data.location+"-"+dir]) {
game.output.addLine("הדלת נעולה!");
return;
}
var curRoom = game.constants.map[game.data.location];
var nextRoom = curRoom[dir];
if (typeof nextRoom=='undefined') {
game.output.addLine("דפקתי את הראש בקיר. אאו!");
} else {
game.data.location=nextRoom;
if (game.data.beenThere[game.data.location]) game.output.addLine("אוקיי");
else look();
}
}
function look() {
if (game.data.location.indexOf("dark")>-1 && !game.data.haveLight) {
game.output.addLine("חושך מצריים - אני לא רואה כלום");
} else {
game.output.addMessage(game.data.location);
var curRoom = game.constants.map[game.data.location];
listItems(game.data.location);
listDoors(curRoom);
game.data.beenThere[game.data.location]=true;
}
}
function listItems(curLocation) {
var items = game.data.items;
var itemNames = [];
for (i=0; i<items.length; i++ ) {
var item = items[i];
if (item.location==curLocation) {
if (item.moved) {
itemNames.push(item.name);
} else {
game.output.addMessage(item.name+"-init");
}
}
if (itemNames.length==1) {
game.output.addText("על הרצפה מונח ");
game.output.addMessage(itemNames[0]);
} else if (itemNames.length>1) {
game.output.addLine("");
for (i=0; i<itemNames.length ; i++ ) game.output.addLine(itemNames[i]);
}
}
}
function listDoors(curRoom) {
var doors = [];
if (curRoom.n) doors.push("צפון");
if (curRoom.s) doors.push("דרום");
if (curRoom.e) doors.push("מזרח");
if (curRoom.w) doors.push("מערב");
if (doors.length==1) {
game.output.addMessage("יש דלת מכיוון "+doors[0]);
} else if (doors.length>0) {
var last = doors.pop();
var d=doors.join(", ");
game.output.addLine("יש דלתות מכיוון "+d+" ו"+last);
}
}