-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.js
97 lines (90 loc) · 2.16 KB
/
lib.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
Math.TAU = Math.PI * 2;
var Vec2 = function(x, y){
this.x = x || 0;
this.y = y || 0;
};
Vec2.prototype = {
add: function(a, b){
this.x = a.x + b.x;
this.y = a.y + b.y;
return this;
},
sub: function(a, b){
this.x = a.x - b.x;
this.y = a.y - b.y;
return this;
},
mag: function () {
return Math.sqrt(this.x * this.x + this.y * this.y);
},
magSq: function () {
return this.x * this.x + this.y * this.y;
},
normalize: function () {
var m = this.mag();
if(m===0){
this.x = 0;
this.y = 0;
return this;
}
this.x /= m;
this.y /= m;
return this;
},
scale: function (s) {
this.x *= s;
this.y *= s;
return this;
},
copy: function (a) {
this.x = a.x;
this.y = a.y;
return this;
},
lerp:function(v, a){
this.x += ( v.x - this.x ) * a;
this.y += ( v.y - this.y ) * a;
return this;
},
lerp2:function(v1, v2, a){
this.x = (1 - a) * v1.x + a * v2.x;
this.y = (1 - a) * v1.y + a * v2.y;
return this;
},
derp:function(v1, v2, a){
a = -Math.pow(a-1,2)+1;
this.x = (1 - a) * v1.x + a * v2.x;
this.y = (1 - a) * v1.y + a * v2.y;
return this;
},
dot:function(v){
return this.x * v.x + this.y * v.y;
},
zero:function () {
this.x = 0; this.y = 0;
return this;
}
};
Input = function() {
this.keys = []; this.keyPressFrame = []; this.keyReleaseFrame = [];
for(var i=0;i<255;i++) {
this.keys[i] = 0;
}
for(var i=0;i<255;i++) {
this.keyReleaseFrame[i] = 0;
}
for(var i=0;i<255;i++) {
this.keyPressFrame[i] = 0;
}
};
Input.prototype = {
keyPress:function (c) { this.keyPressFrame[c] = true; },
keyRelease:function (c) { this.keyPressFrame[c] = false; },
update:function() {
for(var i=0;i<255;i++) { if(this.keyPressFrame[i]) { ++this.keys[i]; }else{ this.keys[i] = 0; } }
},
bind:function () {
document.body.addEventListener("keydown", function (e) { this.keyPress(e.keyCode); }, false);
document.body.addEventListener("keyup", function (e) { this.keyRelease(e.keyCode); }, false);
},
};