Skip to content

Commit df0b474

Browse files
committed
Added getTopCenter, getBottomCenter, getLeftCenter and getRightCenter
1 parent 0931853 commit df0b474

2 files changed

Lines changed: 129 additions & 47 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ The following changes took place in the Pointer class:
6363
* `Matter.Factory.velocity` is a new method that allows you to set the velocity on a Matter Body directly.
6464
* `Matter.Factory.angularVelocity` is a new method that allows you to set the angular velocity on a Matter Body directly.
6565
* `Matter.Factory.force` is a new method that allows you to apply a force from a world position on a Matter Body directly.
66+
* `GetBounds.getTopCenter` is a new method that will return the top-center point from the bounds of a Game Object.
67+
* `GetBounds.getBottomCenter` is a new method that will return the bottom-center point from the bounds of a Game Object.
68+
* `GetBounds.getLeftCenter` is a new method that will return the left-center point from the bounds of a Game Object.
69+
* `GetBounds.getRightCenter` is a new method that will return the right-center point from the bounds of a Game Object.
6670

6771
### Updates
6872

@@ -74,6 +78,7 @@ The following changes took place in the Pointer class:
7478
* The `TimeStep.step` method no longer uses the time value passed to the raf callback, as it's not actually the current point in time, but rather the time that the main thread began at. Which doesn't help if we're comparing it to event timestamps.
7579
* `TimeStep.now` is a new property that holds the exact `performance.now` value, as set at the start of the current game step.
7680
* `Matter.Factory.fromVertices` can now take a vertices path string as its `vertexSets` argument, as well as an array of vertices.
81+
* `GetBounds.prepareBoundsOutput` is a new private method that handles processing the output point. All of the bounds methods now use this, allowing us to remove a lot of duplicated code.
7782

7883
### Bug Fixes
7984

src/gameobjects/components/GetBounds.js

Lines changed: 124 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,39 @@ var Vector2 = require('../../math/Vector2');
1818

1919
var GetBounds = {
2020

21+
/**
22+
* Processes the bounds output vector before returning it.
23+
*
24+
* @method Phaser.GameObjects.Components.GetBounds#prepareBoundsOutput
25+
* @private
26+
* @since 3.18.0
27+
*
28+
* @generic {Phaser.Math.Vector2} O - [output,$return]
29+
*
30+
* @param {(Phaser.Math.Vector2|object)} output - An object to store the values in. If not provided a new Vector2 will be created.
31+
* @param {boolean} [includeParent=false] - If this Game Object has a parent Container, include it (and all other ancestors) in the resulting vector?
32+
*
33+
* @return {(Phaser.Math.Vector2|object)} The values stored in the output object.
34+
*/
35+
prepareBoundsOutput: function (output, includeParent)
36+
{
37+
if (includeParent === undefined) { includeParent = false; }
38+
39+
if (this.rotation !== 0)
40+
{
41+
RotateAround(output, this.x, this.y, this.rotation);
42+
}
43+
44+
if (includeParent && this.parentContainer)
45+
{
46+
var parentMatrix = this.parentContainer.getBoundsTransformMatrix();
47+
48+
parentMatrix.transformPoint(output.x, output.y, output);
49+
}
50+
51+
return output;
52+
},
53+
2154
/**
2255
* Gets the center coordinate of this Game Object, regardless of origin.
2356
* The returned point is calculated in local space and does not factor in any parent containers
@@ -58,24 +91,35 @@ var GetBounds = {
5891
getTopLeft: function (output, includeParent)
5992
{
6093
if (!output) { output = new Vector2(); }
61-
if (includeParent === undefined) { includeParent = false; }
6294

6395
output.x = this.x - (this.displayWidth * this.originX);
6496
output.y = this.y - (this.displayHeight * this.originY);
6597

66-
if (this.rotation !== 0)
67-
{
68-
RotateAround(output, this.x, this.y, this.rotation);
69-
}
98+
return this.prepareBoundsOutput(output, includeParent);
99+
},
70100

71-
if (includeParent && this.parentContainer)
72-
{
73-
var parentMatrix = this.parentContainer.getBoundsTransformMatrix();
101+
/**
102+
* Gets the top-center coordinate of this Game Object, regardless of origin.
103+
* The returned point is calculated in local space and does not factor in any parent containers
104+
*
105+
* @method Phaser.GameObjects.Components.GetBounds#getTopCenter
106+
* @since 3.18.0
107+
*
108+
* @generic {Phaser.Math.Vector2} O - [output,$return]
109+
*
110+
* @param {(Phaser.Math.Vector2|object)} [output] - An object to store the values in. If not provided a new Vector2 will be created.
111+
* @param {boolean} [includeParent=false] - If this Game Object has a parent Container, include it (and all other ancestors) in the resulting vector?
112+
*
113+
* @return {(Phaser.Math.Vector2|object)} The values stored in the output object.
114+
*/
115+
getTopCenter: function (output, includeParent)
116+
{
117+
if (!output) { output = new Vector2(); }
74118

75-
parentMatrix.transformPoint(output.x, output.y, output);
76-
}
119+
output.x = (this.x - (this.displayWidth * this.originX)) + (this.displayWidth / 2);
120+
output.y = this.y - (this.displayHeight * this.originY);
77121

78-
return output;
122+
return this.prepareBoundsOutput(output, includeParent);
79123
},
80124

