-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
572 lines (536 loc) · 19.6 KB
/
index.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
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
const SHORT_TIMEOUT = 100
const LONG_TIMEOUT = 1000
const IMPORTANT_TIMEOUT = 1000
let divs = {}
let state = {
carry: 'none',
dragonDead: false
}
class Start {
isDisplayed = true
audio
constructor() {
this.displayStart()
this.audio = new Audio(`audio/hejnal.ogg`)
this.audio.play()
window.addEventListener('keydown', this.deleteStart)
}
displayStart = async () => {
let div = document.createElement('div')
div.id = 'startimg'
div.classList.add('start')
document.getElementById('wrapper').appendChild(div)
await new Promise(r => setTimeout(r, 5000));
if (!this.isDisplayed)
return
document.querySelector('.start').remove()
div = document.createElement('div')
div.id = 'starttext1'
div.classList.add('start')
document.getElementById('wrapper').appendChild(div)
await new Promise(r => setTimeout(r, 20000));
if (!this.isDisplayed)
return
document.querySelector('.start').remove()
div = document.createElement('div')
div.id = 'starttext2'
div.classList.add('start')
document.getElementById('wrapper').appendChild(div)
}
deleteStart = () => {
this.isDisplayed = false
this.audio.pause()
document.querySelector('.start').remove()
window.removeEventListener('keydown', this.deleteStart)
document.getElementById('content').innerHTML = `
<div id="title"></div>
<div id="img">
<div id="imganimation"></div>
</div>
<div id="compass">
<div class='compasshide' id='n'></div>
<div class='compasshide' id='e'></div>
<div class='compasshide' id='s'></div>
<div class='compasshide' id='w'></div>
</div>
<div id="direction"></div>
<div id="see"></div>
<div id="items"></div>
<div id="question"></div>
<input type="text" id="write" autofocus='true'>`
divs = {
content: document.getElementById('content'),
title: document.getElementById('title'),
img: document.getElementById('img'),
compass: document.getElementById('compass'),
direction: document.getElementById('direction'),
see: document.getElementById('see'),
items: document.getElementById('items'),
question: document.getElementById('question'),
write: document.getElementById('write')
}
focusKeydown()
new Render(6, 3, 'none')
}
}
class Location {
x
y
title
img
color
directions
items = 'none'
actions = 'none'
constructor(x, y) {
let loc = data[y][x]
this.x = x
this.y = y
this.title = loc.title
this.img = loc.img
this.color = loc.color
this.directions = loc.directions
this.items = loc.items
this.actions = loc.actions
}
}
class Render extends Location {
lastKey
vocabularyDisplayed = false
gossipsDisplayed = false
constructor(x, y, carrying, direction) {
super(x, y)
state.carry = carrying
this.generateHTML(direction)
window.onkeydown = e => this.handleKeydown(e, x, y)
divs.write.oninput = e => {
let char = e.target.value[e.target.value.length - 1]
if (!(/[a-zA-Z]/).test(this.lastKey) || this.lastKey.length != 1)
return
char = char.replace(/\w{1}/g, function (val) {
return val === val.toLowerCase() ? val.toUpperCase() : val.toUpperCase();
})
e.target.value = e.target.value.substring(0, e.target.value.length - 1) + char
this.prevInputLgt = divs.write.value.length
}
}
generateHTML = async (direction) => {
divs.write.value = ''
divs.write.style.display = 'none'
divs.question.innerHTML = `You are going ${direction}`
if (divs.title.innerHTML != '')
await this.imgAnimation()
document.getElementById('imganimation').style.height = '0'
divs.img.style.backgroundImage = `url('./img/${this.img}')`
divs.img.style.backgroundColor = this.color
await this.generateText()
document.getElementById('write').focus()
}
generateText = async () => {
divs.title.innerHTML = this.title
await new Promise(r => setTimeout(r, SHORT_TIMEOUT))
this.updateCompass()
await new Promise(r => setTimeout(r, SHORT_TIMEOUT))
divs.direction.innerHTML = this.getDirection()
await new Promise(r => setTimeout(r, SHORT_TIMEOUT))
divs.see.innerHTML = this.getSee()
await new Promise(r => setTimeout(r, SHORT_TIMEOUT))
divs.items.innerHTML = this.getItems()
await new Promise(r => setTimeout(r, SHORT_TIMEOUT))
divs.question.innerHTML = 'What now?'
divs.write.style.display = 'initial'
document.getElementById('write').focus()
return
}
imgAnimation = async () => {
document.getElementById('imganimation').style.backgroundImage = `url('./img/${this.img}')`
document.getElementById('imganimation').style.backgroundColor = this.color
let n = 0
let lastRender = Date.now()
const animate = () => {
if (n > 100)
return
window.requestAnimationFrame(animate)
if (Date.now() - lastRender < 30)
return
lastRender = Date.now()
document.getElementById('imganimation').style.height = `${n}%`
n += 4
}
window.requestAnimationFrame(animate)
return await new Promise(r => setTimeout(r, LONG_TIMEOUT));
}
updateCompass = () => {
const els = document.querySelectorAll('.compasshide')
for (const e of els)
e.style.background = 'var(--darkgray)'
for (const e of this.directions)
document.getElementById(e.toLowerCase()).style.background = 'none'
}
displayInfo = async (info) => {
divs.write.value = ''
divs.write.style.display = 'none'
divs.question.innerHTML = info
await new Promise(r => setTimeout(r, IMPORTANT_TIMEOUT))
divs.see.innerHTML = this.getSee()
divs.items.innerHTML = this.getItems()
divs.question.innerHTML = 'What now?'
divs.write.style.display = 'initial'
divs.write.focus()
}
displayInfoWithTimeouts = async (info) => {
divs.write.value = ''
divs.write.style.display = 'none'
divs.question.innerHTML = ''
for (const line of info) {
divs.question.innerHTML += line
await new Promise(r => setTimeout(r, IMPORTANT_TIMEOUT))
}
divs.see.innerHTML = this.getSee()
divs.items.innerHTML = this.getItems()
divs.question.innerHTML = 'What now?'
divs.write.style.display = 'initial'
divs.write.focus()
}
getDirection = () => {
let str = 'You can go'
for (const [i, e] of this.directions.entries()) {
str += i != 0 ? ', ' : ' '
switch (e) {
case 'W':
str += 'WEST'
break
case 'N':
str += 'NORTH'
break
case 'E':
str += 'EAST'
break
case 'S':
str += 'SOUTH'
}
}
return str
}
getSee = () => {
let str = 'You see '
if (this.items.length == 0)
return `${str}nothing`
for (const [i, item] of this.items.entries())
str += i == 0 ? item.longName : `, ${item.longName}`
return str
}
getItems = () => {
return state.carry == 'none' ?
`You are carrying nothing` :
`You are carrying ${state.carry.longName}`
}
handleKeydown = async (e, x, y) => {
if (this.vocabularyDisplayed) {
this.deleteVocabulary()
return
}
else if (this.gossipsDisplayed) {
this.deleteGossips()
return
}
this.lastKey = e.key
if (e.key != 'Enter')
return
const query = divs.write.value
if (query.split(' ').length == 1) { //commands without parameter
switch (query) {
case 'W': case 'WEST':
if (this.title == "You are inside a dragon's cave"
&& !state.dragonDead)
this.displayInfoWithTimeouts([
`You can't go that way...`,
`The dragon sleeps in a cave!`
])
else if (this.directions.includes('W'))
new Render(x - 1, y, state.carry, 'WEST')
else
this.badDirection()
break
case 'NORTH': case 'N':
if (this.directions.includes('N'))
new Render(x, y - 1, state.carry, 'NORTH')
else
this.badDirection()
break
case 'EAST': case 'E':
if (this.directions.includes('E'))
new Render(x + 1, y, state.carry, 'EAST')
else
this.badDirection()
break
case 'SOUTH': case 'S':
if (this.directions.includes('S'))
new Render(x, y + 1, state.carry, 'SOUTH')
else
this.badDirection()
break
case 'V': case 'VOCABULARY':
this.displayVocabulary()
break
case 'G': case 'GOSSIPS':
this.displayGossips()
break
default:
this.badCommand()
}
}
else if (query.split(' ').length == 2) { //commands with parameter
switch (query.split(' ')[0]) {
case 'T': case 'TAKE':
this.takeItem(query.split(' ')[1])
break
case 'D': case 'DROP':
this.dropItem(query.split(' ')[1])
break
case 'U': case 'USE':
this.useItem(query.split(' ')[1])
break
default:
this.badCommand()
}
}
else
this.badCommand()
}
badDirection = async () => {
divs.write.style.display = 'none'
divs.question.innerHTML = `You can't go that way`
await new Promise(r => setTimeout(r, IMPORTANT_TIMEOUT));
divs.write.style.display = 'initial'
divs.question.innerHTML = `What now?`
divs.write.value = ''
divs.write.focus()
}
takeItem = (itemName) => {
if (!this.items.some(el => el.name == itemName)) {
this.displayInfo(`There isn't anything like that here`)
return
}
if (state.carry != 'none') {
this.displayInfo('You are carrying something')
return
}
if (this.items.filter(a => a.name == itemName)[0].flag == '0') {
this.displayInfo(`You can't carry this`)
return
}
state.carry = this.items.filter(a => a.name == itemName)[0]
this.items = this.items.filter(a => a.name != itemName)
data[this.y][this.x].items = this.items
this.displayInfo(`You are taking ${state.carry.longName}`)
}
dropItem = (itemName) => {
if (state.carry == 'none') {
this.displayInfo('You are not carrying anything')
return
}
if (this.items.filter(a => a.flag == '1').length > 2) {
this.displayInfo(`You can't story any more here`)
return
}
if (state.carry.name != itemName) {
this.displayInfo(`You are not carrying it`)
return
}
this.items.push(state.carry)
this.displayInfo(`You are about to drop ${state.carry.longName}`)
state.carry = 'none'
}
useItem = async (itemName) => {
if (state.carry.name != itemName || state.carry == 'none') {
this.displayInfo(`You are not carrying anything like that`)
return
}
if (state.carry.name == 'PRIZE') {
this.endGame()
return
}
if (!this.actions.some(action => action.requiredId == state.carry.id)) {
this.displayInfo(`Nothing happened`)
return
}
const action = this.actions.filter(el => el.requiredId == state.carry.id)[0]
if (action.flag === '1')
state.carry = {
flag: action.flag,
id: action.id,
name: action.name,
longName: action.longName
}
else if (action.flag === '0') {
this.items.push({
flag: action.flag,
id: action.id,
name: action.name,
longName: action.longName
})
state.carry = 'none'
}
if (typeof action.text == 'string')
this.displayInfo(action.text)
else
this.displayInfoWithTimeouts(action.text)
if (action.name == 'dead dragon') {
state.dragonDead = true
this.img = `smok.bmp`
await this.imgAnimation()
document.getElementById('imganimation').style.height = '0'
divs.img.style.backgroundImage = `url('./img/${this.img}')`
divs.img.style.backgroundColor = this.color
state.dragonDead = true
data[this.y][this.x].actions.push({
flag: "1",
id: "34",
longName: "a DRAGONSKIN",
name: "DRAGONSKIN",
requiredId: "33",
text: "You cut a piece of dragon's skin"
})
this.actions.push({
flag: "1",
id: "34",
longName: "a DRAGONSKIN",
name: "DRAGONSKIN",
requiredId: "33",
text: "You cut a piece of dragon's skin"
})
return
}
//if sheep is ready
if (!(this.items.filter(item => item.flag == 0).length == 6))
return
this.items = []
data[this.y][this.x].items = []
state.carry = {
flag: '1',
id: '37',
name: 'SHEEP',
longName: 'a SHEEP'
}
this.displayInfo('Your fake sheep is full of poison and ready to be eaten by the dragon')
}
displayVocabulary = async () => {
this.vocabularyDisplayed = true
divs.write.value = ''
divs.write.style.display = 'none'
divs.question.innerHTML = ''
await new Promise(r => setTimeout(r, SHORT_TIMEOUT))
divs.items.innerHTML = ''
await new Promise(r => setTimeout(r, SHORT_TIMEOUT))
divs.see.innerHTML = ''
await new Promise(r => setTimeout(r, SHORT_TIMEOUT))
divs.direction.innerHTML = ''
const arr =
[
'NORTH or N, SOUTH or S<br>',
'WEST or W, EAST or E<br>',
'TAKE (object) or T (object)<br>',
'DROP (object) or D (object)<br>',
'USE (object) or U (object)<br>',
'GOSSIPS or G, VOCABULARY or V<br>',
'Press any key'
]
for (const e of arr) {
divs.direction.innerHTML += e
await new Promise(r => setTimeout(r, SHORT_TIMEOUT))
}
}
deleteVocabulary = async () => {
this.vocabularyDisplayed = false
const arr =
[
'NORTH or N, SOUTH or S<br>',
'WEST or W, EAST or E<br>',
'TAKE (object) or T (object)<br>',
'DROP (object) or D (object)<br>',
'USE (object) or U (object)<br>',
'GOSSIPS or G, VOCABULARY or V<br>',
'Press any key'
]
for (let i = arr.length; i >= 0; i--) {
divs.direction.innerHTML = ''
for (let j = 0; j < i; j++)
divs.direction.innerHTML += arr[j]
await new Promise(r => setTimeout(r, SHORT_TIMEOUT))
}
this.generateText()
}
displayGossips = async () => {
this.gossipsDisplayed = true
divs.write.value = ''
divs.write.style.display = 'none'
divs.question.innerHTML = ''
await new Promise(r => setTimeout(r, SHORT_TIMEOUT))
divs.items.innerHTML = ''
await new Promise(r => setTimeout(r, SHORT_TIMEOUT))
divs.see.innerHTML = ''
await new Promise(r => setTimeout(r, SHORT_TIMEOUT))
divs.direction.innerHTML = ''
const arr = [
"The woodcutter lost his home key...<br>",
"The butcher likes fruit... The cooper<br>",
"is greedy... Dratewka plans to make a<br>",
"poisoned bait for the dragon... The<br>",
"tavern owner is buying food from the<br>",
"pickers... Making a rag from a bag...<br>",
"Press any key"
]
for (const e of arr) {
divs.direction.innerHTML += e
await new Promise(r => setTimeout(r, SHORT_TIMEOUT))
}
}
deleteGossips = async () => {
this.gossipsDisplayed = false
const arr =
[
"The woodcutter lost his home key...<br>",
"The butcher likes fruit... The cooper<br>",
"is greedy... Dratewka plans to make a<br>",
"poisoned bait for the dragon... The<br>",
"tavern owner is buying food from the<br>",
"pickers... Making a rag from a bag...<br>",
"Press any key"
]
for (let i = arr.length; i >= 0; i--) {
divs.direction.innerHTML = ''
for (let j = 0; j < i; j++)
divs.direction.innerHTML += arr[j]
await new Promise(r => setTimeout(r, SHORT_TIMEOUT))
}
this.generateText()
}
badCommand = async () => {
divs.question.innerHTML = 'Try another word or V for vocabulary'
divs.write.value = ''
divs.write.style.display = 'none'
await new Promise(r => setTimeout(r, IMPORTANT_TIMEOUT))
divs.question.innerHTML = 'What now?'
divs.write.style.display = 'initial'
}
endGame = () => {
document.getElementById('wrapper').innerHTML = ''
let div = document.createElement('div')
div.id = 'end'
document.getElementById('wrapper').appendChild(div)
}
}
document.onmousedown = (e) => {
e.preventDefault();
}
const focusKeydown = () => {
window.addEventListener('keydown', () => {
divs.write.focus()
})
}
const displayStart = () => {
window.removeEventListener('keydown', displayStart)
new Start()
}
window.addEventListener('keydown', displayStart)