forked from textadventures/questkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.js
174 lines (145 loc) · 3.97 KB
/
core.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
/* jshint quotmark: single */
var questkit = {};
questkit.ui = {};
String.prototype.format = function () {
var args = arguments;
return this.replace(/{(\d+)}/g, function (match, number) {
return typeof args[number] != 'undefined' ? args[number] : match;
});
};
(function () {
'use strict';
var world = {
attributes: {}
};
questkit.init = function (data) {
world.scripts = data.scripts || {};
world.objects = data.objects || [];
world.commands = data.commands || [];
world.exits = data.exits || [];
world.regexes = data.regexes || {};
world.templates = data.templates || {};
world.walkthroughs = data.walkthroughs || {};
questkit.initPov();
questkit.startLocation();
questkit.ui.init();
};
questkit.povParent = function () {
return get(get('pov'), 'parent');
};
questkit.allObjects = function () {
return world.objects;
};
questkit.allCommands = function () {
return world.commands;
};
questkit.allExits = function () {
return world.exits;
};
questkit.allWalkthroughs = function () {
return world.walkthroughs;
};
questkit.commandRegex = function (command) {
return world.regexes[command];
};
questkit.displayAlias = function (object) {
var alias = get(object, 'alias');
if (alias) return alias;
return object;
};
questkit.subjectPronoun = function (object) {
return get(object, 'subjectpronoun') || questkit.defaultSubjectPronoun(object);
};
questkit.objectPronoun = function (object) {
return get(object, 'objectpronoun') || questkit.defaultObjectPronoun(object);
};
questkit.template = function (template) {
return world.templates[template];
};
questkit.get = function (arg1, arg2) {
var attribute = arg1;
if (arg2) {
attribute = arg1 + '.' + arg2;
}
var value = world.attributes[attribute];
if (value === undefined) return null;
return JSON.parse(value);
};
questkit.set = function (arg1, arg2, arg3, isUndoing) {
var attribute = arg1;
var value = arg2;
if (arg3) {
attribute = arg1 + '.' + arg2;
value = arg3;
}
if (!isUndoing) logOldValue(attribute);
world.attributes[attribute] = JSON.stringify(value);
};
questkit.startTransaction = function (command) {
var current = get('~undo.current') || 0;
current++;
set('~undo.current', current);
set('~undo', 'log' + current, {});
set('~undo', 'command' + current, command);
};
var logOldValue = function (attribute) {
if (attribute.indexOf('~undo.') === 0) return;
var current = get('~undo.current');
if (!current) return;
var log = get('~undo', 'log' + current);
if (attribute in log) return;
log[attribute] = get(attribute);
set('~undo', 'log' + current, log);
};
questkit.undo = function () {
var current = get('~undo.current') || 0;
if (!current) {
msg(questkit.template('NothingToUndo'));
return;
}
var log = get('~undo', 'log' + current);
msg(questkit.template('UndoTurn').format(get('~undo', 'command' + current)));
for (var attribute in log) {
set(attribute, log[attribute], null, true);
}
current--;
set('~undo.current', current);
};
questkit.getscript = function (arg1, arg2) {
var attribute = arg1;
if (arg2) {
attribute = arg1 + '.' + arg2;
}
return world.scripts[attribute];
};
questkit.msg = function (text) {
questkit.ui.addText(text);
};
questkit.initPov = function (oldPov) {
if (oldPov) {
set(oldPov, 'alias', get(oldPov, 'externalalias'));
set(oldPov, 'alt', get(oldPov, 'externalalt'));
set(oldPov, 'look', get(oldPov, 'externallook'));
}
var pov = get('pov');
set(pov, 'externalalias', get(pov, 'alias'));
set(pov, 'externalalt', get(pov, 'alt'));
set(pov, 'externallook', get(pov, 'look'));
set(pov, 'alias', get(pov, 'povalias'));
set(pov, 'alt', get(pov, 'povalt'));
set(pov, 'look', get(pov, 'povlook'));
};
})();
var get = questkit.get;
var set = questkit.set;
var getscript = questkit.getscript;
var msg = questkit.msg;
var initData = {
attributes: {},
scripts: {},
objects: [],
commands: [],
exits: [],
regexes: {},
walkthroughs: [],
};