Skip to content

Commit 4db1fc0

Browse files
committed
Restored Phaser.QuadTree - should get all remaining Arcade Physics examples working again.
1 parent 480c181 commit 4db1fc0

8 files changed

Lines changed: 104 additions & 37 deletions

File tree

Gruntfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ module.exports = function (grunt) {
153153

154154
'src/math/Math.js',
155155
'src/math/RandomDataGenerator.js',
156+
'src/math/QuadTree.js',
156157

157158
'src/net/Net.js',
158159

@@ -184,7 +185,6 @@ module.exports = function (grunt) {
184185

185186
'src/physics/arcade/World.js',
186187
'src/physics/arcade/Body.js',
187-
'src/physics/arcade/QuadTree.js',
188188

189189
'src/physics/ninja/World.js',
190190
'src/physics/ninja/Body.js',

build/config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
109109
<script src="$path/src/math/Math.js"></script>
110110
<script src="$path/src/math/RandomDataGenerator.js"></script>
111+
<script src="$path/src/math/QuadTree.js"></script>
111112
112113
<script src="$path/src/net/Net.js"></script>
113114
@@ -139,7 +140,6 @@
139140
140141
<script src="$path/src/physics/arcade/World.js"></script>
141142
<script src="$path/src/physics/arcade/Body.js"></script>
142-
<script src="$path/src/physics/arcade/QuadTree.js"></script>
143143
144144
<script src="$path/src/physics/ninja/World.js"></script>
145145
<script src="$path/src/physics/ninja/Body.js"></script>

examples/_site/view_full.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114

115115
<script src="../src/math/Math.js"></script>
116116
<script src="../src/math/RandomDataGenerator.js"></script>
117+
<script src="../src/math/QuadTree.js"></script>
117118

118119
<script src="../src/net/Net.js"></script>
119120

examples/_site/view_lite.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114

115115
<script src="../src/math/Math.js"></script>
116116
<script src="../src/math/RandomDataGenerator.js"></script>
117+
<script src="../src/math/QuadTree.js"></script>
117118

118119
<script src="../src/net/Net.js"></script>
119120

@@ -145,7 +146,6 @@
145146

146147
<script src="../src/physics/arcade/World.js"></script>
147148
<script src="../src/physics/arcade/Body.js"></script>
148-
<script src="../src/physics/arcade/QuadTree.js"></script>
149149

150150
<script src="../src/physics/ninja/World.js"></script>
151151
<script src="../src/physics/ninja/Body.js"></script>

examples/wip/tween swap.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function create() {
1717
game.stage.backgroundColor = 0x3d4d3d;
1818

1919
mummy = game.add.sprite(0, 300, 'mummy', 5);
20-
mummy.scale.set(2);
20+
// mummy.scale.set(2);
2121

2222
anim = mummy.animations.add('walk');
2323

@@ -26,6 +26,7 @@ function create() {
2626
// game.onPause.add(paused, this);
2727
// game.onResume.add(resumed, this);
2828

29+
game.add.tween(mummy.scale).to({x:4,y:4}, 1000, Phaser.Easing.Linear.None, true);
2930
t = game.add.tween(mummy).to({x:700}, 15000, Phaser.Easing.Linear.None, true);
3031
t.onComplete.add(tweenOver, this);
3132

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
/**
4545
* QuadTree Constructor
4646
*
47-
* @class Phaser.Physics.Arcade.QuadTree
47+
* @class Phaser.QuadTree
4848
* @classdesc A QuadTree implementation. The original code was a conversion of the Java code posted to GameDevTuts. However I've tweaked
4949
* it massively to add node indexing, removed lots of temp. var creation and significantly increased performance as a result. Original version at https://github.com/timohausmann/quadtree-js/
5050
* @constructor
@@ -57,7 +57,7 @@
5757
* @param {number} maxLevels - Description.
5858
* @param {number} level - Description.
5959
*/
60-
Phaser.Physics.Arcade.QuadTree = function (physicsManager, x, y, width, height, maxObjects, maxLevels, level) {
60+
Phaser.QuadTree = function (physicsManager, x, y, width, height, maxObjects, maxLevels, level) {
6161

6262
this.physicsManager = physicsManager;
6363
this.ID = physicsManager.quadTreeID;
@@ -83,28 +83,28 @@ Phaser.Physics.Arcade.QuadTree = function (physicsManager, x, y, width, height,
8383

8484
};
8585

86-
Phaser.Physics.Arcade.QuadTree.prototype = {
86+
Phaser.QuadTree.prototype = {
8787

8888
/*
8989
* Split the node into 4 subnodes
9090
*
91-
* @method Phaser.Physics.Arcade.QuadTree#split
91+
* @method Phaser.QuadTree#split
9292
*/
9393
split: function() {
9494

9595
this.level++;
9696

9797
// top right node
98-
this.nodes[0] = new Phaser.Physics.Arcade.QuadTree(this.physicsManager, this.bounds.right, this.bounds.y, this.bounds.subWidth, this.bounds.subHeight, this.maxObjects, this.maxLevels, this.level);
98+
this.nodes[0] = new Phaser.QuadTree(this.physicsManager, this.bounds.right, this.bounds.y, this.bounds.subWidth, this.bounds.subHeight, this.maxObjects, this.maxLevels, this.level);
9999

100100
// top left node
101-
this.nodes[1] = new Phaser.Physics.Arcade.QuadTree(this.physicsManager, this.bounds.x, this.bounds.y, this.bounds.subWidth, this.bounds.subHeight, this.maxObjects, this.maxLevels, this.level);
101+
this.nodes[1] = new Phaser.QuadTree(this.physicsManager, this.bounds.x, this.bounds.y, this.bounds.subWidth, this.bounds.subHeight, this.maxObjects, this.maxLevels, this.level);
102102

103103
// bottom left node
104-
this.nodes[2] = new Phaser.Physics.Arcade.QuadTree(this.physicsManager, this.bounds.x, this.bounds.bottom, this.bounds.subWidth, this.bounds.subHeight, this.maxObjects, this.maxLevels, this.level);
104+
this.nodes[2] = new Phaser.QuadTree(this.physicsManager, this.bounds.x, this.bounds.bottom, this.bounds.subWidth, this.bounds.subHeight, this.maxObjects, this.maxLevels, this.level);
105105

106106
// bottom right node
107-
this.nodes[3] = new Phaser.Physics.Arcade.QuadTree(this.physicsManager, this.bounds.right, this.bounds.bottom, this.bounds.subWidth, this.bounds.subHeight, this.maxObjects, this.maxLevels, this.level);
107+
this.nodes[3] = new Phaser.QuadTree(this.physicsManager, this.bounds.right, this.bounds.bottom, this.bounds.subWidth, this.bounds.subHeight, this.maxObjects, this.maxLevels, this.level);
108108

109109
},
110110

@@ -113,7 +113,7 @@ Phaser.Physics.Arcade.QuadTree.prototype = {
113113
* exceeds the capacity, it will split and add all
114114
* objects to their corresponding subnodes.
115115
*
116-
* @method Phaser.Physics.Arcade.QuadTree#insert
116+
* @method Phaser.QuadTree#insert
117117
* @param {object} body - Description.
118118
*/
119119
insert: function (body) {
@@ -165,7 +165,7 @@ Phaser.Physics.Arcade.QuadTree.prototype = {
165165
/*
166166
* Determine which node the object belongs to.
167167
*
168-
* @method Phaser.Physics.Arcade.QuadTree#getIndex
168+
* @method Phaser.QuadTree#getIndex
169169
* @param {object} rect - Description.
170170
* @return {number} index - Index of the subnode (0-3), or -1 if rect cannot completely fit within a subnode and is part of the parent node.
171171
*/
@@ -209,7 +209,7 @@ Phaser.Physics.Arcade.QuadTree.prototype = {
209209
/*
210210
* Return all objects that could collide with the given object.
211211
*
212-
* @method Phaser.Physics.Arcade.QuadTree#retrieve
212+
* @method Phaser.QuadTree#retrieve
213213
* @param {object} rect - Description.
214214
* @Return {array} - Array with all detected objects.
215215
*/
@@ -245,7 +245,7 @@ Phaser.Physics.Arcade.QuadTree.prototype = {
245245

246246
/*
247247
* Clear the quadtree.
248-
* @method Phaser.Physics.Arcade.QuadTree#clear
248+
* @method Phaser.QuadTree#clear
249249
*/
250250
clear: function () {
251251

src/physics/Physics.js

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -137,41 +137,48 @@ Phaser.Physics.prototype = {
137137
*
138138
* @method Phaser.Physics#enable
139139
* @param {object|array} object - The game object to create the physics body on. Can also be an array of objects, a body will be created on every object in the array.
140-
* @param {number} [system=0] - The physics system that will be used to create the body. Defaults to Arcade Physics.
140+
* @param {number} [system=Phaser.Physics.ARCADE] - The physics system that will be used to create the body. Defaults to Arcade Physics.
141141
*/
142142
enable: function (object, system) {
143143

144144
if (typeof system === 'undefined') { system = Phaser.Physics.ARCADE; }
145145

146146
var i = 1;
147147

148-
if (Array.isArray(object))
148+
if (object instanceof Phaser.Group)
149149
{
150-
// Add to Group
151-
i = object.length;
150+
152151
}
153152
else
154153
{
155-
object = [object];
156-
}
154+
if (Array.isArray(object))
155+
{
156+
// Add to Group
157+
i = object.length;
158+
}
159+
else
160+
{
161+
object = [object];
162+
}
157163

158-
while (i--)
159-
{
160-
if (object[i].body === null)
164+
while (i--)
161165
{
162-
if (system === Phaser.Physics.ARCADE)
163-
{
164-
object[i].body = new Phaser.Physics.Arcade.Body(object[i]);
165-
}
166-
else if (system === Phaser.Physics.P2)
167-
{
168-
object[i].body = new Phaser.Physics.P2.Body(this.game, object[i], object[i].x, object[i].y, 1);
169-
object[i].anchor.set(0.5);
170-
}
171-
else if (system === Phaser.Physics.NINJA)
166+
if (object[i].body === null)
172167
{
173-
object[i].body = new Phaser.Physics.Ninja.Body(this.ninja, object[i]);
174-
object[i].anchor.set(0.5);
168+
if (system === Phaser.Physics.ARCADE)
169+
{
170+
object[i].body = new Phaser.Physics.Arcade.Body(object[i]);
171+
}
172+
else if (system === Phaser.Physics.P2)
173+
{
174+
object[i].body = new Phaser.Physics.P2.Body(this.game, object[i], object[i].x, object[i].y, 1);
175+
object[i].anchor.set(0.5);
176+
}
177+
else if (system === Phaser.Physics.NINJA)
178+
{
179+
object[i].body = new Phaser.Physics.Ninja.Body(this.ninja, object[i]);
180+
object[i].anchor.set(0.5);
181+
}
175182
}
176183
}
177184
}

src/physics/arcade/World.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,64 @@ Phaser.Physics.Arcade.prototype.constructor = Phaser.Physics.Arcade;
165165

166166
Phaser.Physics.Arcade.prototype = {
167167

168+
/**
169+
* This will create an Arcade Physics body on the given game object or array of game objects.
170+
* A game object can only have 1 physics body active at any one time, and it can't be changed until the object is destroyed.
171+
*
172+
* @method Phaser.Physics.Arcade#enable
173+
* @param {object|array|Phaser.Group} object - The game object to create the physics body on. Can also be an array of objects, a body will be created on every object in the array that has a body parameter.
174+
* @param {boolean} [children=true] - Should a body be created on all children of this object? If true it will propagate down the display list.
175+
*/
176+
enable: function (object, children) {
177+
178+
if (typeof children === 'undefined') { children = true; }
179+
180+
var i = 1;
181+
182+
if (Array.isArray(object))
183+
{
184+
// Add to Group
185+
i = object.length;
186+
}
187+
else
188+
{
189+
object = [object];
190+
}
191+
192+
while (i--)
193+
{
194+
if (object[i] instanceof Phaser.Group)
195+
{
196+
object[i].forEach(this.enableBody, this, false, children);
197+
}
198+
else
199+
{
200+
this.enableBody(object[i]);
201+
}
202+
}
203+
204+
},
205+
206+
enableBody: function (object, children) {
207+
208+
if (object instanceof Phaser.Group)
209+
{
210+
this.enable(object, true, children);
211+
return;
212+
}
213+
214+
if (object.hasOwnProperty('body') && object.body === null)
215+
{
216+
object.body = new Phaser.Physics.Arcade.Body(object);
217+
}
218+
219+
if (children && object.hasOwnProperty('children'))
220+
{
221+
object.children.forEach(this.enable, this);
222+
}
223+
224+
},
225+
168226
/**
169227
* Called automatically by a Physics body, it updates all motion related values on the Body.
170228
*

0 commit comments

Comments
 (0)