-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.js
189 lines (183 loc) · 5.03 KB
/
types.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*global a2d */
/**
* Vector
* @class
* @param {number} x X
* @param {number} y Y
* */
a2d.Vector = function (x, y) {
'use strict';
/** the X value */
this.X = x;
/** the Y value */
this.Y = y;
/**
* Returns a Vector object with the same values.
* @returns {a2d.Vector} a clone of this Vector object
*/
this.clone = function() {
return new a2d.Vector(this.X, this.Y);
};
};
/**
* Position
* @class
* @augments a2d.Vector
* @param {number} x Horizontal coordinate
* @param {number} y Vertical coordinate
* */
a2d.Position = function (x, y) {
'use strict';
a2d.Vector.apply(this, [x, y]);
/**
* Scales coordinates.
* @param {a2d.Position} _scale X and Y scale
* @public
*/
this.scale = function (scale) {
if(scale.Width) {
this.X = Math.floor(this.X * scale.Width);
this.Y = Math.floor(this.Y * scale.Height);
} else {
this.X = Math.floor(this.X * scale.X);
this.Y = Math.floor(this.Y * scale.Y);
}
};
/**
* Adds coordinates.
* @param {a2d.Position} diff Other position
* @public
*/
this.add = function (diff) {
this.X += diff.X;
this.Y += diff.Y;
};
/**
* Subtracts coordinates.
* @param {a2d.Position} diff other position
* @deprecated In favor of a2d.Position#subtract
* @see a2d.Position#subtract
*/
this.substract = function (diff) {
this.subtract(diff);
};
/**
* Subtracts other position from this one
* @param {a2d.Position} diff other position
*/
this.subtract = function (diff) {
this.X -= diff.X;
this.Y -= diff.Y;
};
/**
* Divides this position by another
* @param {a2d.Position} diff other position
*/
this.divide = function (diff) {
this.X = Math.floor(this.X / diff.X);
this.Y = Math.floor(this.Y / diff.Y);
};
/**
* Checks if this position is inside a given rectangle.
* @param {a2d.Rectangle} rectangle Rectangle to check against.
*/
this.isInside = function (rectangle) {
if (this.X > rectangle.topLeft.X && this.X < rectangle.bottomRight.X &&
this.Y > rectangle.topLeft.Y && this.Y < rectangle.bottomRight.Y) {
return true;
}
return false;
};
/**
* Checks if two positions are equal
* @param {a2d.Position} position2 The other position
* @returns {boolean} true if the positions are the same
*/
this.is = function (position2) {
return (this.X === position2.X && this.Y === position2.Y);
};
/**
* Checks if two positions are not equal
* @param {a2d.Position} position2 The other position
* @returns {boolean} true if the positions are not the same
*/
this.not = function (position2) {
return (this.X !== position2.X || this.Y !== position2.Y);
};
/**
* Get the distance between two points
* @param {a2d.Position} position2 The other position
* @returns {number} The distance between the two points
*/
this.distanceTo = function (position2) {
var xdiff = Math.abs(this.X - position2.X),
ydiff = Math.abs(this.Y - position2.Y);
return Math.sqrt(Math.pow(xdiff, 2) + Math.pow(ydiff, 2));
};
/**
* Returns a Position object with the same coordinates
* @returns {a2d.Position} a clone of this Position object
*/
this.clone = function() {
return new a2d.Position(this.X, this.Y);
};
};
/**
* Rectangle
* @class
* @param {a2d.Position} topLeft top left coordinates
* @param {a2d.Position} bottomRight bottom right coordinates
* */
a2d.Rectangle = function (topLeft, bottomRight) {
'use strict';
/** @type a2d.Position */
this.topLeft = topLeft;
/** @type a2d.Position */
this.bottomRight = bottomRight;
/** @type a2d.Position */
this.topRight = new a2d.Position(bottomRight.X, topLeft.Y);
/** @type a2d.Position */
this.bottomLeft = new a2d.Position(topLeft.X, bottomRight.Y);
/**
* Moves a rectangle by a position
* @param {a2d.Position} pos The position to move by
*/
this.add = function (pos) {
this.topLeft.add(pos);
this.topRight.add(pos);
this.bottomLeft.add(pos);
this.bottomRight.add(pos);
};
/**
* Checks if this rectangle overlaps another.
* @param {a2d.Rectangle} other Another rectangle to check against
* @returns {boolean} true if the rectangles overlap.
*/
this.overlaps = function (other) {
return (other.topLeft.isInside(this) ||
other.bottomRight.isInside(this) ||
other.topRight.isInside(this) ||
other.bottomLeft.isInside(this));
};
};
/**
* Dimension
* @class
* @param {number} width Width
* @param {number} height Height
* */
a2d.Dimension = function (width, height) {
'use strict';
/** @type number */
this.Width = width;
/** @type number */
this.Height = height;
/**
* Effectively multiplies this Dimension by a position or vector
* @param {a2d.Position|a2d.Vector} scale The scale to apply
*/
this.scale = function (scale) {
this.Width = Math.floor(this.Width * scale.X);
this.Height = Math.floor(this.Height * scale.Y);
};
};