forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetBounds.js
More file actions
353 lines (295 loc) · 13.5 KB
/
Copy pathGetBounds.js
File metadata and controls
353 lines (295 loc) · 13.5 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2019 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Rectangle = require('../../geom/rectangle/Rectangle');
var RotateAround = require('../../math/RotateAround');
var Vector2 = require('../../math/Vector2');
/**
* Provides methods used for obtaining the bounds of a Game Object.
* Should be applied as a mixin and not used directly.
*
* @namespace Phaser.GameObjects.Components.GetBounds
* @since 3.0.0
*/
var GetBounds = {
/**
* Processes the bounds output vector before returning it.
*
* @method Phaser.GameObjects.Components.GetBounds#prepareBoundsOutput
* @private
* @since 3.18.0
*
* @generic {Phaser.Math.Vector2} O - [output,$return]
*
* @param {(Phaser.Math.Vector2|object)} output - An object to store the values in. If not provided a new Vector2 will be created.
* @param {boolean} [includeParent=false] - If this Game Object has a parent Container, include it (and all other ancestors) in the resulting vector?
*
* @return {(Phaser.Math.Vector2|object)} The values stored in the output object.
*/
prepareBoundsOutput: function (output, includeParent)
{
if (includeParent === undefined) { includeParent = false; }
if (this.rotation !== 0)
{
RotateAround(output, this.x, this.y, this.rotation);
}
if (includeParent && this.parentContainer)
{
var parentMatrix = this.parentContainer.getBoundsTransformMatrix();
parentMatrix.transformPoint(output.x, output.y, output);
}
return output;
},
/**
* Gets the center coordinate of this Game Object, regardless of origin.
* The returned point is calculated in local space and does not factor in any parent containers
*
* @method Phaser.GameObjects.Components.GetBounds#getCenter
* @since 3.0.0
*
* @generic {Phaser.Math.Vector2} O - [output,$return]
*
* @param {(Phaser.Math.Vector2|object)} [output] - An object to store the values in. If not provided a new Vector2 will be created.
*
* @return {(Phaser.Math.Vector2|object)} The values stored in the output object.
*/
getCenter: function (output)
{
if (output === undefined) { output = new Vector2(); }
output.x = this.x - (this.displayWidth * this.originX) + (this.displayWidth / 2);
output.y = this.y - (this.displayHeight * this.originY) + (this.displayHeight / 2);
return output;
},
/**
* Gets the top-left corner coordinate of this Game Object, regardless of origin.
* The returned point is calculated in local space and does not factor in any parent containers
*
* @method Phaser.GameObjects.Components.GetBounds#getTopLeft
* @since 3.0.0
*
* @generic {Phaser.Math.Vector2} O - [output,$return]
*
* @param {(Phaser.Math.Vector2|object)} [output] - An object to store the values in. If not provided a new Vector2 will be created.
* @param {boolean} [includeParent=false] - If this Game Object has a parent Container, include it (and all other ancestors) in the resulting vector?
*
* @return {(Phaser.Math.Vector2|object)} The values stored in the output object.
*/
getTopLeft: function (output, includeParent)
{
if (!output) { output = new Vector2(); }
output.x = this.x - (this.displayWidth * this.originX);
output.y = this.y - (this.displayHeight * this.originY);
return this.prepareBoundsOutput(output, includeParent);
},
/**
* Gets the top-center coordinate of this Game Object, regardless of origin.
* The returned point is calculated in local space and does not factor in any parent containers
*
* @method Phaser.GameObjects.Components.GetBounds#getTopCenter
* @since 3.18.0
*
* @generic {Phaser.Math.Vector2} O - [output,$return]
*
* @param {(Phaser.Math.Vector2|object)} [output] - An object to store the values in. If not provided a new Vector2 will be created.
* @param {boolean} [includeParent=false] - If this Game Object has a parent Container, include it (and all other ancestors) in the resulting vector?
*
* @return {(Phaser.Math.Vector2|object)} The values stored in the output object.
*/
getTopCenter: function (output, includeParent)
{
if (!output) { output = new Vector2(); }
output.x = (this.x - (this.displayWidth * this.originX)) + (this.displayWidth / 2);
output.y = this.y - (this.displayHeight * this.originY);
return this.prepareBoundsOutput(output, includeParent);
},
/**
* Gets the top-right corner coordinate of this Game Object, regardless of origin.
* The returned point is calculated in local space and does not factor in any parent containers
*
* @method Phaser.GameObjects.Components.GetBounds#getTopRight
* @since 3.0.0
*
* @generic {Phaser.Math.Vector2} O - [output,$return]
*
* @param {(Phaser.Math.Vector2|object)} [output] - An object to store the values in. If not provided a new Vector2 will be created.
* @param {boolean} [includeParent=false] - If this Game Object has a parent Container, include it (and all other ancestors) in the resulting vector?
*
* @return {(Phaser.Math.Vector2|object)} The values stored in the output object.
*/
getTopRight: function (output, includeParent)
{
if (!output) { output = new Vector2(); }
output.x = (this.x - (this.displayWidth * this.originX)) + this.displayWidth;
output.y = this.y - (this.displayHeight * this.originY);
return this.prepareBoundsOutput(output, includeParent);
},
/**
* Gets the left-center coordinate of this Game Object, regardless of origin.
* The returned point is calculated in local space and does not factor in any parent containers
*
* @method Phaser.GameObjects.Components.GetBounds#getLeftCenter
* @since 3.18.0
*
* @generic {Phaser.Math.Vector2} O - [output,$return]
*
* @param {(Phaser.Math.Vector2|object)} [output] - An object to store the values in. If not provided a new Vector2 will be created.
* @param {boolean} [includeParent=false] - If this Game Object has a parent Container, include it (and all other ancestors) in the resulting vector?
*
* @return {(Phaser.Math.Vector2|object)} The values stored in the output object.
*/
getLeftCenter: function (output, includeParent)
{
if (!output) { output = new Vector2(); }
output.x = this.x - (this.displayWidth * this.originX);
output.y = (this.y - (this.displayHeight * this.originY)) + (this.displayHeight / 2);
return this.prepareBoundsOutput(output, includeParent);
},
/**
* Gets the right-center coordinate of this Game Object, regardless of origin.
* The returned point is calculated in local space and does not factor in any parent containers
*
* @method Phaser.GameObjects.Components.GetBounds#getRightCenter
* @since 3.18.0
*
* @generic {Phaser.Math.Vector2} O - [output,$return]
*
* @param {(Phaser.Math.Vector2|object)} [output] - An object to store the values in. If not provided a new Vector2 will be created.
* @param {boolean} [includeParent=false] - If this Game Object has a parent Container, include it (and all other ancestors) in the resulting vector?
*
* @return {(Phaser.Math.Vector2|object)} The values stored in the output object.
*/
getRightCenter: function (output, includeParent)
{
if (!output) { output = new Vector2(); }
output.x = (this.x - (this.displayWidth * this.originX)) + this.displayWidth;
output.y = (this.y - (this.displayHeight * this.originY)) + (this.displayHeight / 2);
return this.prepareBoundsOutput(output, includeParent);
},
/**
* Gets the bottom-left corner coordinate of this Game Object, regardless of origin.
* The returned point is calculated in local space and does not factor in any parent containers
*
* @method Phaser.GameObjects.Components.GetBounds#getBottomLeft
* @since 3.0.0
*
* @generic {Phaser.Math.Vector2} O - [output,$return]
*
* @param {(Phaser.Math.Vector2|object)} [output] - An object to store the values in. If not provided a new Vector2 will be created.
* @param {boolean} [includeParent=false] - If this Game Object has a parent Container, include it (and all other ancestors) in the resulting vector?
*
* @return {(Phaser.Math.Vector2|object)} The values stored in the output object.
*/
getBottomLeft: function (output, includeParent)
{
if (!output) { output = new Vector2(); }
output.x = this.x - (this.displayWidth * this.originX);
output.y = (this.y - (this.displayHeight * this.originY)) + this.displayHeight;
return this.prepareBoundsOutput(output, includeParent);
},
/**
* Gets the bottom-center coordinate of this Game Object, regardless of origin.
* The returned point is calculated in local space and does not factor in any parent containers
*
* @method Phaser.GameObjects.Components.GetBounds#getBottomCenter
* @since 3.18.0
*
* @generic {Phaser.Math.Vector2} O - [output,$return]
*
* @param {(Phaser.Math.Vector2|object)} [output] - An object to store the values in. If not provided a new Vector2 will be created.
* @param {boolean} [includeParent=false] - If this Game Object has a parent Container, include it (and all other ancestors) in the resulting vector?
*
* @return {(Phaser.Math.Vector2|object)} The values stored in the output object.
*/
getBottomCenter: function (output, includeParent)
{
if (!output) { output = new Vector2(); }
output.x = (this.x - (this.displayWidth * this.originX)) + (this.displayWidth / 2);
output.y = (this.y - (this.displayHeight * this.originY)) + this.displayHeight;
return this.prepareBoundsOutput(output, includeParent);
},
/**
* Gets the bottom-right corner coordinate of this Game Object, regardless of origin.
* The returned point is calculated in local space and does not factor in any parent containers
*
* @method Phaser.GameObjects.Components.GetBounds#getBottomRight
* @since 3.0.0
*
* @generic {Phaser.Math.Vector2} O - [output,$return]
*
* @param {(Phaser.Math.Vector2|object)} [output] - An object to store the values in. If not provided a new Vector2 will be created.
* @param {boolean} [includeParent=false] - If this Game Object has a parent Container, include it (and all other ancestors) in the resulting vector?
*
* @return {(Phaser.Math.Vector2|object)} The values stored in the output object.
*/
getBottomRight: function (output, includeParent)
{
if (!output) { output = new Vector2(); }
output.x = (this.x - (this.displayWidth * this.originX)) + this.displayWidth;
output.y = (this.y - (this.displayHeight * this.originY)) + this.displayHeight;
return this.prepareBoundsOutput(output, includeParent);
},
/**
* Gets the bounds of this Game Object, regardless of origin.
* The values are stored and returned in a Rectangle, or Rectangle-like, object.
*
* @method Phaser.GameObjects.Components.GetBounds#getBounds
* @since 3.0.0
*
* @generic {Phaser.Geom.Rectangle} O - [output,$return]
*
* @param {(Phaser.Geom.Rectangle|object)} [output] - An object to store the values in. If not provided a new Rectangle will be created.
*
* @return {(Phaser.Geom.Rectangle|object)} The values stored in the output object.
*/
getBounds: function (output)
{
if (output === undefined) { output = new Rectangle(); }
// We can use the output object to temporarily store the x/y coords in:
var TLx, TLy, TRx, TRy, BLx, BLy, BRx, BRy;
// Instead of doing a check if parent container is
// defined per corner we only do it once.
if (this.parentContainer)
{
var parentMatrix = this.parentContainer.getBoundsTransformMatrix();
this.getTopLeft(output);
parentMatrix.transformPoint(output.x, output.y, output);
TLx = output.x;
TLy = output.y;
this.getTopRight(output);
parentMatrix.transformPoint(output.x, output.y, output);
TRx = output.x;
TRy = output.y;
this.getBottomLeft(output);
parentMatrix.transformPoint(output.x, output.y, output);
BLx = output.x;
BLy = output.y;
this.getBottomRight(output);
parentMatrix.transformPoint(output.x, output.y, output);
BRx = output.x;
BRy = output.y;
}
else
{
this.getTopLeft(output);
TLx = output.x;
TLy = output.y;
this.getTopRight(output);
TRx = output.x;
TRy = output.y;
this.getBottomLeft(output);
BLx = output.x;
BLy = output.y;
this.getBottomRight(output);
BRx = output.x;
BRy = output.y;
}
output.x = Math.min(TLx, TRx, BLx, BRx);
output.y = Math.min(TLy, TRy, BLy, BRy);
output.width = Math.max(TLx, TRx, BLx, BRx) - output.x;
output.height = Math.max(TLy, TRy, BLy, BRy) - output.y;
return output;
}
};
module.exports = GetBounds;