-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunstable-vampire.html
494 lines (422 loc) · 12.7 KB
/
unstable-vampire.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Unstable Vampire</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
html, body {
margin: 0;
padding: 0;
height: 100%;
background-color: #312;
color: #dd3;
font-family: monospace;
font-size: 20px;
}
#main {
max-width: 70%;
margin: 0 auto;
height: 100%;
display: flex;
flex-direction: column;
}
.spacer {
flex: 1;
}
#start {
display: block;
padding: 1em;
font-size: 2em;
text-align: center;
}
#command-form {
padding-top: 1em;
padding-bottom: 1em;
}
#command {
width: 100%;
border: none;
outline: none;
border-bottom: 3px solid;
background: none;
font-family: inherit;
font-size: inherit;
color: inherit;
padding: .2em;
}
#game {
flex: 1;
}
#game p {
padding: 4px 10px;
}
.info { border-left: 5px solid #999; }
.info.warning {
border-color: #e89f28;
/*color: #e89f28;
background-color: #6b371f;*/
}
.info.success {
border-color: #70c12a;
/*color: #70c12a;
background-color: #2a4a25;*/
}
.info.fail {
border-color: #d63d3d;
/*color: #d63d3d;
background-color: #56160f;*/
}
span.warning {
color: #e89f28;
}
span.success {
color: #70c12a;
}
.command-line {
text-align: right;
}
.command-line.invalid {
text-decoration: line-through;
opacity: .5;
}
.suggest {
color: white;
border-bottom: 1px solid;
}
</style>
</head>
<body>
<div id="main">
<div class="spacer"></div>
<div id="game">
<div id="start">
Type <span class="suggest">run</span> to begin
</div>
</div>
<form id="command-form">
<input id="command" autocomplete="off">
</form>
</div>
<script>
;(function () {
var main = document.getElementById('main')
var game = document.getElementById('game')
var form = document.getElementById('command-form')
var cmd = document.getElementById('command')
var startBtn = document.getElementById('start')
var VampireP
var startTime
var isWaiting = true
var meters = 0
var interval
var item
var ammo
var weapon
var record = window.localStorage ? +window.localStorage['text_runner_record'] || 0 : 0
var startItem = {
next: ['start'],
run: function () {
step()
}
}
var defaultCommands = {}
var gameCommands = {
shoot: function () {
if (!weapon)
return say('You don\'t have any weapon', 'warning')
if (weapon.ammo <= 0)
return say('You don\'t have any arrows left, try to <span class="suggest">reload</span>', 'warning')
weapon.ammo--
var closestVampire = Vampires.sort(function (z1, z2) { return z2.meters - z1.meters })[0]
if (!closestVampire)
return say('There is no Vampire, you wasted an arrow')
var i = Vampires.indexOf(closestVampire)
Vampires.splice(i, 1)
if (!Vampires.length)
return say('You killed the last Vampire, you can take a breath', 'success')
say('You killed a Vampire, there ' + (Vampires.length == 1 ? 'is one Vampire' : 'are ' + Vampires.length + ' Vampires') + ' left', 'success')
},
reload: function () {
if (!weapon)
return say('You don\'t have any weapon to rearm', 'warning')
if (ammo <= 0)
return say('You don\'t have any arrows left')
ammo--
weapon.ammo = 4
say('You rearmed your bow', 'success')
}
}
var lastGunAt = 0
var nextItems = {
Vampire: function () {
var i = rand(0, 1)
console.log('next Vampire', i)
if (i === 0)
return 'VampireLeft'
return 'VampireRight'
},
gun: function () {
if (meters - lastGunAt < 20 || ammo >= 2)
return 'floor'
if (weapon)
return 'ammo'
lastGunAt = meters
return 'gun'
}
}
var items = {
start: {
description: 'You have one Vampire following you, you better <span class="suggest">run</span>!',
next: ['floor'],
run: function () { step() },
jump: function () { step() }
},
floor: {
next: ['floor', 'floor', 'floor', 'floor', 'wall', 'wall', nextItems.Vampire, nextItems.gun],
run: function () { step() },
jump: function () { step() }
},
wall: {
next: ['floor'],
description: 'You came to a wall',
color: 'warning',
run: function () {
say('You cannot run through a wall, try to <span class="suggest">jump</span> it', 'warning')
},
jump: function () {
say('You jumped the wall', 'success')
step()
}
},
VampireRight: {
description: 'A Vampire growls at your right',
color: 'warning',
next: ['floor'],
__init: function () {
addVampire(meters - 2)
},
run: function () {
say('You ran onto the Vampire and it bit you. You had to dodge it going <span class="suggest">left</span>', 'fail'),
die()
},
jump: function () {
say('You jumped onto the Vampire and it bit you. You had to dodge it going <span class="suggest">left</span>', 'fail')
die()
},
left: function () {
say('You dodged the Vampire!', 'success')
step()
},
right: function () {
say('You ran onto the Vampire and it bit you. You had to dodge it going <span class="suggest">left</span>', 'fail'),
die()
}
},
VampireLeft: {
description: 'A Vampire shrieks at your left',
color: 'warning',
next: ['floor'],
__init: function () {
addVampire(meters - 2)
},
run: function () {
say('You ran onto the Vampire and it bit you. You had to dodge it going <span class="suggest">right</span>', 'fail'),
die()
},
jump: function () {
say('You jumped onto the Vampire and it bit you. You had to dodge it going <span class="suggest">right</span>', 'fail')
die()
},
right: function () {
say('You dodged the Vampire!', 'success')
step()
},
left: function () {
say('You ran onto the Vampire and it bit you. You had to dodge it going <span class="suggest">right</span>', 'fail'),
die()
}
},
gun: {
description: 'You found a bow. You can <span class="suggest">grab</span> it or keep <span class="suggest">run</span>ning',
next: ['floor'],
grab: function () {
weapon = { ammo: 4 }
say('You took the bow, now you can <span class="suggest">shoot</span> the Vampires', 'success')
step(0)
},
run: function () {
step()
},
jump: function () {
step()
}
},
ammo: {
description: 'You found an arrow, you can <span class="suggest">grab</span> them or keep <span class="suggest">run</span>ning',
next: ['floor'],
grab: function () {
ammo++
say('You took ammo', 'success')
step(0)
},
run: function () {
step()
},
jump: function () {
step()
}
}
}
var Vampires = []
cmd.focus()
document.body.onclick = function () {
cmd.focus()
}
form.onsubmit = function (e) {
e.preventDefault()
var command = cmd.value.trim().toLowerCase()
if (command) {
if (isWaiting) {
if (command === 'run') {
start()
doCommand(command)
}
} else {
doCommand(command)
}
}
cmd.value = ''
cmd.focus()
return false
}
function start () {
game.innerHTML = ''
isWaiting = false
meters = 0
Vampires = []
weapon = null
ammo = 0
startTime = Date.now()
addVampire(-3)
item = startItem
step()
cmd.focus()
interval = setTimeout(loop, 1000)
}
function die () {
if (interval)
clearTimeout(interval)
cmd.value = ''
var sec = Math.floor((Date.now() - startTime) / 1000)
if (sec > record) {
record = sec
if (window.localStorage)
localStorage['text_runner_record'] = record
}
var dieMessage = 'You escaped for <span class="success">' + sec + '</span> seconds'
if (record) {
if (record === sec)
dieMessage += '<br>This is your best time!'
else
dieMessage += '<br>Your best time is <span class="success">' + record + '</span> seconds'
}
say(dieMessage)
game.appendChild(startBtn)
isWaiting = true
scroll()
}
function step(dist) {
if (typeof dist === 'undefined')
dist = 1
meters += dist
var nextItemName = randItem(item.next)
if (typeof nextItemName === 'function')
nextItemName = nextItemName()
item = items[nextItemName]
if (item.description)
say(item.description, item.color)
if (item.__init)
item.__init()
commands = assignMethods({}, defaultCommands, gameCommands, item)
}
function doCommand (text) {
var command = commands[text]
var p = line('command-line ' + (command ? '' : 'invalid'))
p.innerText = text
scroll()
if (command)
command()
}
function addVampire(position) {
Vampires.push({
meters: position
})
}
function loop () {
var maxPosition = -1000
// make Vampires walk
Vampires.forEach(function (Vampire) {
Vampire.meters++
if (Vampire.meters > maxPosition)
maxPosition = Vampire.meters
})
var distance = meters - maxPosition
if (!Vampires.length) {
addVampire(meters - 3)
say('Another Vampire arrived and started to chase you')
scroll()
interval = setTimeout(loop, 2000)
} else if (distance === 0) {
say((Vampires.length > 1 ? 'Vampires' : 'The Vampire') + ' bit you. You didn\'t <span class="suggest">run</span> enough', 'fail')
die()
} else {
var p = game.lastChild === VampireP ? VampireP : line()
VampireP = p
var distanceSpan = distance > 3 ? distance : '<span class="warning">' + distance + '</span>'
p.innerHTML = [(Vampires.length == 1 ? 'The Vampire is ' : 'Vampires are '),
distanceSpan,
' meter', (distance > 1 ? 's' : ''), ' behind you'].join('')
scroll()
interval = setTimeout(loop, 2000)
}
}
/** utils */
function say (text, type) {
var p = line('info ' + (type || ''), )
p.innerHTML = text
scroll()
}
function line (type) {
var p = document.createElement('p')
p.className = 'line ' + (type || '')
game.appendChild(p)
return p
}
function rand (min, max) {
return Math.floor(Math.random() * (max + 1 - min)) + min
}
function randItem(ar) {
return ar[rand(0, ar.length - 1)]
}
function assignMethods (objects) {
var ret
for (var i = 0; i < arguments.length; i++) {
var obj = arguments[i]
ret = ret || obj
for (var p in obj) {
if (typeof obj[p] !== 'function')
continue
ret[p] = obj[p]
}
}
return ret
}
function scroll () {
window.scrollTo(0, document.body.scrollHeight)
}
})()
</script>
</body>
</html>