Skip to content

Commit d6fb8d7

Browse files
committed
Added setBounds helper method
1 parent d05bd40 commit d6fb8d7

1 file changed

Lines changed: 158 additions & 0 deletions

File tree

v3/src/physics/impact/World.js

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ var World = new Class({
5353

5454
this.delta = 0;
5555

56+
/**
57+
* @property {object} walls - An object containing the 4 wall bodies that bound the physics world.
58+
*/
59+
this.walls = { left: null, right: null, top: null, bottom: null };
60+
5661
this._lastId = 0;
5762

5863
if (this.drawDebug)
@@ -61,6 +66,159 @@ var World = new Class({
6166
}
6267
},
6368

69+
/**
70+
* Sets the bounds of the Physics world to match the given world pixel dimensions.
71+
* You can optionally set which 'walls' to create: left, right, top or bottom.
72+
* If none of the walls are given it will default to use the walls settings it had previously.
73+
* I.e. if you previously told it to not have the left or right walls, and you then adjust the world size
74+
* the newly created bounds will also not have the left and right walls.
75+
* Explicitly state them in the parameters to override this.
76+
*
77+
* @method Phaser.Physics.P2#setBounds
78+
* @param {number} x - The x coordinate of the top-left corner of the bounds.
79+
* @param {number} y - The y coordinate of the top-left corner of the bounds.
80+
* @param {number} width - The width of the bounds.
81+
* @param {number} height - The height of the bounds.
82+
* @param {boolean} [left=true] - If true will create the left bounds wall.
83+
* @param {boolean} [right=true] - If true will create the right bounds wall.
84+
* @param {boolean} [top=true] - If true will create the top bounds wall.
85+
* @param {boolean} [bottom=true] - If true will create the bottom bounds wall.
86+
*/
87+
setBounds: function (x, y, width, height, thickness, left, right, top, bottom)
88+
{
89+
if (x === undefined) { x = 0; }
90+
if (y === undefined) { y = 0; }
91+
if (width === undefined) { width = this.scene.sys.game.config.width; }
92+
if (height === undefined) { height = this.scene.sys.game.config.height; }
93+
if (thickness === undefined) { thickness = 64; }
94+
if (left === undefined) { left = true; }
95+
if (right === undefined) { right = true; }
96+
if (top === undefined) { top = true; }
97+
if (bottom === undefined) { bottom = true; }
98+
99+
this.updateWall(left, 'left', x - thickness, y, thickness, height);
100+
this.updateWall(right, 'right', x + width, y, thickness, height);
101+
this.updateWall(top, 'top', x, y - thickness, width, thickness);
102+
this.updateWall(bottom, 'bottom', x, y + height, width, thickness);
103+
104+
// if (left)
105+
// {
106+
// if (leftWall)
107+
// {
108+
// leftWall.setSize(x - thickness, y, thickness, height);
109+
// }
110+
// else
111+
// {
112+
// this.walls.left = this.create(x - thickness, y, thickness, height);
113+
// this.walls.left.gravityFactor = 0;
114+
// this.walls.left.collides = COLLIDES.FIXED;
115+
// }
116+
// }
117+
// else
118+
// {
119+
// if (leftWall)
120+
// {
121+
// this.bodies.remove(leftWall);
122+
// }
123+
124+
// this.walls.left = null;
125+
// }
126+
127+
// if (right)
128+
// {
129+
// if (rightWall)
130+
// {
131+
// rightWall.setSize(x + width, y, thickness, height);
132+
// }
133+
// else
134+
// {
135+
// this.walls.right = this.create(x + width, y, thickness, height);
136+
// }
137+
// }
138+
// else
139+
// {
140+
// if (rightWall)
141+
// {
142+
// this.bodies.remove(rightWall);
143+
// }
144+
145+
// this.walls.right = null;
146+
// }
147+
148+
// if (top)
149+
// {
150+
// if (topWall)
151+
// {
152+
// topWall.setSize(x, y - thickness, width, thickness);
153+
// }
154+
// else
155+
// {
156+
// this.walls.top = this.create(x, y - thickness, width, thickness);
157+
// }
158+
// }
159+
// else
160+
// {
161+
// if (topWall)
162+
// {
163+
// this.bodies.remove(topWall);
164+
// }
165+
166+
// this.walls.top = null;
167+
// }
168+
169+
// if (bottom)
170+
// {
171+
// if (bottomWall)
172+
// {
173+
// bottomWall.setSize(x, y + height, width, thickness);
174+
// }
175+
// else
176+
// {
177+
// this.walls.bottom = this.create(x, y + height, width, thickness);
178+
// }
179+
// }
180+
// else
181+
// {
182+
// if (bottomWall)
183+
// {
184+
// this.bodies.remove(bottomWall);
185+
// }
186+
187+
// this.walls.bottom = null;
188+
// }
189+
190+
return this;
191+
},
192+
193+
// position = 'left', 'right', 'top' or 'bottom'
194+
updateWall: function (add, position, x, y, width, height)
195+
{
196+
var wall = this.walls[position];
197+
198+
if (add)
199+
{
200+
if (wall)
201+
{
202+
wall.setSize(x, y, width, height);
203+
}
204+
else
205+
{
206+
this.walls[position] = this.create(x, y, width, height);
207+
this.walls[position].gravityFactor = 0;
208+
this.walls[position].collides = COLLIDES.FIXED;
209+
}
210+
}
211+
else
212+
{
213+
if (wall)
214+
{
215+
this.bodies.remove(wall);
216+
}
217+
218+
this.walls[position] = null;
219+
}
220+
},
221+
64222
createDebugGraphic: function ()
65223
{
66224
var graphic = this.scene.sys.add.graphics({ x: 0, y: 0 });

0 commit comments

Comments
 (0)