-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathD$E_Core.js
332 lines (292 loc) · 8.72 KB
/
D$E_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
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
//==============================================================================
// Dragon Engine (D$E) Core
// D$E_Core.js
// Version 1.1.0
//==============================================================================
/*
* Copyright 2015 Ramiro Rojo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*:
* @plugindesc The core script used by all of the Dragon Engine plugins.
*
* @param Allow Plugin Commands
* Determines if this plugin and it's extensions will allow plugin commands.
* Default: yes
* @default yes
*
* @param Plugin Command name
* The name used as plugin command.
* Default: D$E
* @default D$E
*
* @help
*
*
*/
var Imported = Imported || {};
if (PluginManager.register) {
PluginManager.register("D$E_Core", "1.0.0", {
"email": "[email protected]",
"website": "http://binarychest.wordpress.com",
"name": "Ramiro Rojo"
}, "2015-10-26");
} else {
Imported["D$E_Core"] = "1.0.0";
}
if (!window.MVC) {
throw new Error("The plugin 'D$E_Core' require the 'MVCommons plugin' to work properly!");
}
if (!Object.keys(PluginManager.parameters("D$E_Core")).length) {
throw new Error("You seem to have put to 'D$E_Core' a different name in your plugin folder, this won't work!");
}
var D$E = (function (oldD$E) {
"use strict";
var editorParams = PluginManager.parameters("D$E_Core");
var $ = {};
var params = {};
var _pluginCommands = {};
/**
* Restores the previous value stored in the D$E variable.
*
* @return The current value of the dragon engine.
*/
$.noConflict = function () {
D$E = oldD$E;
return $;
};
if (MVC && MVC.naturalBoolean) {
// I just take it from MVCommons then
$.naturalBoolean = MVC.naturalBoolean;
} else {
/**
* Allows to match your string as any of this to true:
* - true
* - yes
* - active
* - enabled
* - on
* - y
*
* @param text The text to check.
* @return true if the text matches any of that text, or false.
* @note Value is case insensitive.
*/
$.naturalBoolean = function (text) {
if ( text.match(/(y(es)?)|true|on|active|enabled/gi) ) {
return true;
}
return false;
};
}
$.number = function (value, max, min) {
var n = Number(value || 0);
if (max !== null && n > max) {
return max;
}
if (min !== null && n < min) {
return min;
}
return n;
}
$.trim = function (value) {
return ('' + value).trim();
}
$.asFunction = function (value) {
return Function("return " + value + ';');
}
$.parseList = function (list, schema, separator) {
return list.split(separator).map(function (i) {
return $.parseParamType(schema, i)
});
}
$.parseHash = function (list, schema, separator, keySeparator) {
var result = {};
function each(i) {
var list = i.split(keySeparator);
result[list[0].trim()] = $.parseParamType(schema, list[1]);
};
list.split(separator).forEach(each);
return result;
}
$.parseAsStringOrNumber = function (value) {
var t = $.trim(value);
return isNaN(t) ? t : Number(value || 0);
}
$.parseParamType = function (schema, value) {
var s, t, v, result, filter;
filter = true;
if (typeof schema == 'string') {
t = schema;
s = {};
} else {
t = schema.type || null;
s = schema;
}
v = s.before ? s.before(value) : value;
switch ((t || 'string').toLowerCase()) {
case 'bool': case 'boolean':
result = $.naturalBoolean(v);
break;
case 'number': case 'nul':
result = $.number(v, s.max || null, s.min || null);
break;
case 'list': case 'array': case 'arr': case 'ary':
result = $.parseList(v, s.of || '', s.separator || s.sep || ',');
break;
case 'function': case 'method': case 'funct': case 'lambda': case 'fun':
result = $.asFunction(v);
break;
case 'hash': case 'object': case 'obj':
result = $.parseHash(v, s.of || '', s.separator || s.sep || ',', s.pairSeparator || ':' );
break;
case 'json':
result = JSON.parse(v);
case 'str': case 'string': default:
result = v.trim();
break;
}
if (s.after && filter) {
return s.after(result);
}
return result;
}
$.parametersFromSchema = function (editorParameters, schema) {
var result = {};
for (var p in schema) {
result[p] = $.parseParamType(schema[p], editorParameters[p] || '');
}
return result;
}
/**
* Adds an object that can handle commands for the Dragon Engine.
* It will be called inside D$E Plugin command to avoid name collitions.
* When the command is requested, this plugin will call the method
* _handleCommand of the handler object.
*
* @param name The name of the command.
* @param handlerObject The object than handles the event.
*
*/
$.addCommandHandle = function (name, handlerObject) {
_pluginCommands[name] = handlerObject;
};
if (MVC && MVC.safeEval) {
// Taking it from MVC.
$.safeEval = MVC.safeEval;
} else {
/**
* Evaluates a context with some safety measure to stop it from breaking all.
*
* @param text The text to evaluate.
* @return The result of the expression or null if something fails.
*
*/
$.safeEval = function (text) {
try {
return eval(text);
} catch(e) {
console.error(e); // print the error as error anyway
return null;
}
};
}
if (MVC && MVC.degToRad) {
// Taking it from MVC.
$.degToRad = MVC.degToRad;
} else {
/**
* Converts degrees into radians.
*
* @param deg Degrees to convert
* @return Radiants equivalent to those degrees.
*
*/
$.degToRad = function (deg) {
return deg * Math.PI / 180;
}
}
$.copyAttributes = function (src, dst) {
for (var p in src) {
if (src.hasOwnProperty(p)) {
dst[p] = dst[p] || src[p];
}
}
}
$.merge = function () {
var result = {};
var length = arguments.length;
for (var i = 0; i < length; ++i) {
$.copyAttributes(arguments[i], result);
}
return result;
}
if (MVC && MVC.radToDeg) {
$.radToDeg = MVC.radToDeg;
} else {
/**
* Converts radians into degrees.
*
* @param rad Radians to convert
* @return Degrees equivalent to those radians.
*
*/
$.radToDeg = function (rad) {
return rad * 180 / Math.PI;
}
}
$.ensureParameters = function (name) {
if (!Object.keys(PluginManager.parameters(name)).length) {
throw new Error("You seem to have put to the plugin '" + name + "' a different name in your plugin folder, this won't work!");
}
};
function fireEventCommand(name, args) {
if (_pluginCommands[name]) {
_pluginCommands['name']._handleCommand.apply(this, args);
return;
}
throw new Error("The Dragon Engine doesn't recognize the plugin command '" + name + "'!");
}
$.PARAMETERS = {};
$.PARAMETERS['Core'] = params;
params.allowPluginCommands = $.naturalBoolean(editorParams["Allow Plugin Commands"] || 'no');
params.pluginCommandName = editorParams['Plugin Command name'] || 'D$E';
if (!params.allowPluginCommands) {
$.addCommandHandle = function () {};
}
// Adding plugin command handling
var Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
Game_Interpreter.prototype.pluginCommand = function(command, args) {
Game_Interpreter_pluginCommand.call(this, command, args);
if (command === params.pluginCommandName) {
var subArgs = Array.prototype.slice.call(args, 1);
fireEventCommand.call(this, args[0], subArgs);
}
};
$.mouse = new Point(0, 0);
(function () { // must protect from pollution
function _onMouseMove(event) {
$.mouse.x = Graphics.pageToCanvasX(event.pageX);
$.mouse.y = Graphics.pageToCanvasY(event.pageY);
};
function _onTouchMove(event) {
for (var i = 0; i < event.changedTouches.length; i++) {
var touch = event.changedTouches[i];
$.mouse.x = Graphics.pageToCanvasX(touch.pageX);
$.mouse.y = Graphics.pageToCanvasY(touch.pageY);
}
};
document.addEventListener('mousemove', _onMouseMove);
document.addEventListener('touchmove', _onTouchMove);
})();
return $;
})(D$E || null);