81125
/**
@@ -95,24 +139,59 @@ var GetBounds = {
95139
getTopRight: function (output, includeParent)
96140
{
97141
if (!output) { output = new Vector2(); }
98-
if (includeParent === undefined) { includeParent = false; }
99142

100143
output.x = (this.x - (this.displayWidth * this.originX)) + this.displayWidth;
101144
output.y = this.y - (this.displayHeight * this.originY);
102145

103-
if (this.rotation !== 0)
104-
{
105-
RotateAround(output, this.x, this.y, this.rotation);
106-
}
146+
return this.prepareBoundsOutput(output, includeParent);
147+
},
107148

108-
if (includeParent && this.parentContainer)
109-
{
110-
var parentMatrix = this.parentContainer.getBoundsTransformMatrix();
149+
/**
150+
* Gets the left-center coordinate of this Game Object, regardless of origin.
151+
* The returned point is calculated in local space and does not factor in any parent containers
152+
*
153+
* @method Phaser.GameObjects.Components.GetBounds#getLeftCenter
154+
* @since 3.18.0
155+
*
156+
* @generic {Phaser.Math.Vector2} O - [output,$return]
157+
*
158+
* @param {(Phaser.Math.Vector2|object)} [output] - An object to store the values in. If not provided a new Vector2 will be created.
159+
* @param {boolean} [includeParent=false] - If this Game Object has a parent Container, include it (and all other ancestors) in the resulting vector?
160+
*
161+
* @return {(Phaser.Math.Vector2|object)} The values stored in the output object.
162+
*/
163+
getLeftCenter: function (output, includeParent)
164+
{
165+
if (!output) { output = new Vector2(); }
111166

112-
parentMatrix.transformPoint(output.x, output.y, output);
113-
}
167+
output.x = this.x - (this.displayWidth * this.originX);
168+
output.y = (this.y - (this.displayHeight * this.originY)) + (this.displayHeight / 2);
114169

