mirror of
https://github.com/photonstorm/phaser
synced 2024-11-26 22:52:14 +00:00
Restored Phaser.QuadTree - should get all remaining Arcade Physics examples working again.
This commit is contained in:
parent
480c1819d6
commit
4db1fc0079
8 changed files with 105 additions and 38 deletions
|
@ -153,6 +153,7 @@ module.exports = function (grunt) {
|
|||
|
||||
'src/math/Math.js',
|
||||
'src/math/RandomDataGenerator.js',
|
||||
'src/math/QuadTree.js',
|
||||
|
||||
'src/net/Net.js',
|
||||
|
||||
|
@ -184,7 +185,6 @@ module.exports = function (grunt) {
|
|||
|
||||
'src/physics/arcade/World.js',
|
||||
'src/physics/arcade/Body.js',
|
||||
'src/physics/arcade/QuadTree.js',
|
||||
|
||||
'src/physics/ninja/World.js',
|
||||
'src/physics/ninja/Body.js',
|
||||
|
|
|
@ -108,6 +108,7 @@
|
|||
|
||||
<script src="$path/src/math/Math.js"></script>
|
||||
<script src="$path/src/math/RandomDataGenerator.js"></script>
|
||||
<script src="$path/src/math/QuadTree.js"></script>
|
||||
|
||||
<script src="$path/src/net/Net.js"></script>
|
||||
|
||||
|
@ -139,7 +140,6 @@
|
|||
|
||||
<script src="$path/src/physics/arcade/World.js"></script>
|
||||
<script src="$path/src/physics/arcade/Body.js"></script>
|
||||
<script src="$path/src/physics/arcade/QuadTree.js"></script>
|
||||
|
||||
<script src="$path/src/physics/ninja/World.js"></script>
|
||||
<script src="$path/src/physics/ninja/Body.js"></script>
|
||||
|
|
|
@ -114,6 +114,7 @@
|
|||
|
||||
<script src="../src/math/Math.js"></script>
|
||||
<script src="../src/math/RandomDataGenerator.js"></script>
|
||||
<script src="../src/math/QuadTree.js"></script>
|
||||
|
||||
<script src="../src/net/Net.js"></script>
|
||||
|
||||
|
|
|
@ -114,6 +114,7 @@
|
|||
|
||||
<script src="../src/math/Math.js"></script>
|
||||
<script src="../src/math/RandomDataGenerator.js"></script>
|
||||
<script src="../src/math/QuadTree.js"></script>
|
||||
|
||||
<script src="../src/net/Net.js"></script>
|
||||
|
||||
|
@ -145,7 +146,6 @@
|
|||
|
||||
<script src="../src/physics/arcade/World.js"></script>
|
||||
<script src="../src/physics/arcade/Body.js"></script>
|
||||
<script src="../src/physics/arcade/QuadTree.js"></script>
|
||||
|
||||
<script src="../src/physics/ninja/World.js"></script>
|
||||
<script src="../src/physics/ninja/Body.js"></script>
|
||||
|
|
|
@ -17,7 +17,7 @@ function create() {
|
|||
game.stage.backgroundColor = 0x3d4d3d;
|
||||
|
||||
mummy = game.add.sprite(0, 300, 'mummy', 5);
|
||||
mummy.scale.set(2);
|
||||
// mummy.scale.set(2);
|
||||
|
||||
anim = mummy.animations.add('walk');
|
||||
|
||||
|
@ -26,6 +26,7 @@ function create() {
|
|||
// game.onPause.add(paused, this);
|
||||
// game.onResume.add(resumed, this);
|
||||
|
||||
game.add.tween(mummy.scale).to({x:4,y:4}, 1000, Phaser.Easing.Linear.None, true);
|
||||
t = game.add.tween(mummy).to({x:700}, 15000, Phaser.Easing.Linear.None, true);
|
||||
t.onComplete.add(tweenOver, this);
|
||||
|
||||
|
|
|
@ -44,7 +44,7 @@
|
|||
/**
|
||||
* QuadTree Constructor
|
||||
*
|
||||
* @class Phaser.Physics.Arcade.QuadTree
|
||||
* @class Phaser.QuadTree
|
||||
* @classdesc A QuadTree implementation. The original code was a conversion of the Java code posted to GameDevTuts. However I've tweaked
|
||||
* 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/
|
||||
* @constructor
|
||||
|
@ -57,7 +57,7 @@
|
|||
* @param {number} maxLevels - Description.
|
||||
* @param {number} level - Description.
|
||||
*/
|
||||
Phaser.Physics.Arcade.QuadTree = function (physicsManager, x, y, width, height, maxObjects, maxLevels, level) {
|
||||
Phaser.QuadTree = function (physicsManager, x, y, width, height, maxObjects, maxLevels, level) {
|
||||
|
||||
this.physicsManager = physicsManager;
|
||||
this.ID = physicsManager.quadTreeID;
|
||||
|
@ -83,28 +83,28 @@ Phaser.Physics.Arcade.QuadTree = function (physicsManager, x, y, width, height,
|
|||
|
||||
};
|
||||
|
||||
Phaser.Physics.Arcade.QuadTree.prototype = {
|
||||
Phaser.QuadTree.prototype = {
|
||||
|
||||
/*
|
||||
* Split the node into 4 subnodes
|
||||
*
|
||||
* @method Phaser.Physics.Arcade.QuadTree#split
|
||||
* @method Phaser.QuadTree#split
|
||||
*/
|
||||
split: function() {
|
||||
|
||||
this.level++;
|
||||
|
||||
// top right node
|
||||
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);
|
||||
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);
|
||||
|
||||
// top left node
|
||||
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);
|
||||
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);
|
||||
|
||||
// bottom left node
|
||||
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);
|
||||
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);
|
||||
|
||||
// bottom right node
|
||||
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);
|
||||
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);
|
||||
|
||||
},
|
||||
|
||||
|
@ -113,7 +113,7 @@ Phaser.Physics.Arcade.QuadTree.prototype = {
|
|||
* exceeds the capacity, it will split and add all
|
||||
* objects to their corresponding subnodes.
|
||||
*
|
||||
* @method Phaser.Physics.Arcade.QuadTree#insert
|
||||
* @method Phaser.QuadTree#insert
|
||||
* @param {object} body - Description.
|
||||
*/
|
||||
insert: function (body) {
|
||||
|
@ -165,7 +165,7 @@ Phaser.Physics.Arcade.QuadTree.prototype = {
|
|||
/*
|
||||
* Determine which node the object belongs to.
|
||||
*
|
||||
* @method Phaser.Physics.Arcade.QuadTree#getIndex
|
||||
* @method Phaser.QuadTree#getIndex
|
||||
* @param {object} rect - Description.
|
||||
* @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.
|
||||
*/
|
||||
|
@ -209,7 +209,7 @@ Phaser.Physics.Arcade.QuadTree.prototype = {
|
|||
/*
|
||||
* Return all objects that could collide with the given object.
|
||||
*
|
||||
* @method Phaser.Physics.Arcade.QuadTree#retrieve
|
||||
* @method Phaser.QuadTree#retrieve
|
||||
* @param {object} rect - Description.
|
||||
* @Return {array} - Array with all detected objects.
|
||||
*/
|
||||
|
@ -245,7 +245,7 @@ Phaser.Physics.Arcade.QuadTree.prototype = {
|
|||
|
||||
/*
|
||||
* Clear the quadtree.
|
||||
* @method Phaser.Physics.Arcade.QuadTree#clear
|
||||
* @method Phaser.QuadTree#clear
|
||||
*/
|
||||
clear: function () {
|
||||
|
|
@ -137,7 +137,7 @@ Phaser.Physics.prototype = {
|
|||
*
|
||||
* @method Phaser.Physics#enable
|
||||
* @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.
|
||||
* @param {number} [system=0] - The physics system that will be used to create the body. Defaults to Arcade Physics.
|
||||
* @param {number} [system=Phaser.Physics.ARCADE] - The physics system that will be used to create the body. Defaults to Arcade Physics.
|
||||
*/
|
||||
enable: function (object, system) {
|
||||
|
||||
|
@ -145,33 +145,40 @@ Phaser.Physics.prototype = {
|
|||
|
||||
var i = 1;
|
||||
|
||||
if (Array.isArray(object))
|
||||
if (object instanceof Phaser.Group)
|
||||
{
|
||||
// Add to Group
|
||||
i = object.length;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
object = [object];
|
||||
}
|
||||
|
||||
while (i--)
|
||||
{
|
||||
if (object[i].body === null)
|
||||
if (Array.isArray(object))
|
||||
{
|
||||
if (system === Phaser.Physics.ARCADE)
|
||||
// Add to Group
|
||||
i = object.length;
|
||||
}
|
||||
else
|
||||
{
|
||||
object = [object];
|
||||
}
|
||||
|
||||
while (i--)
|
||||
{
|
||||
if (object[i].body === null)
|
||||
{
|
||||
object[i].body = new Phaser.Physics.Arcade.Body(object[i]);
|
||||
}
|
||||
else if (system === Phaser.Physics.P2)
|
||||
{
|
||||
object[i].body = new Phaser.Physics.P2.Body(this.game, object[i], object[i].x, object[i].y, 1);
|
||||
object[i].anchor.set(0.5);
|
||||
}
|
||||
else if (system === Phaser.Physics.NINJA)
|
||||
{
|
||||
object[i].body = new Phaser.Physics.Ninja.Body(this.ninja, object[i]);
|
||||
object[i].anchor.set(0.5);
|
||||
if (system === Phaser.Physics.ARCADE)
|
||||
{
|
||||
object[i].body = new Phaser.Physics.Arcade.Body(object[i]);
|
||||
}
|
||||
else if (system === Phaser.Physics.P2)
|
||||
{
|
||||
object[i].body = new Phaser.Physics.P2.Body(this.game, object[i], object[i].x, object[i].y, 1);
|
||||
object[i].anchor.set(0.5);
|
||||
}
|
||||
else if (system === Phaser.Physics.NINJA)
|
||||
{
|
||||
object[i].body = new Phaser.Physics.Ninja.Body(this.ninja, object[i]);
|
||||
object[i].anchor.set(0.5);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -165,6 +165,64 @@ Phaser.Physics.Arcade.prototype.constructor = Phaser.Physics.Arcade;
|
|||
|
||||
Phaser.Physics.Arcade.prototype = {
|
||||
|
||||
/**
|
||||
* This will create an Arcade Physics body on the given game object or array of game objects.
|
||||
* 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.
|
||||
*
|
||||
* @method Phaser.Physics.Arcade#enable
|
||||
* @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.
|
||||
* @param {boolean} [children=true] - Should a body be created on all children of this object? If true it will propagate down the display list.
|
||||
*/
|
||||
enable: function (object, children) {
|
||||
|
||||
if (typeof children === 'undefined') { children = true; }
|
||||
|
||||
var i = 1;
|
||||
|
||||
if (Array.isArray(object))
|
||||
{
|
||||
// Add to Group
|
||||
i = object.length;
|
||||
}
|
||||
else
|
||||
{
|
||||
object = [object];
|
||||
}
|
||||
|
||||
while (i--)
|
||||
{
|
||||
if (object[i] instanceof Phaser.Group)
|
||||
{
|
||||
object[i].forEach(this.enableBody, this, false, children);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.enableBody(object[i]);
|
||||
}
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
enableBody: function (object, children) {
|
||||
|
||||
if (object instanceof Phaser.Group)
|
||||
{
|
||||
this.enable(object, true, children);
|
||||
return;
|
||||
}
|
||||
|
||||
if (object.hasOwnProperty('body') && object.body === null)
|
||||
{
|
||||
object.body = new Phaser.Physics.Arcade.Body(object);
|
||||
}
|
||||
|
||||
if (children && object.hasOwnProperty('children'))
|
||||
{
|
||||
object.children.forEach(this.enable, this);
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Called automatically by a Physics body, it updates all motion related values on the Body.
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue