-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhyped.js
355 lines (306 loc) · 8.2 KB
/
hyped.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
/**
* Copyright (c) 2015 Sarah Harmon
*
* This source code is free to use under the GNU General Public License (GPL) with author attribution.
*
**/
/*
* Creates a Passage object.
*
* Parameters:
* Title: Passage name (String).
* Scene: Static text description of scene (String array - each element is a paragraph).
* Choices: List of choices for the passage (String array).
*/
(function() {
window.Passage = function(title,scene,choices){
this.title = title;
this.scene = scene;
this.choices = choices;
};
Passage.prototype = {
render:function(){
// Make temporary arrays for scene and choices.
// This will ensure that we do not overwrite coded variables.
var scenes = this.scene.slice(0);
var chs = new Choices([]);
for (var j=0; j<this.choices.list.length;j++){
chs.addChoice(this.choices.list[j].text,this.choices.list[j].link);
}
// Check for commands.
for(k=0;k<scenes.length;k++){
scenes[k] = check_commands(scenes[k]);
}
// Show scene description.
document.getElementById("scene-description").innerHTML = scenes.join("<br><br>");
// Clear choice points.
document.getElementById("choice-points").innerHTML = "";
// Add new choice points.
var element = document.getElementById("choice-points");
for (i=0; i<chs.list.length;i++){
// Check for commands in the current choice point.
chs.list[i].text = check_commands(chs.list[i].text);
// Show choice point.
element.innerHTML += "<p class='choice-point' id="+ chs.list[i].link + " onClick='click_choice(this.id)'>" + chs.list[i].text + "</p>";
}
}
}
/*
* Creates a Choices object.
*
* Parameters
* List: all choices (Choice array).
*/
window.Choices = function(listOfChoices){
this.list = listOfChoices;
}
Choices.prototype = {
addChoice:function(text,link){
var ch = new Choice();
ch.text = text;
ch.link = link;
this.list.push(ch);
}
}
/*
* Creates a Choice object.
*
* Parameters:
* Text: text for a choice (String).
* Link: name of the passage the choice leads to (String).
*/
function Choice(text,link){
this.text = text;
this.link = link;
}
/*
* When you click a choice, show the passage that follows.
*/
window.click_choice = function(chosenLink){
// Clear choices shown on screen.
document.getElementById("choice-points").innerHTML = "";
// Display chosen passage.
passages[chosenLink].render();
}
/*
* Check for commands in scene description.
*/
function check_commands(text){
// Make an array of all of the commands.
var re = /\@@(.*?)\@@/g;
var commands = text.match(re);
if (commands){
// For each command,
for (var i=0; i<commands.length; i++){
// Trim @@ marker on both sides.
commands[i] = commands[i].slice(2,-2);
// Act on the command, if it does not modify the surrounding text.
follow_simple_command(commands[i]);
}
// If command was a request for a parameter value, state the value.
text = text.replace(/\@@(get\s.*?)\@@/g, function(matched){
return store.get(matched.slice(6,-2));
});
// Process conditionals.
text = text.replace(/\@@(if\s.*?)\endif@@/g, function(matched){
return process_conditional(matched);
});
// Return text with commands removed.
text = text.replace(re,"");
}
return text;
}
/*
* Reads a string containing a command, and attempts
* to follow the command, if the command is known.
*
* Currently handles:
* - @@set PARAM to VALUE@@: Sets a variable to a number or string.
* - @@incr PARAM@@: Increments a numeric variable by one.
* - @@incr PARAM x@@: Increments a numeric variable by x.
* - @@decr PARAM@@: Decrements a numeric variable by one.
* - @@decr PARAM x@@: Decrements a numeric variable by x.
* - @@mult PARAM x@@: Multiplies a numeric variable by x.
*/
function follow_simple_command(text){
// If the command is setting a parameter, do so.
if (~text.indexOf("set ")){
// Determine parameter name.
var findParam = get_var_name(text,"set",false);
var paramName = findParam[0];
// Determine parameter value.
var value = findParam[1].slice(paramName.length+4);
// Set parameter to value.
// Value may be a float (number) or a String.
if (isNumeric(value)){
store.set(paramName, parseFloat(value));
}
else {
store.set(paramName, value);
}
}
else if (~text.indexOf("incr ")){
perform_op(text,"incr")
}
else if (~text.indexOf("decr ")){
perform_op(text,"decr")
}
else if(~text.indexOf("mult ")){
perform_op(text,"mult");
}
}
/*
* Extracts and handles a conditional command.
*
* Currently handles:
* - @@if PARAM eq VALUE@@Write something.@@endif@@
* - @@if PARAM eq VALUE@@Write something.@@else@@Write something else.@@endif@@
*/
function process_conditional(cond){
var replacement = ""; // We will replace the code with the text it specifies.
var operator = ""; // eq, geq, leq, gt, lt
// Determine parameter name.
var findParam = get_var_name(cond,"if",true);
var param = findParam[0];
// Determine operator.
var findOp = get_var_name(findParam[1],param,false);
var operator = findOp[0];
// Handy variables for later reference:
// - x is remaining command after finding the operator
// - y is the first space we find in x
var x = findOp[1];
var y = x.indexOf(" ");
// Determine value.
var z = x.indexOf("@@");
var value = x.slice(y+1,z);
// Check if the expression is true.
isTrue = is_exp_true(param,operator,value);
// If the conditional is true, write the first statement.
if (isTrue){
var g = x.indexOf("@@");
var i = x.slice(g+2);
var h = i.indexOf("@@");
replacement = i.slice(0,h);
}
// If the conditional is not true,
else{
// If conditional contains else,
var a = x.indexOf('@@else@@');
if(~a){
// ...write the statement corresponding with the else.
var b = x.slice(a+8);
var c = b.indexOf("@@endif@@");
replacement=b.slice(0,c);
}
}
return replacement;
}
/*
* Extracts entity name from a command expression
* (e.g., finds parameter and operator names).
*
* If codetag is true, assumes command includes '@@'
* in the beginning.
*
*/
function get_var_name(text, keyword, codetag){
var tag = 0;
if (codetag){
tag = 2;
}
var keywordRemoved = text.slice(keyword.length+tag+1);
var firstSpace = keywordRemoved.indexOf(" ");
// If there is no first space, parameter is until end of string
if (firstSpace==-1){
firstSpace = keywordRemoved.length;
}
var paramName = keywordRemoved.slice(0, firstSpace);
return [paramName,keywordRemoved];
}
/*
* Performs basic operation (incr, decr, mult), as
* demanded by a command expression.
*
* Operators (op) include "incr", "decr", and "mult".
*
*/
function perform_op(text,op){
// Determine parameter name.
var findParam = get_var_name(text,op,false);
var paramName = findParam[0];
// How much to multiply the parameter by.
var toOp = 1;
if (findParam[1].length > paramName.length){
toOp = parseFloat(get_var_name(findParam[1],paramName,false));
}
// Retrieve stored param
var paramVal = store.get(paramName);
// If it is a number, increment appropriately
if (isNumeric(paramVal)){
if (op=="incr"){
store.set(paramName, paramVal+toOp);
}
else if (op=="decr"){
store.set(paramName, paramVal-toOp);
}
else if (op=="mult"){
store.set(paramName, paramVal*toOp);
}
else{
console.log("Unrecognized operator. " + paramName + " is still " + paramVal + ".");
}
}
}
/*
* Determines if an expression that compares a parameter's
* ('param') actual value with another value ('val'), by way
* of an operator ('op').
*
* Currently supports the following operators for comparison:
* - eq (equals)
* - lt (less than)
* - gt (greater than)
* - geq (greater than or equal to)
* - leq (less than or equal to)
*
*/
function is_exp_true(param, op, val){
var isTrue = false;
var actualVal = store.get(param);
if (op=="eq"){
if (actualVal==val){
isTrue=true;
}
}
else if(op=="lt"){
if (actualVal<val){
isTrue=true;
}
}
else if(op=="gt"){
if (actualVal>val){
isTrue=true;
}
}
else if(op=="geq"){
if (actualVal>=val){
isTrue=true;
}
}
else if(op=="leq"){
if (actualVal<=val){
isTrue=true;
}
}
else{
console.log("Unrecognized operator. Returning false.");
}
return isTrue;
}
/*
* Check if an input String contains a number.
*/
function isNumeric(s) {
return !isNaN(parseFloat(s)) && isFinite(s);
}
})();