115-
return output;
170+
return this.prepareBoundsOutput(output, includeParent);
171+
},
172+
173+
/**
174+
* Gets the right-center coordinate of this Game Object, regardless of origin.
175+
* The returned point is calculated in local space and does not factor in any parent containers
176+
*
177+
* @method Phaser.GameObjects.Components.GetBounds#getRightCenter
178+
* @since 3.18.0
179+
*
180+
* @generic {Phaser.Math.Vector2} O - [output,$return]
181+
*
182+
* @param {(Phaser.Math.Vector2|object)} [output] - An object to store the values in. If not provided a new Vector2 will be created.
183+
* @param {boolean} [includeParent=false] - If this Game Object has a parent Container, include it (and all other ancestors) in the resulting vector?
184+
*
185+
* @return {(Phaser.Math.Vector2|object)} The values stored in the output object.
186+
*/
187+
getRightCenter: function (output, includeParent)
188+
{
189+
if (!output) { output = new Vector2(); }
190+
191+
output.x = (this.x - (this.displayWidth * this.originX)) + this.displayWidth;
192+
output.y = (this.y - (this.displayHeight * this.originY)) + (this.displayHeight / 2);
193+
194+
return this.prepareBoundsOutput(output, includeParent);
116195
},
117196

118197
/**
@@ -132,24 +211,35 @@ var GetBounds = {
132211
getBottomLeft: function (output, includeParent)
133212
{
134213
if (!output) { output = new Vector2(); }
135-
if (includeParent === undefined) { includeParent = false; }
136214

137215
output.x = this.x - (this.displayWidth * this.originX);
138216
output.y = (this.y - (this.displayHeight * this.originY)) + this.displayHeight;
139217

140-
if (this.rotation !== 0)
141-
{
142-
RotateAround(output, this.x, this.y, this.rotation);
143-
}
218+
return this.prepareBoundsOutput(output, includeParent);
219+
},
144220

145-
if (includeParent && this.parentContainer)
146-
{
147-
var parentMatrix = this.parentContainer.getBoundsTransformMatrix();
221+
/**
222+
* Gets the bottom-center coordinate of this Game Object, regardless of origin.
223+
* The returned point is calculated in local space and does not factor in any parent containers
224+
*
225+
* @method Phaser.GameObjects.Components.GetBounds#getBottomCenter
226+
* @since 3.18.0
227+
*
228+
* @generic {Phaser.Math.Vector2} O - [output,$return]
229+
*
230+
* @param {(Phaser.Math.Vector2|object)} [output] - An object to store the values in. If not provided a new Vector2 will be created.
231+
* @param {boolean} [includeParent=false] - If this Game Object has a parent Container, include it (and all other ancestors) in the resulting vector?
232+
*
233+
* @return {(Phaser.Math.Vector2|object)} The values stored in the output object.
234+
*/
235+
getBottomCenter: function (output, includeParent)
236+
{
237+
if (!output) { output = new Vector2(); }
148238

149-
parentMatrix.transformPoint(output.x, output.y, output);
150-
}
239+
output.x = (this.x - (this.displayWidth * this.originX)) + (this.displayWidth / 2);
240+
output.y = (this.y - (this.displayHeight * this.originY)) + this.displayHeight;
151241

152-
return output;
242+
return this.prepareBoundsOutput(output, includeParent);
153243
},
154244

155245
/**
@@ -169,24 +259,11 @@ var GetBounds = {
169259
getBottomRight: function (output, includeParent)
170260
{
171261
if (!output) { output = new Vector2(); }
172-
if (includeParent === undefined) { includeParent = false; }
173262

174263
output.x = (this.x - (this.displayWidth * this.originX)) + this.displayWidth;
175264
output.y = (this.y - (this.displayHeight * this.originY)) + this.displayHeight;
176265

177-
if (this.rotation !== 0)
178-
{
179-
RotateAround(output, this.x, this.y, this.rotation);
180-
}
181-
182-
if (includeParent && this.parentContainer)
183-
{
184-
var parentMatrix = this.parentContainer.getBoundsTransformMatrix();
185-
186-
parentMatrix.transformPoint(output.x, output.y, output);
187-
}
188-
189-
return output;
266+
return this.prepareBoundsOutput(output, includeParent);
190267
},
191268

192269
/**

0 commit comments

Comments
 (0)