-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhaven.js
More file actions
172 lines (138 loc) · 5.01 KB
/
haven.js
File metadata and controls
172 lines (138 loc) · 5.01 KB
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
import { addCallback, finalCallback, finished } from "./assets";
import { init as initInput } from "./input";
import { init as initBuffer } from "./buffer";
import { remove } from "./loader";
import { init as initOptions } from "./options";
import { init as initPrompt } from "./prompt";
import { init as initStyle } from "./style";
import { init as initWindow } from "./window";
// hook that's called when the game ends (engine stops)
let quitHook = null;
/**
* Start the game. If assets haven't loaded yet, the game starts
* as soon as they're ready.
*/
export async function start( opt ) {
// create the HTML structure
let havenElement;
let containerId = "haven"; // default main container id
// opt.container can supply a different main container element or id
if( opt.container instanceof Element ) {
havenElement = opt.container;
}
else if( opt.container && typeof opt.container === "string" ) {
havenElement = document.querySelector( opt.container );
if( !havenElement ) {
if( opt.container.charAt( 0 ) === "#" && opt.container.indexOf( " " ) === -1 ) {
containerId = opt.container.substr( 1 );
}
else {
throw new Error( `Can't find element "${opt.container}" to use as the main container` );
}
}
}
else {
havenElement = document.getElementById( "haven" );
}
if( !havenElement ) {
havenElement = document.createElement( "main" );
havenElement.id = containerId;
document.body.appendChild( havenElement );
}
let outputElement = document.getElementById( "output" );
if( !outputElement ) {
outputElement = document.createElement( "div" );
outputElement.id = "output";
havenElement.appendChild( outputElement );
}
let windowElement = document.getElementById( "window0" );
if( !windowElement ) {
windowElement = document.createElement( "div" );
windowElement.id = "window0";
windowElement.setAttribute( "aria-live", "polite" );
windowElement.setAttribute( "aria-atomic", "false" );
windowElement.setAttribute( "aria-relevant", "additions" );
outputElement.appendChild( windowElement );
}
let loaderContainer = document.getElementById( "loader" );
if( !loaderContainer ) {
loaderContainer = document.createElement( "div" );
loaderContainer.id = "loader";
havenElement.appendChild( loaderContainer );
}
let loaderMessageElement = document.getElementById( "loader-message" );
if( !loaderMessageElement ) {
loaderMessageElement = document.createElement( "h2" );
loaderMessageElement.id = "loader-message";
loaderContainer.appendChild( loaderMessageElement );
}
let spinnerElement = document.getElementById( "spinner" );
if( !spinnerElement ) {
spinnerElement = document.createElement( "h2" );
spinnerElement.id = "spinner";
spinnerElement.innerText = ".";
loaderContainer.appendChild( spinnerElement );
}
loaderMessageElement.innerText = "Loading interpreter and game file";
// read options from URL
initOptions( opt.options );
// load the story file
const storyFile = opt.loadStoryFile ? await opt.loadStoryFile( opt.virtualStoryfile ) : null;
// set up window elements
initWindow();
// set up input handlers
initInput({
expectHook: opt.hooks && opt.hooks.expectKeypress,
submitHook: opt.hooks && opt.hooks.submitKeypress
});
// set up output handlers
initBuffer({
outputFilter: opt.hooks && opt.filters.output
});
// set up the prompt
initPrompt({
engineInputFunction: opt.engineInputFunction,
enginePrompt: !!opt.enginePrompt,
expectHook: opt.hooks && opt.hooks.expectCommand,
inputFilter: opt.filters && opt.filters.input,
submitHook: opt.hooks && opt.hooks.submitCommand,
unicode: !!opt.unicode
});
// initialize style options
initStyle({
engineColors: !!opt.engineColors,
engineFontFamily: !!opt.engineFontFamily
});
// remove the loader
addCallback( remove );
// add the quit hook
if( opt.hooks && opt.hooks.quit ) {
quitHook = opt.hooks.quit;
}
// start the engine
finalCallback( () => opt.startEngine( storyFile ) );
finished( "storyfile" );
}
import * as bufferMethods from "./buffer";
import * as fileMethods from "./file";
import * as inputMethods from "./input";
import * as promptMethods from "./prompt";
import * as stateMethods from "./state";
import * as windowMethods from "./window";
// expose methods for the C engine to use
window.haven = {
buffer: bufferMethods,
file: fileMethods,
input: inputMethods,
prompt: promptMethods,
state: stateMethods,
window: windowMethods
};
/**
* Called by the engine to tell that the game has ended
*/
export function engineStops() {
if( typeof quitHook === "function" ) {
quitHook();
}
}