This commit is contained in:
Michael Hadley 2017-11-09 19:48:36 -06:00
commit 35ecba702b
5 changed files with 112 additions and 7 deletions

View file

@ -558,7 +558,7 @@ var Body = new Class({
var y = pos.y + this.halfHeight; var y = pos.y + this.halfHeight;
graphic.lineStyle(1, this.world.defaults.velocityDebugColor, 1); graphic.lineStyle(1, this.world.defaults.velocityDebugColor, 1);
graphic.lineBetween(x, y, x + this.velocity.x, y + this.velocity.y); graphic.lineBetween(x, y, x + this.velocity.x / 2, y + this.velocity.y / 2);
} }
}, },

View file

@ -38,6 +38,8 @@ var Collider = new Class({
{ {
this.world.removeCollider(this); this.world.removeCollider(this);
this.active = false;
this.world = null; this.world = null;
this.object1 = null; this.object1 = null;

View file

@ -8,6 +8,7 @@ var GetValue = require('../../utils/object/GetValue');
var Rectangle = require('../../geom/rectangle/Rectangle'); var Rectangle = require('../../geom/rectangle/Rectangle');
var RTree = require('../../structs/RTree'); var RTree = require('../../structs/RTree');
var Set = require('../../structs/Set'); var Set = require('../../structs/Set');
var ProcessQueue = require('../../structs/ProcessQueue');
var StaticBody = require('./StaticBody'); var StaticBody = require('./StaticBody');
var Vector2 = require('../../math/Vector2'); var Vector2 = require('../../math/Vector2');
@ -27,7 +28,7 @@ var World = new Class({
// Static Bodies // Static Bodies
this.staticBodies = new Set(); this.staticBodies = new Set();
this.colliders = []; this.colliders = new ProcessQueue();
this.gravity = new Vector2(GetValue(config, 'gravity.x', 0), GetValue(config, 'gravity.y', 0)); this.gravity = new Vector2(GetValue(config, 'gravity.x', 0), GetValue(config, 'gravity.y', 0));
@ -231,7 +232,7 @@ var World = new Class({
var collider = new Collider(this, false, object1, object2, collideCallback, processCallback, callbackContext); var collider = new Collider(this, false, object1, object2, collideCallback, processCallback, callbackContext);
this.colliders.push(collider); this.colliders.add(collider);
return collider; return collider;
}, },
@ -244,14 +245,14 @@ var World = new Class({
var collider = new Collider(this, true, object1, object2, collideCallback, processCallback, callbackContext); var collider = new Collider(this, true, object1, object2, collideCallback, processCallback, callbackContext);
this.colliders.push(collider); this.colliders.add(collider);
return collider; return collider;
}, },
removeCollider: function (collider) removeCollider: function (collider)
{ {
// TODO this.colliders.remove(collider);
}, },
update: function (time, delta) update: function (time, delta)
@ -288,10 +289,11 @@ var World = new Class({
this.tree.load(bodies); this.tree.load(bodies);
// Process any colliders // Process any colliders
var colliders = this.colliders.update();
for (i = 0; i < this.colliders.length; i++) for (i = 0; i < colliders.length; i++)
{ {
var collider = this.colliders[i]; var collider = colliders[i];
if (collider.active) if (collider.active)
{ {

View file

@ -0,0 +1,99 @@
// Phaser.Structs.ProcessQueue
var Class = require('../utils/Class');
var ProcessQueue = new Class({
initialize:
function ProcessQueue ()
{
this._pending = [];
this._active = [];
this._destroy = [];
this._toProcess = 0;
},
add: function (item)
{
this._pending.push(item);
this._toProcess++;
return this;
},
remove: function (item)
{
this._destroy.push(item);
this._toProcess++;
return this;
},
update: function ()
{
if (this._toProcess === 0)
{
// Quick bail
return this._active;
}
var list = this._destroy;
var active = this._active;
var i;
var item;
// Clear the 'destroy' list
for (i = 0; i < list.length; i++)
{
item = list[i];
// Remove from the 'active' array
var idx = active.indexOf(item);
if (idx !== -1)
{
active.splice(idx, 1);
}
}
list.length = 0;
// Process the pending addition list
// This stops callbacks and out of sync events from populating the active array mid-way during an update
list = this._pending;
for (i = 0; i < list.length; i++)
{
item = list[i];
this._active.push(item);
}
list.length = 0;
this._toProcess = 0;
// The owner of this queue can now safely do whatever it needs to with the active list
return this._active;
},
getActive: function ()
{
return this._active;
},
destroy: function ()
{
this._pending = [];
this._active = [];
this._destroy = [];
}
});
module.exports = ProcessQueue;

View file

@ -2,7 +2,9 @@
module.exports = { module.exports = {
List: require('./List'),
Map: require('./Map'), Map: require('./Map'),
ProcessQueue: require('./ProcessQueue'),
RTree: require('./RTree'), RTree: require('./RTree'),
Set: require('./Set') Set: require('./Set')