BlueGit

timfa frameworkv3

format

timfa Tim · pushed by timfa

Authored 2022-10-20 · 4 years ago

Hash
24db7846035b309bfe74b3e9a89a5a9ff15d24ef
Parent
c77b377c
5 files changed +103 −123
.editorconfig added +0 −0
.vscode/settings.json added +10 −0
@@ -0,0 +1,10 @@
1 {
2 "javascript.format.placeOpenBraceOnNewLineForFunctions": true,
3 "typescript.format.placeOpenBraceOnNewLineForFunctions": true,
4 "editor.formatOnSave": true,
5 "javascript.format.placeOpenBraceOnNewLineForControlBlocks": true,
6 "javascript.format.semicolons": "insert",
7 "javascript.format.insertSpaceBeforeFunctionParenthesis": true,
8 "javascript.format.insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": false,
9 "javascript.format.insertSpaceAfterConstructor": true
10 }
@@ -0,0 +1,10 @@
1{
2 "javascript.format.placeOpenBraceOnNewLineForFunctions": true,
3 "typescript.format.placeOpenBraceOnNewLineForFunctions": true,
4 "editor.formatOnSave": true,
5 "javascript.format.placeOpenBraceOnNewLineForControlBlocks": true,
6 "javascript.format.semicolons": "insert",
7 "javascript.format.insertSpaceBeforeFunctionParenthesis": true,
8 "javascript.format.insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": false,
9 "javascript.format.insertSpaceAfterConstructor": true
10}
framework/engine/objectComponents/collider.js +44 −63
@@ -10,17 +10,14 @@ class Collider
10 10
11 11 update(deltaTime)
12 12 {
13 if(this.enabled)
14 {
13 if (this.enabled) {
15 14 let pressed = GameInput.mousePressed();
16 15 let held = GameInput.mouseHeld();
17 if (pressed.left || pressed.middle || pressed.right)
18 {
16 if (pressed.left || pressed.middle || pressed.right) {
19 17 this.checkClick(true, pressed);
20 18 }
21 19
22 if (held.left || held.middle || held.right)
23 {
20 if (held.left || held.middle || held.right) {
24 21 this.checkClick(false, held);
25 22 }
26 23
@@ -32,26 +29,21 @@ class Collider
32 29 {
33 30 var hit = false;
34 31 var clickPos = GameInput.mousePosition.subtract(this.gameObject.scene.position).subtract(this.gameObject.scene.cameraPosition);
35
32
36 33 var hw = this.gameObject.transform.size.x / 2;
37 34 var hh = this.gameObject.transform.size.y / 2;
38
39 if(clickPos.x > this.gameObject.transform.position.x - hw && clickPos.x < this.gameObject.transform.position.x + hw)
40 {
41 if(clickPos.y > this.gameObject.transform.position.y - hh && clickPos.y < this.gameObject.transform.position.y + hh)
42 {
35
36 if (clickPos.x > this.gameObject.transform.position.x - hw && clickPos.x < this.gameObject.transform.position.x + hw) {
37 if (clickPos.y > this.gameObject.transform.position.y - hh && clickPos.y < this.gameObject.transform.position.y + hh) {
43 38 hit = true;
44 39 }
45 40 }
46 41
47 if (hit)
48 {
49 if (thisFrame)
50 {
42 if (hit) {
43 if (thisFrame) {
51 44 this.gameObject.onClick && this.gameObject.onClick(btns);
52 45 }
53 else
54 {
46 else {
55 47 this.gameObject.onMouseHeld && this.gameObject.onMouseHeld(btns);
56 48 }
57 49 }
@@ -62,10 +54,8 @@ class Collider
62 54 var hw = this.gameObject.transform.size.x / 2;
63 55 var hh = this.gameObject.transform.size.y / 2;
64 56
65 if(point.x > this.gameObject.transform.position.x - hw && point.x < this.gameObject.transform.position.x + hw)
66 {
67 if(point.y > this.gameObject.transform.position.y - hh && point.y < this.gameObject.transform.position.y + hh)
68 {
57 if (point.x > this.gameObject.transform.position.x - hw && point.x < this.gameObject.transform.position.x + hw) {
58 if (point.y > this.gameObject.transform.position.y - hh && point.y < this.gameObject.transform.position.y + hh) {
69 59 return true;
70 60 }
71 61 }
@@ -75,66 +65,57 @@ class Collider
75 65
76 66 checkCollisions()
77 67 {
78 if(this.enabled && !this.gameObject.destroyed)
79 {
80 for(var i = 0; i < this.gameObject.scene.activeObjects.length; i++)
81 {
82 if(this.gameObject.scene.activeObjects[i] != this && this.gameObject.scene.activeObjects[i].collider.enabled)
83 {
68 if (this.enabled && !this.gameObject.destroyed) {
69 for (var i = 0; i < this.gameObject.scene.activeObjects.length; i++) {
70 if (this.gameObject.scene.activeObjects[i] != this && this.gameObject.scene.activeObjects[i].collider.enabled) {
84 71 var hit = false;
85 72 var o2 = this.gameObject.scene.activeObjects[i];
86
73
87 74 //hw = halfWidth, hh = halfHeight
88 75 var hw = this.gameObject.transform.size.x / 2;
89 76 var hh = this.gameObject.transform.size.y / 2;
90
77
91 78 var ohw = o2.transform.size.x / 2;
92 79 var ohh = o2.transform.size.y / 2;
93
80
94 81 //CHECK FROM OBJ PERSPECTIVE
95 var p =
96 [
97 [this.gameObject.transform.position.x - hw, this.gameObject.transform.position.y - hh],
98 [this.gameObject.transform.position.x + hw, this.gameObject.transform.position.y - hh],
99 [this.gameObject.transform.position.x - hw, this.gameObject.transform.position.y + hh],
100 [this.gameObject.transform.position.x + hw, this.gameObject.transform.position.y + hh]
101 ];
102
103 for(var pi = 0; pi < p.length; pi++)
104 {
105 if(p[pi][0] > o2.transform.position.x - ohw && p[pi][0] < o2.transform.position.x + ohw)
106 {
107 if(p[pi][1] > o2.transform.position.y - ohh && p[pi][1] < o2.transform.position.y + ohh)
108 {
82 var p =
83 [
84 [this.gameObject.transform.position.x - hw, this.gameObject.transform.position.y - hh],
85 [this.gameObject.transform.position.x + hw, this.gameObject.transform.position.y - hh],
86 [this.gameObject.transform.position.x - hw, this.gameObject.transform.position.y + hh],
87 [this.gameObject.transform.position.x + hw, this.gameObject.transform.position.y + hh]
88 ];
89
90 for (var pi = 0; pi < p.length; pi++) {
91 if (p[pi][0] > o2.transform.position.x - ohw && p[pi][0] < o2.transform.position.x + ohw) {
92 if (p[pi][1] > o2.transform.position.y - ohh && p[pi][1] < o2.transform.position.y + ohh) {
109 93 hit = true;
110 94 }
111 95 }
112 96 }
113
97
114 98 //CHECK FROM OTHER PERSPECTIVE
115 var p =
116 [
117 [o2.transform.position.x - ohw, o2.transform.position.y - ohh],
118 [o2.transform.position.x + ohw, o2.transform.position.y - ohh],
119 [o2.transform.position.x - ohw, o2.transform.position.y + ohh],
120 [o2.transform.position.x + ohw, o2.transform.position.y + ohh]
121 ];
122
123 for(var pi = 0; pi < p.length; pi++)
124 {
125 if(p[pi][0] > this.gameObject.transform.position.x - hw && p[pi][0] < this.gameObject.transform.position.x + hw)
126 {
127 if(p[pi][1] > this.gameObject.transform.position.y - hh && p[pi][1] < this.gameObject.transform.position.y + hh)
128 {
99 var p =
100 [
101 [o2.transform.position.x - ohw, o2.transform.position.y - ohh],
102 [o2.transform.position.x + ohw, o2.transform.position.y - ohh],
103 [o2.transform.position.x - ohw, o2.transform.position.y + ohh],
104 [o2.transform.position.x + ohw, o2.transform.position.y + ohh]
105 ];
106
107 for (var pi = 0; pi < p.length; pi++) {
108 if (p[pi][0] > this.gameObject.transform.position.x - hw && p[pi][0] < this.gameObject.transform.position.x + hw) {
109 if (p[pi][1] > this.gameObject.transform.position.y - hh && p[pi][1] < this.gameObject.transform.position.y + hh) {
129 110 hit = true;
130 111 }
131 112 }
132 113 }
133
134 if(hit)
114
115 if (hit)
135 116 if (!this.gameObject.destroyed && typeof this.gameObject.onCollision == 'function') { this.gameObject.onCollision(o2); }
136 117 }
137 }
118 }
138 119 }
139 120 }
140 121 }
@@ -10,17 +10,14 @@ class Collider
10
10
11 update(deltaTime)
11 update(deltaTime)
12 {
12 {
13 if(this.enabled)
13 if (this.enabled) {
14 {
15 let pressed = GameInput.mousePressed();
14 let pressed = GameInput.mousePressed();
16 let held = GameInput.mouseHeld();
15 let held = GameInput.mouseHeld();
17 if (pressed.left || pressed.middle || pressed.right)
16 if (pressed.left || pressed.middle || pressed.right) {
18 {
19 this.checkClick(true, pressed);
17 this.checkClick(true, pressed);
20 }
18 }
21
19
22 if (held.left || held.middle || held.right)
20 if (held.left || held.middle || held.right) {
23 {
24 this.checkClick(false, held);
21 this.checkClick(false, held);
25 }
22 }
26
23
@@ -32,26 +29,21 @@ class Collider
32 {
29 {
33 var hit = false;
30 var hit = false;
34 var clickPos = GameInput.mousePosition.subtract(this.gameObject.scene.position).subtract(this.gameObject.scene.cameraPosition);
31 var clickPos = GameInput.mousePosition.subtract(this.gameObject.scene.position).subtract(this.gameObject.scene.cameraPosition);
35
32
36 var hw = this.gameObject.transform.size.x / 2;
33 var hw = this.gameObject.transform.size.x / 2;
37 var hh = this.gameObject.transform.size.y / 2;
34 var hh = this.gameObject.transform.size.y / 2;
38
35
39 if(clickPos.x > this.gameObject.transform.position.x - hw && clickPos.x < this.gameObject.transform.position.x + hw)
36 if (clickPos.x > this.gameObject.transform.position.x - hw && clickPos.x < this.gameObject.transform.position.x + hw) {
40 {
37 if (clickPos.y > this.gameObject.transform.position.y - hh && clickPos.y < this.gameObject.transform.position.y + hh) {
41 if(clickPos.y > this.gameObject.transform.position.y - hh && clickPos.y < this.gameObject.transform.position.y + hh)
42 {
43 hit = true;
38 hit = true;
44 }
39 }
45 }
40 }
46
41
47 if (hit)
42 if (hit) {
48 {
43 if (thisFrame) {
49 if (thisFrame)
50 {
51 this.gameObject.onClick && this.gameObject.onClick(btns);
44 this.gameObject.onClick && this.gameObject.onClick(btns);
52 }
45 }
53 else
46 else {
54 {
55 this.gameObject.onMouseHeld && this.gameObject.onMouseHeld(btns);
47 this.gameObject.onMouseHeld && this.gameObject.onMouseHeld(btns);
56 }
48 }
57 }
49 }
@@ -62,10 +54,8 @@ class Collider
62 var hw = this.gameObject.transform.size.x / 2;
54 var hw = this.gameObject.transform.size.x / 2;
63 var hh = this.gameObject.transform.size.y / 2;
55 var hh = this.gameObject.transform.size.y / 2;
64
56
65 if(point.x > this.gameObject.transform.position.x - hw && point.x < this.gameObject.transform.position.x + hw)
57 if (point.x > this.gameObject.transform.position.x - hw && point.x < this.gameObject.transform.position.x + hw) {
66 {
58 if (point.y > this.gameObject.transform.position.y - hh && point.y < this.gameObject.transform.position.y + hh) {
67 if(point.y > this.gameObject.transform.position.y - hh && point.y < this.gameObject.transform.position.y + hh)
68 {
69 return true;
59 return true;
70 }
60 }
71 }
61 }
@@ -75,66 +65,57 @@ class Collider
75
65
76 checkCollisions()
66 checkCollisions()
77 {
67 {
78 if(this.enabled && !this.gameObject.destroyed)
68 if (this.enabled && !this.gameObject.destroyed) {
79 {
69 for (var i = 0; i < this.gameObject.scene.activeObjects.length; i++) {
80 for(var i = 0; i < this.gameObject.scene.activeObjects.length; i++)
70 if (this.gameObject.scene.activeObjects[i] != this && this.gameObject.scene.activeObjects[i].collider.enabled) {
81 {
82 if(this.gameObject.scene.activeObjects[i] != this && this.gameObject.scene.activeObjects[i].collider.enabled)
83 {
84 var hit = false;
71 var hit = false;
85 var o2 = this.gameObject.scene.activeObjects[i];
72 var o2 = this.gameObject.scene.activeObjects[i];
86
73
87 //hw = halfWidth, hh = halfHeight
74 //hw = halfWidth, hh = halfHeight
88 var hw = this.gameObject.transform.size.x / 2;
75 var hw = this.gameObject.transform.size.x / 2;
89 var hh = this.gameObject.transform.size.y / 2;
76 var hh = this.gameObject.transform.size.y / 2;
90
77
91 var ohw = o2.transform.size.x / 2;
78 var ohw = o2.transform.size.x / 2;
92 var ohh = o2.transform.size.y / 2;
79 var ohh = o2.transform.size.y / 2;
93
80
94 //CHECK FROM OBJ PERSPECTIVE
81 //CHECK FROM OBJ PERSPECTIVE
95 var p =
82 var p =
96 [
83 [
97 [this.gameObject.transform.position.x - hw, this.gameObject.transform.position.y - hh],
84 [this.gameObject.transform.position.x - hw, this.gameObject.transform.position.y - hh],
98 [this.gameObject.transform.position.x + hw, this.gameObject.transform.position.y - hh],
85 [this.gameObject.transform.position.x + hw, this.gameObject.transform.position.y - hh],
99 [this.gameObject.transform.position.x - hw, this.gameObject.transform.position.y + hh],
86 [this.gameObject.transform.position.x - hw, this.gameObject.transform.position.y + hh],
100 [this.gameObject.transform.position.x + hw, this.gameObject.transform.position.y + hh]
87 [this.gameObject.transform.position.x + hw, this.gameObject.transform.position.y + hh]
101 ];
88 ];
102
89
103 for(var pi = 0; pi < p.length; pi++)
90 for (var pi = 0; pi < p.length; pi++) {
104 {
91 if (p[pi][0] > o2.transform.position.x - ohw && p[pi][0] < o2.transform.position.x + ohw) {
105 if(p[pi][0] > o2.transform.position.x - ohw && p[pi][0] < o2.transform.position.x + ohw)
92 if (p[pi][1] > o2.transform.position.y - ohh && p[pi][1] < o2.transform.position.y + ohh) {
106 {
107 if(p[pi][1] > o2.transform.position.y - ohh && p[pi][1] < o2.transform.position.y + ohh)
108 {
109 hit = true;
93 hit = true;
110 }
94 }
111 }
95 }
112 }
96 }
113
97
114 //CHECK FROM OTHER PERSPECTIVE
98 //CHECK FROM OTHER PERSPECTIVE
115 var p =
99 var p =
116 [
100 [
117 [o2.transform.position.x - ohw, o2.transform.position.y - ohh],
101 [o2.transform.position.x - ohw, o2.transform.position.y - ohh],
118 [o2.transform.position.x + ohw, o2.transform.position.y - ohh],
102 [o2.transform.position.x + ohw, o2.transform.position.y - ohh],
119 [o2.transform.position.x - ohw, o2.transform.position.y + ohh],
103 [o2.transform.position.x - ohw, o2.transform.position.y + ohh],
120 [o2.transform.position.x + ohw, o2.transform.position.y + ohh]
104 [o2.transform.position.x + ohw, o2.transform.position.y + ohh]
121 ];
105 ];
122
106
123 for(var pi = 0; pi < p.length; pi++)
107 for (var pi = 0; pi < p.length; pi++) {
124 {
108 if (p[pi][0] > this.gameObject.transform.position.x - hw && p[pi][0] < this.gameObject.transform.position.x + hw) {
125 if(p[pi][0] > this.gameObject.transform.position.x - hw && p[pi][0] < this.gameObject.transform.position.x + hw)
109 if (p[pi][1] > this.gameObject.transform.position.y - hh && p[pi][1] < this.gameObject.transform.position.y + hh) {
126 {
127 if(p[pi][1] > this.gameObject.transform.position.y - hh && p[pi][1] < this.gameObject.transform.position.y + hh)
128 {
129 hit = true;
110 hit = true;
130 }
111 }
131 }
112 }
132 }
113 }
133
114
134 if(hit)
115 if (hit)
135 if (!this.gameObject.destroyed && typeof this.gameObject.onCollision == 'function') { this.gameObject.onCollision(o2); }
116 if (!this.gameObject.destroyed && typeof this.gameObject.onCollision == 'function') { this.gameObject.onCollision(o2); }
136 }
117 }
137 }
118 }
138 }
119 }
139 }
120 }
140}
121}
framework/extensions/extentions.js +11 −21
@@ -2,51 +2,41 @@
2 2 * Extending exising functionality
3 3 */
4 4
5 Array.prototype.remove = function(element)
6 {
5 Array.prototype.remove = function (element) {
7 6 var index = this.indexOf(element);
8 7
9 if (index > -1)
10 {
8 if (index > -1) {
11 9 this.splice(index, 1);
12 10 }
13 else
14 {
11 else {
15 12 console.error("Element not part of array!", this, element);
16 13 }
17 14 }
18 15
19 Array.prototype.removeAt = function(index)
20 {
21 if (index > -1)
22 {
16 Array.prototype.removeAt = function (index) {
17 if (index > -1) {
23 18 this.splice(index, 1);
24 19 }
25 20 }
26 21
27 Array.prototype.contains = function(element)
28 {
22 Array.prototype.contains = function (element) {
29 23 var index = this.indexOf(element);
30 24
31 25 return index > -1;
32 26 }
33 27
34 Math.clamp = function(number, min, max)
35 {
28 Math.clamp = function (number, min, max) {
36 29 return Math.min(max, Math.max(min, number));
37 30 }
38 31
39 Math.randomRange = function(min, max)
40 {
32 Math.randomRange = function (min, max) {
41 33 return min + (Math.random() * (max - min));
42 34 }
43 35
44 Math.lerp = function(a, b, t)
45 {
36 Math.lerp = function (a, b, t) {
46 37 return a + (b - a) * t;
47 38 }
48 39
49 Array.prototype.random = function ()
50 {
51 return this[Math.floor((Math.random()*this.length))];
40 Array.prototype.random = function () {
41 return this[Math.floor((Math.random() * this.length))];
52 42 }
@@ -2,51 +2,41 @@
2 * Extending exising functionality
2 * Extending exising functionality
3 */
3 */
4
4
5Array.prototype.remove = function(element)
5Array.prototype.remove = function (element) {
6{
7 var index = this.indexOf(element);
6 var index = this.indexOf(element);
8
7
9 if (index > -1)
8 if (index > -1) {
10 {
11 this.splice(index, 1);
9 this.splice(index, 1);
12 }
10 }
13 else
11 else {
14 {
15 console.error("Element not part of array!", this, element);
12 console.error("Element not part of array!", this, element);
16 }
13 }
17}
14}
18
15
19Array.prototype.removeAt = function(index)
16Array.prototype.removeAt = function (index) {
20{
17 if (index > -1) {
21 if (index > -1)
22 {
23 this.splice(index, 1);
18 this.splice(index, 1);
24 }
19 }
25}
20}
26
21
27Array.prototype.contains = function(element)
22Array.prototype.contains = function (element) {
28{
29 var index = this.indexOf(element);
23 var index = this.indexOf(element);
30
24
31 return index > -1;
25 return index > -1;
32}
26}
33
27
34Math.clamp = function(number, min, max)
28Math.clamp = function (number, min, max) {
35{
36 return Math.min(max, Math.max(min, number));
29 return Math.min(max, Math.max(min, number));
37}
30}
38
31
39Math.randomRange = function(min, max)
32Math.randomRange = function (min, max) {
40{
41 return min + (Math.random() * (max - min));
33 return min + (Math.random() * (max - min));
42}
34}
43
35
44Math.lerp = function(a, b, t)
36Math.lerp = function (a, b, t) {
45{
46 return a + (b - a) * t;
37 return a + (b - a) * t;
47}
38}
48
39
49Array.prototype.random = function ()
40Array.prototype.random = function () {
50{
41 return this[Math.floor((Math.random() * this.length))];
51 return this[Math.floor((Math.random()*this.length))];
52}
42}
framework/util/vector.js +38 −39
@@ -6,10 +6,10 @@ class vector
6 6 constructor(x, y)
7 7 {
8 8 this.x = x;
9
9
10 10 this.y = y;
11 11 }
12
12
13 13 get length()
14 14 {
15 15 return Math.sqrt((this.x * this.x) + (this.y * this.y));
@@ -19,16 +19,16 @@ class vector
19 19 {
20 20 return Math.sqrt((this.x * this.x) + (this.y * this.y));
21 21 }
22
23
22
23
24 24 toAngle()
25 25 {
26 26 let angle = (Math.atan2(this.y, this.x) * 180 / Math.PI) + 90;
27 if(angle < 0)
27 if (angle < 0)
28 28 angle += 360;
29 29 return angle;
30 30 }
31
31
32 32 static fromAngle(angle)
33 33 {
34 34 angle = 360 - ((angle + 180) % 360);
@@ -39,86 +39,85 @@ class vector
39 39 normalize()
40 40 {
41 41 var newVector = this.getCopy();
42
43 if(newVector.length != 0)
44 {
42
43 if (newVector.length != 0) {
45 44 var length = newVector.length;
46 45 newVector.x /= length;
47 46 newVector.y /= length;
48 47 }
49
48
50 49 return newVector;
51 50 }
52
53
51
52
54 53 lerpTo(otherVector, t)
55 54 {
56 55 var delta = otherVector.subtract(this);
57
56
58 57 return this.add(delta.stretch(t));
59 58 }
60
61
59
60
62 61 distanceTo(otherVector)
63 62 {
64 63 var dX = this.x - otherVector.x;
65 64 var dY = this.y - otherVector.y;
66
65
67 66 return new vector(dX, dY).length;
68 67 }
69
70
68
69
71 70 add(otherVector)
72 71 {
73 72 var newVector = this.getCopy();
74
73
75 74 newVector.x += otherVector.x;
76 75 newVector.y += otherVector.y;
77
76
78 77 return newVector;
79 78 }
80
81
79
80
82 81 subtract(otherVector)
83 82 {
84 83 var newVector = this.getCopy();
85
84
86 85 newVector.x -= otherVector.x;
87 86 newVector.y -= otherVector.y;
88
87
89 88 return newVector;
90 89 }
91
92
90
91
93 92 multiply(otherVector)
94 93 {
95 94 var newVector = this.getCopy();
96
95
97 96 newVector.x *= otherVector.x;
98 97 newVector.y *= otherVector.y;
99
98
100 99 return newVector;
101 100 }
102
103
101
102
104 103 divide(otherVector)
105 104 {
106 105 var newVector = this.getCopy();
107
106
108 107 newVector.x /= otherVector.x;
109 108 newVector.y /= otherVector.y;
110
109
111 110 return newVector;
112 111 }
113
114
112
113
115 114 stretch(length)
116 115 {
117 116 var newVector = this.getCopy();
118
117
119 118 newVector.x *= length;
120 119 newVector.y *= length;
121
120
122 121 return newVector;
123 122 }
124 123
@@ -126,7 +125,7 @@ class vector
126 125 {
127 126 var ca = Math.cos(degrees * (Math.PI / 180));
128 127 var sa = Math.sin(degrees * (Math.PI / 180));
129 return new vector(ca*this.x - sa*this.y, sa*this.x + ca*this.y);
128 return new vector(ca * this.x - sa * this.y, sa * this.x + ca * this.y);
130 129 }
131 130
132 131 deltaAngle(otherVector)
@@ -141,8 +140,8 @@ class vector
141 140 {
142 141 return this.x * otherVector.y - this.y * otherVector.x;
143 142 }
144
145
143
144
146 145 getCopy()
147 146 {
148 147 return new vector(this.x, this.y);
@@ -6,10 +6,10 @@ class vector
6 constructor(x, y)
6 constructor(x, y)
7 {
7 {
8 this.x = x;
8 this.x = x;
9
9
10 this.y = y;
10 this.y = y;
11 }
11 }
12
12
13 get length()
13 get length()
14 {
14 {
15 return Math.sqrt((this.x * this.x) + (this.y * this.y));
15 return Math.sqrt((this.x * this.x) + (this.y * this.y));
@@ -19,16 +19,16 @@ class vector
19 {
19 {
20 return Math.sqrt((this.x * this.x) + (this.y * this.y));
20 return Math.sqrt((this.x * this.x) + (this.y * this.y));
21 }
21 }
22
22
23
23
24 toAngle()
24 toAngle()
25 {
25 {
26 let angle = (Math.atan2(this.y, this.x) * 180 / Math.PI) + 90;
26 let angle = (Math.atan2(this.y, this.x) * 180 / Math.PI) + 90;
27 if(angle < 0)
27 if (angle < 0)
28 angle += 360;
28 angle += 360;
29 return angle;
29 return angle;
30 }
30 }
31
31
32 static fromAngle(angle)
32 static fromAngle(angle)
33 {
33 {
34 angle = 360 - ((angle + 180) % 360);
34 angle = 360 - ((angle + 180) % 360);
@@ -39,86 +39,85 @@ class vector
39 normalize()
39 normalize()
40 {
40 {
41 var newVector = this.getCopy();
41 var newVector = this.getCopy();
42
42
43 if(newVector.length != 0)
43 if (newVector.length != 0) {
44 {
45 var length = newVector.length;
44 var length = newVector.length;
46 newVector.x /= length;
45 newVector.x /= length;
47 newVector.y /= length;
46 newVector.y /= length;
48 }
47 }
49
48
50 return newVector;
49 return newVector;
51 }
50 }
52
51
53
52
54 lerpTo(otherVector, t)
53 lerpTo(otherVector, t)
55 {
54 {
56 var delta = otherVector.subtract(this);
55 var delta = otherVector.subtract(this);
57
56
58 return this.add(delta.stretch(t));
57 return this.add(delta.stretch(t));
59 }
58 }
60
59
61
60
62 distanceTo(otherVector)
61 distanceTo(otherVector)
63 {
62 {
64 var dX = this.x - otherVector.x;
63 var dX = this.x - otherVector.x;
65 var dY = this.y - otherVector.y;
64 var dY = this.y - otherVector.y;
66
65
67 return new vector(dX, dY).length;
66 return new vector(dX, dY).length;
68 }
67 }
69
68
70
69
71 add(otherVector)
70 add(otherVector)
72 {
71 {
73 var newVector = this.getCopy();
72 var newVector = this.getCopy();
74
73
75 newVector.x += otherVector.x;
74 newVector.x += otherVector.x;
76 newVector.y += otherVector.y;
75 newVector.y += otherVector.y;
77
76
78 return newVector;
77 return newVector;
79 }
78 }
80
79
81
80
82 subtract(otherVector)
81 subtract(otherVector)
83 {
82 {
84 var newVector = this.getCopy();
83 var newVector = this.getCopy();
85
84
86 newVector.x -= otherVector.x;
85 newVector.x -= otherVector.x;
87 newVector.y -= otherVector.y;
86 newVector.y -= otherVector.y;
88
87
89 return newVector;
88 return newVector;
90 }
89 }
91
90
92
91
93 multiply(otherVector)
92 multiply(otherVector)
94 {
93 {
95 var newVector = this.getCopy();
94 var newVector = this.getCopy();
96
95
97 newVector.x *= otherVector.x;
96 newVector.x *= otherVector.x;
98 newVector.y *= otherVector.y;
97 newVector.y *= otherVector.y;
99
98
100 return newVector;
99 return newVector;
101 }
100 }
102
101
103
102
104 divide(otherVector)
103 divide(otherVector)
105 {
104 {
106 var newVector = this.getCopy();
105 var newVector = this.getCopy();
107
106
108 newVector.x /= otherVector.x;
107 newVector.x /= otherVector.x;
109 newVector.y /= otherVector.y;
108 newVector.y /= otherVector.y;
110
109
111 return newVector;
110 return newVector;
112 }
111 }
113
112
114
113
115 stretch(length)
114 stretch(length)
116 {
115 {
117 var newVector = this.getCopy();
116 var newVector = this.getCopy();
118
117
119 newVector.x *= length;
118 newVector.x *= length;
120 newVector.y *= length;
119 newVector.y *= length;
121
120
122 return newVector;
121 return newVector;
123 }
122 }
124
123
@@ -126,7 +125,7 @@ class vector
126 {
125 {
127 var ca = Math.cos(degrees * (Math.PI / 180));
126 var ca = Math.cos(degrees * (Math.PI / 180));
128 var sa = Math.sin(degrees * (Math.PI / 180));
127 var sa = Math.sin(degrees * (Math.PI / 180));
129 return new vector(ca*this.x - sa*this.y, sa*this.x + ca*this.y);
128 return new vector(ca * this.x - sa * this.y, sa * this.x + ca * this.y);
130 }
129 }
131
130
132 deltaAngle(otherVector)
131 deltaAngle(otherVector)
@@ -141,8 +140,8 @@ class vector
141 {
140 {
142 return this.x * otherVector.y - this.y * otherVector.x;
141 return this.x * otherVector.y - this.y * otherVector.x;
143 }
142 }
144
143
145
144
146 getCopy()
145 getCopy()
147 {
146 {
148 return new vector(this.x, this.y);
147 return new vector(this.x, this.y);