-
Notifications
You must be signed in to change notification settings - Fork 0
/
vn-runner.js
484 lines (450 loc) · 13.5 KB
/
vn-runner.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
let story
(function(storyContent) {
// These keys cause the text to advance.
const continueKeys = new Set([' ', 'Enter'])
// Ignore clicks/keypresses on anything in one of these CSS selectors.
// Any selector should work, so also things like '.myClass' or '#myID'
const noContinue = new Set(['a', 'button', 'input', 'textarea'])
story = new inkjs.Story(storyContent)
let storyContainer = document.querySelectorAll('.chat')[0]
const historyContainer = document.getElementById('old-chat')
const chatHistory = historyContainer.querySelectorAll('.chat-history')[0]
let choicePoint
const menuParent = elt => {
do {
elt = elt.parentNode
} while(elt != null && !elt.classList.contains('menu'))
return elt
}
for(const b of document.querySelectorAll('button.close')) {
const m = menuParent(b)
if(m != null) {
N(b, {'click': e => { m.classList.remove('show') }})
}
}
N(document.body, {'keydown': e => {
const menu = document.body.querySelector('.menu.show')
if(menu == null) return
const modifiers = e.shiftKey || e.altKey || e.ctrlKey || e.metaKey
if(!modifiers && e.key === 'Escape') menu.classList.remove('show')
}})
const saveMenu = document.getElementById('save-menu')
N(document.getElementById('save'), {'click': e => {
saveMenu.classList.add('show')
saveMenu.querySelector('.close').focus()
}})
N(document.getElementById('save-browser'), {'click': e => {
saveGame()
saveMenu.classList.remove('show')
}})
N(document.getElementById('load-browser'), {'click': e => {
loadGameFromBrowser()
saveMenu.classList.remove('show')
}})
N(document.getElementById('save-file'), {'click': e => {
const data = URL.createObjectURL(new Blob([JSON.stringify(gameState())]))
const link = N('a', {
href: data,
download: 'ink-vn-save.json',
style: {visibility: 'hidden'}
})
N(saveMenu, link)
link.click()
link.remove()
saveMenu.classList.remove('show')
}})
const loadButton = document.getElementById('load-file')
N(loadButton, {change: e => {
e.preventDefault()
const file = loadButton.files[0]
if(file == null) return
file.text().then(json => {
loadGame(JSON.parse(json), file.name)
saveMenu.classList.remove('show')
})
}})
N(document.getElementById('theme'), {'click': e => {
setTheme(!document.documentElement.classList.contains('dark'))
}})
N(document.getElementById('history'), {'click': e => {
historyContainer.classList.add('show')
historyContainer.querySelector('.close').focus()
}})
const confirmRestart = document.getElementById('confirm-restart')
N(document.getElementById('restart'), {'click': e => {
confirmRestart.classList.add('show')
}})
N(document.getElementById('yes-restart'), {'click': e => {
confirmRestart.classList.remove('show')
restart()
}})
N(document.getElementById('no-restart'), {'click': e => {
confirmRestart.classList.remove('show')
}})
let speechTags = { player_tag: "Me", speaker_tag: "" }
const speechTagName = isPlayer => isPlayer ? 'player_tag' : 'speaker_tag'
function stripSpeechTag(text, isPlayer) {
const m = /^([A-Z]\S*):\s+/.exec(text)
if(m) {
const varName = speechTagName(isPlayer)
text = text.substr(m[0].length)
speechTags[varName] = m[1]
if(story.variablesState[varName] != null) {
story.variablesState[varName] = m[1]
}
}
return text
}
function addSpeechTag(text, isPlayer) {
let strip = story.variablesState.strip_speaker_tags
if(strip == null) strip = 'player'
if(!strip || (strip == 'player' && !isPlayer)) {
text = speechTags[speechTagName(isPlayer)] + ': ' + text
}
return text
}
function getInkSpeechTag(isPlayer) {
const varName = speechTagName(isPlayer)
const inkVar = story.variablesState[varName]
if(inkVar != null) speechTags[varName] = inkVar
}
const images = {}
function clearImage(tag) {
const img = images[tag]
if(img && img.elt) {
img.elt.remove()
delete img.elt
delete images[tag]
if(tag === 'bg') delete camera.style['background-image']
}
}
function setImage(str) {
const err = ()=>{throw new Error("invalid image command: #"+str)}
const args = str.trim().split(/\s+/)
if(!/:$/.test(args[0])) err()
const tag = args.shift().slice(0,-1).toLowerCase()
let img = images[tag]
let file, coords = []
for(let i=0; i<args.length; ++i) {
const n = Number(args[i])
if(n === n) coords.push(n)
else if(file == null) file = args[i]
else err()
}
const cameras = document.querySelectorAll('.camera')
const camera = cameras[0].firstElementChild
if(file == null && coords.length === 0) {
clearImage(tag)
return
} else if(tag == 'bg') {
if(file == null || coords.length !== 0) err()
camera.style['background-image'] = 'url("img/'+file+'")'
images.bg = { tag: tag, img: file }
return
} else if(img == null) {
if(file == null || coords.length !== 3) err()
img = images[tag] = { tag: tag }
}
if(file != null && file !== img.file) {
const src = 'img/'+file
if(img.elt) img.elt.src = src
else {
img.elt = N('img', {src: src, style: {position: 'absolute'}})
N(camera, img.elt)
}
img.file = file
}
img.x = coords[0] ?? img.x
img.y = coords[1] ?? img.y
img.w = coords[2] ?? img.w
const ox=-img.x, oy=-img.y
N(img.elt, {style: {
left: img.x+'%',
top: img.y+'%',
transform: 'translate('+ox+'%, '+oy+'%)',
width: img.w+'%'
}})
}
function isInSelector(elt, selector) {
while(elt && elt.matches) {
if(elt.matches(selector)) return true
elt = elt.parentNode
}
return false
}
function choose(event) {
event.preventDefault()
getInkSpeechTag(true)
const p = N('p')
while(this.firstChild != null) p.appendChild(this.firstChild)
p.innerHTML = stripSpeechTag(p.innerHTML, true)
addHistoryLine(p, speechTags.player_tag)
const existingChoices = storyContainer.querySelectorAll('p.choice')
for(const c of existingChoices) c.remove()
story.ChooseChoiceIndex(+this.dataset.index)
choicePoint = story.state.toJson()
continueStory()
}
function setTheme(dark) {
try {
if(dark == null) {
dark = window.localStorage.getItem('theme') === 'dark'
}
if(dark == null) {
for(const t of story.globalTags) {
const m = /^([^:])+:(.*)$/.exec(t.trim().toLowerCase())
if(m && m[1] == 'theme') {
dark = m[2] === 'dark'
break
}
}
}
if(dark == null) {
const m = "(prefers-color-scheme: dark)"
dark = window.matchMedia(m).matches;
}
const classes = document.documentElement.classList
if(dark) classes.add('dark')
else classes.remove('dark')
window.localStorage.setItem('theme', dark ? 'dark' : '')
} catch(e) {
console.warn("Couldn't set/save theme")
}
}
function serializeImages() {
const out = []
for(tag in images) {
const i = images[tag]
out.push([tag+':',i.file, i.x, i.y, i.w].join(' '))
}
return out
}
function deserializeImages(images) {
for(const i of images) setImage(i)
}
function gameState() {
return {
ink: choicePoint,
alert: document.body.classList.contains('alert'),
speechTags: speechTags,
img: serializeImages(images)
}
}
function loadGame(state, name) {
try {
clearContent()
if(state.ink.alert) document.body.classList.add('alert')
story.state.LoadJson(state.ink)
choicePoint = story.state.toJson()
speechTags = state.speechTags
deserializeImages(state.img)
continueStory()
return true
} catch(e) {
console.debug("Couldn't load game \""+name+"\"", e)
}
return false
}
function saveGame(name) {
name = name ?? 'default'
try {
window.localStorage.setItem(name, JSON.stringify(gameState()))
} catch(e) {
console.warn("Couldn't save game \""+name+"\"")
}
}
function loadGameFromBrowser(name) {
name = name ?? 'default'
try {
let state = window.localStorage.getItem(name)
if(state) return loadGame(JSON.parse(state), name)
} catch(e) {
console.debug("Couldn't load game \""+name+"\"", e)
}
return false
}
function clearContent() {
for(const tag in images) clearImage(tag)
document.body.classList.remove('alert')
document.querySelector('.chat').textContent = ''
chatHistory.textContent = ''
}
function restart() {
clearContent()
story.ResetState()
choicePoint = story.state.toJson()
continueStory()
}
function splitTag(t) {
let split = /([^:]+):(.*)/.exec(t)
if(split != null) {
split = {
property: split[1].toLowerCase(),
value: split[2].trim()
}
}
return split
}
let currentAudio = null
function continueStory() {
if(story.canContinue) {
let text = story.Continue().trim()
text = stripSpeechTag(text)
for(const t of story.currentTags) {
const lower = t.trim().toLowerCase()
const split = splitTag(t)
if(split?.property === 'audio') {
if(currentAudio) {
currentAudio.pause()
currentAudio.removeAttribute('src')
currentAudio.load()
}
currentAudio = new Audio(split.value)
currentAudio.play()
} else if(lower === 'alert') {
document.body.classList.add('alert')
} else if(lower === 'dialogue') {
document.body.classList.remove('alert')
} else if(lower === 'restart') {
restart()
return
} else if(/^img\s/.test(lower)) setImage(t.substring(3).trim())
}
storyContainer.replaceChildren()
if(text !== '') {
const p = N('p', {class: 'text'})
p.innerHTML = addSpeechTag(text)
storyContainer.appendChild(p)
getInkSpeechTag()
const logLine = N('p', {class: 'text'})
logLine.innerHTML = text
addHistoryLine(logLine, speechTags.speaker_tag)
}
}
// Render choices, if not already rendered.
if(!story.canContinue && storyContainer.querySelectorAll('.choice').length === 0) {
if(document.body.classList.contains('alert')) {
// Alert box: single paragraph with buttons.
const p = N('p', {class: 'choice'})
story.currentChoices.forEach((choice) => {
const i = choice.index
const t = stripSpeechTag(choice.text, true)
const b = N('button', {click: choose, 'data-index': i})
b.innerHTML = addSpeechTag(t, true)
N(p, b)
})
N(storyContainer, p)
} else {
// Regular dialogue: links on separate lines.
story.currentChoices.forEach((choice) => {
const i = choice.index
const t = stripSpeechTag(choice.text, true)
const a = N('a', {href: '#', click: choose, 'data-index': i})
a.innerHTML = addSpeechTag(t, true)
N(storyContainer, N('p', a, {class: 'choice'}))
})
}
}
if(storyContainer.querySelector('.continue')) return
// Detect end of story.
if(!story.canContinue && story.currentChoices.length === 0) return
const paras = storyContainer.querySelectorAll('p')
const last = paras[paras.length-1]
if(last && last.classList.contains('text')) {
N(last, N('br'), N('button', "➔", {
class: 'continue',
'aria-label': 'continue',
'click': e => { continueStory() }
}))
}
}
function maybeContinue(ev) {
// Not if a menu is showing
const menus = document.querySelectorAll('.menu')
for(const menu of menus) {
if(menu.classList.contains('show')) return
}
// Ignore these elements.
if(ev) for(const selector of noContinue) {
if(isInSelector(ev.target, selector)) return
}
const existingChoices = storyContainer.querySelectorAll('p.choice')
if(existingChoices.length === 0) {
// We're doing the special thing, prevent the default behavior
ev && ev.preventDefault()
continueStory()
}
}
const html = document.documentElement
N(html, {
click: maybeContinue,
keypress: function(ev) {
const modifiers = ev.ctrlKey || ev.altKey || ev.shiftKey
if(continueKeys.has(ev.key) && !modifiers) {
maybeContinue(ev)
}
}
})
function addHistoryLine(elt, charName) {
charName = charName ? charName+':' : ''
N(chatHistory, N('p', charName, {class: 'char-name'}), elt)
}
setTheme()
let urlTarget = false
if(window.location.hash !== '') {
try {
urlTarget = true
story.ChoosePathString(window.location.hash.substr(1))
} catch(e) {
urlTarget = false
console.debug("URL hash not a valid knot/stitch.")
}
}
if(urlTarget || !loadGameFromBrowser()) {
choicePoint = story.state.toJson()
continueStory()
}
})(storyContent);
//----------------------------------------------------------------------
// N: (Node) Add attributes, properties, and children to a DOM node
// (possibly creating it first).
// args:
// target: an Element or a tag name (e.g. "div")
// then optional in any order (type determines function)
// Element: child
// string: text node child
// array: values are treated as args
// null/undefined: ignored
// object: set attributes and properties of `target`.
// string: set attribute
// array: set property to array[0]
// object: set property properties.
// example: N('span', {style: {color: 'red'}})
// function: add event listener.
function N(target, ...args) {
const el = typeof target === 'string' ?
document.createElement(target) : target
for(const arg of args) {
if(arg instanceof Element || arg instanceof Text) {
el.appendChild(arg)
} else if(Array.isArray(arg)) {
N(el, ...arg)
} else if(typeof arg === 'string') {
el.appendChild(document.createTextNode(arg))
} else if(arg instanceof Object) {
for(const k in arg) {
const v = arg[k]
if(Array.isArray(v)) el[k] = v[0]
else if(v instanceof Function) el.addEventListener(k, v)
else if(v instanceof Object) {
for(const vk in v) el[k][vk] = v[vk]
} else el.setAttribute(k, v)
}
}
}
return el
}
function NS(name, ...args) {
const el = document.createElementNS("http://www.w3.org/2000/svg", name)
return N(el, ...args)
}