-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyFramework.js
More file actions
142 lines (121 loc) · 3.82 KB
/
myFramework.js
File metadata and controls
142 lines (121 loc) · 3.82 KB
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
//generates a random number between the rand of low and high
// Number is the number data type, when you assign a number to a varible you can call this function from that variable
// you can also call Number.protoype.randomMe()
Number.prototype.randomMe = function(low,high){
var randomNumber;
// hacking overloading
if((low == null) || (high == null)){
low=1;
high=100;
} else if((typeof low == "string") || (typeof high == "string")){
low=1;
high=100;
}
randomNumber = Math.round(Math.random() * (high-low)) + low;
return randomNumber;
}
//----------------------encode/decode
var aryAlphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
var arySpecChars = ['!','@','$',',','.','(',')','\'','?'];
var aryEncSpecChars = ['}','[','?','.','<','&','%',':','*'];
var checkSpaces = new RegExp(/^(\s)$/);
String.prototype.encode = function(){
var myStr = this.toLowerCase();
var oneChar;
var output = "";
for (var i = 0; i <= myStr.length-1; i++) {
oneChar = myStr.charAt(i);
for (var y = 0; y <= aryAlphabet.length -1; y++) {
if (oneChar == aryAlphabet[y]) {
output += aryAlphabet[aryAlphabet.length-y-1];
break;
}else if(checkSpaces.test(oneChar)){
output += "#";
break;
}
}
output+=checkSpecChar(oneChar,false);
}
return output;
}
String.prototype.decode = function(){
var myStr = this.toLowerCase();
var oneChar;
var output = "";
for (var i = 0; i <= myStr.length-1; i++) {
oneChar = myStr.charAt(i);
for (var y = 0; y <= aryAlphabet.length -1; y++) {
if (oneChar == aryAlphabet[y]) {
output += aryAlphabet[Math.abs(aryAlphabet.length-1 - y)];
break;
}else if(oneChar == "#"){
output += " ";
break;
}
}
output+=checkSpecChar(oneChar,true);
}
return output;
}
function checkSpecChar(myChar,isDecode){
for (var i = arySpecChars.length - 1; i >= 0; i--) {
if(isDecode){
if(myChar == aryEncSpecChars[i]){
return arySpecChars[i];
}
}else{
if(myChar == arySpecChars[i]){
return aryEncSpecChars[i];
}
}
}
return "";
}
//------------------------------------------------------animation
//used to create a game loop
// call setInterval("requestAnimFrame()",1000/targetFPS)
//a function like update() would go in requestAnimFrame as a param
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback){
callback();
};
})();
window.cancelRequestAnimFrame = ( function() {
return window.cancelAnimationFrame ||
window.webkitCancelRequestAnimationFrame ||
window.mozCancelRequestAnimationFrame ||
window.oCancelRequestAnimationFrame ||
window.msCancelRequestAnimationFrame ||
clearTimeout
})();
//---------------------------------------------------------------CANVAS 2D
debugObjPos(obj,objName,canvas,txtColor,x,y){
canvas.fillStyle = txtColor;
canvas.font = "14px Arial, sans-serif";
canvas.fillText(objName + " X : " + obj.x, x, y );
canvas.fillText(objName + " Y : " + obj.y, x, y+20 );
canvas.fillText(objName + " Height : " + obj.height, x, y+40 );
canvas.fillText(objName + " Width : " + obj.width, x, y+60 );
}
function boxCollides(obj1,obj2){
return (obj1.x-obj1.r <= obj2.x + obj2.r && //1left is to the left 2right
obj2.x-obj1.r <= obj1.x + obj1.r && //2left is to the left of 1 right
obj1.y-obj1.r <= obj2.y + obj2.r && // 1top is to the top of 2bottom
obj2.y-obj1.r <= obj1.y + obj1.r)
}
function radCollides(obj1,obj2) {
distanceX = Math.abs((obj1.x - obj2.x));
distanceY = Math.abs((obj1.y - obj2.y));
hy = Math.sqrt((distanceX*distanceX) + (distanceY*distanceY));
hy -= (obj1.r + obj2.r);
if(hy <=0){
return true;
}else{
return false;
}
}