-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathengine.js
123 lines (106 loc) · 2.94 KB
/
engine.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
/** @type HTMLDivElement */
let $novelText;
/** @type HTMLAudioElement */
let $audioPlayer;
/** @type HTMLDivElement */
let $novelImage;
/** @type HTMLUListElement */
let $choices;
class Promisable {
constructor() {
this.refresh();
}
refresh() {
this.promise = new Promise((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
});
}
async reusable() {
const result = await this.promise;
this.refresh();
return result;
}
}
let currentPromise = new Promisable();
let choicePromise = new Promisable();
let ready = new Promisable();
document.addEventListener('DOMContentLoaded', () => {
$novelText = document.getElementById('novelText');
$choices = document.getElementById('choices');
$audioPlayer = document.getElementById('audioPlayer');
$novelImage = document.getElementById('novelImage');
$novelText.addEventListener('click', () => {
// Move to next if exists
if(currentPromise) {
currentPromise.resolve();
}
});
ready.resolve();
});
export async function show(text) {
currentPromise.refresh(); // To avoid problems
//$novelText.textContent = text;
return await currentPromise.reusable();
}
export async function image(name) {
if(name) {
$novelImage.style.display = 'flex';
if(name.indexOf('://') >= 0) {
$novelImage.style.backgroundImage = 'url("' + name + '")';
} else {
$novelImage.style.backgroundImage = 'url("./image/' + name + '")';
}
} else {
$novelImage.style.display = 'none';
}
}
export async function ask(text, options) {
choicePromise.refresh();
$novelText.textContent = text;
// Clean $choices
while($choices.lastChild) {
$choices.removeChild($choices.lastChild);
}
for(const [ key, val ] of Object.entries(options)) {
const $li = document.createElement('li');
$li.value = key;
$li.textContent = val;
$li.addEventListener('click', () => {
choicePromise.resolve(key);
});
$choices.append($li);
}
const result = await choicePromise.reusable();
// Clean $choices
while($choices.lastChild) {
$choices.removeChild($choices.lastChild);
}
return result;
}
export async function music(name, loop = true) {
if(name) {
if(name.indexOf('://') >= 0) {
$audioPlayer.src = name;
} else {
$audioPlayer.src = './music/' + name;
}
$audioPlayer.loop = loop;
await $audioPlayer.play();
} else {
await $audioPlayer.pause();
}
}
export function whenready(f) {
ready.promise.then(f);
}
export function randompick(...args) {
return args[Math.floor(Math.random() * args.length)];
}
export function SceneHandler(scene) {
return async () => {
while(scene) {
scene = await scene();
}
}
}