This repository has been archived by the owner on Jan 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.js
119 lines (102 loc) · 2.86 KB
/
functions.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
/*
* GameIndus - A free online platform to imagine, create and publish your game with ease!
*
* GameIndus old 2d engine
* Copyright (c) 2015-2016 Maxime Malgorn (Utarwyn)
* <https://github.com/GameIndus/engine-2d-old>
*
*/
function loadJSON(path, success, error){
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function()
{
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
if (success){
if(xhr.responseText == "") xhr.responseText = "{}";
success(JSON.parse(xhr.responseText));
}
} else {
if (error)
error(xhr);
}
}
};
xhr.open("GET", path, true);
xhr.send();
}
function log(message, type){
if(!Config.debugMode) return false;
if(type!==undefined&&type=="error"){
console.error("[GIEngine] "+message);
}else{
console.log("[GIEngine] "+message);
}
}
function clone(obj) {
if(obj === null || typeof(obj) !== 'object')
return obj;
var temp = obj.constructor(); // changed
for(var key in obj) {
if(Object.prototype.hasOwnProperty.call(obj, key)) {
temp[key] = clone(obj[key]);
}
}
return temp;
}
function clone_object(o){
var n=Object.create(
Object.getPrototypeOf(o),
Object.getOwnPropertyNames(o).reduce(
function(prev,cur){
prev[cur]=Object.getOwnPropertyDescriptor(o,cur);
return prev;
},
{}
)
);
if(!Object.isExtensible(o)){Object.preventExtensions(n);}
if(Object.isSealed(o)){Object.seal(n);}
if(Object.isFrozen(o)){Object.freeze(n);}
return n;
}
function hexToRgb(hex) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
function getIndexFromXY(x, y, sizeH){
return (parseInt(y) * parseInt(sizeH) + parseInt(x));
}
function formatFilename(str){
if(str==null) return "";
return str.toString().split(" ").join("-").toLowerCase();
}
function lerp(value1, value2, n){
n = n < 0 ? 0 : n;
n = n > 1 ? 1 : n;
return value1 + ((value2 - value1) * n);
}
Array.prototype.clone = function() {
return this.slice(0);
};
String.prototype.isEmpty = function() {
return (this.length === 0 || !this.trim());
};
var cloneObject = function() {
var newObj = (this instanceof Array) ? [] : {};
for (var i in this) {
if (this[i] && typeof this[i] == "object") {
newObj[i] = this[i].clone();
}
else
{
newObj[i] = this[i];
}
}
return newObj;
};
Object.defineProperty( Object.prototype, "clone", {value: cloneObject, enumerable: false});