forked from mozilla/BrowserQuest
-
Notifications
You must be signed in to change notification settings - Fork 219
/
utils.js
83 lines (68 loc) · 1.71 KB
/
utils.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
var sanitizer = require('sanitizer');
var Types = require('../../shared/js/gametypes');
var Utils = {};
module.exports = Utils;
Utils.sanitize = function (string) {
// Strip unsafe tags, then escape as html entities.
return sanitizer.escape(sanitizer.sanitize(string));
};
Utils.random = function (range) {
return Math.floor(Math.random() * range);
};
Utils.randomRange = function (min, max) {
return min + (Math.random() * (max - min));
};
Utils.randomInt = function (min, max) {
return min + Math.floor(Math.random() * (max - min + 1));
};
Utils.clamp = function (min, max, value) {
if (value < min) {
return min;
} else if (value > max) {
return max;
} else {
return value;
}
};
Utils.randomOrientation = function () {
var o, r = Utils.random(4);
if (r === 0) {
o = Types.Orientations.LEFT;
}
if (r === 1) {
o = Types.Orientations.RIGHT;
}
if (r === 2) {
o = Types.Orientations.UP;
}
if (r === 3) {
o = Types.Orientations.DOWN;
}
return o;
};
Utils.Mixin = function (target, source) {
if (source) {
for (var key, keys = Object.keys(source), l = keys.length; l--;) {
key = keys[l];
if (source.hasOwnProperty(key)) {
target[key] = source[key];
}
}
}
return target;
};
Utils.distanceTo = function (x, y, x2, y2) {
var distX = Math.abs(x - x2),
distY = Math.abs(y - y2);
return (distX > distY) ? distX : distY;
};
Utils.NaN2Zero = function(num){
if(isNaN(num*1)){
return 0;
} else{
return num*1;
}
};
Utils.trueFalse = function(bool){
return bool === "true" ? true : false;
}