Added Collide event into the world.

This commit is contained in:
photonstorm 2017-08-16 16:30:28 +01:00
parent 01a6592308
commit 8a71a2178c
4 changed files with 70 additions and 10 deletions

View file

@ -1,6 +1,7 @@
var SeperateX = require('./SeperateX');
var SeperateY = require('./SeperateY');
var COLLIDES = require('./COLLIDES');
var Events = require('./events');
// Impact Physics Solver
@ -30,6 +31,8 @@ var Solver = function (world, bodyA, bodyB)
bodyA.collideWith(bodyB, 'y');
bodyB.collideWith(bodyA, 'y');
world.events.dispatch(new Events.COLLIDE(bodyA, bodyB));
}
else if (bodyA.last.y + bodyA.size.y > bodyB.last.y && bodyA.last.y < bodyB.last.y + bodyB.size.y)
{
@ -44,6 +47,8 @@ var Solver = function (world, bodyA, bodyB)
bodyA.collideWith(bodyB, 'x');
bodyB.collideWith(bodyA, 'x');
world.events.dispatch(new Events.COLLIDE(bodyA, bodyB));
}
};

View file

@ -19,6 +19,8 @@ var World = new Class({
this.scene = scene;
this.events = scene.sys.events;
this.bodies = new Set();
this.gravity = gravity;
@ -30,6 +32,13 @@ var World = new Class({
this.delta = 0;
this.timeScale = 1;
// Impacts maximum time step is 20 fps.
this.maxStep = 0.05;
this.enabled = true;
this._lastId = 0;
},
@ -47,19 +56,31 @@ var World = new Class({
return body;
},
pause: function ()
{
this.enabled = false;
return this;
},
resume: function ()
{
this.enabled = true;
return this;
},
update: function (time, delta)
{
if (this.bodies.size === 0)
if (!this.enabled || this.bodies.size === 0)
{
return;
}
// Impact uses a divided delta value
delta /= 1000;
// Impact uses a divided delta value that is clamped to the maxStep (20fps) maximum
this.delta = Math.min(delta / 1000, this.maxStep) * this.timeScale;
this.delta = delta;
// Update all bodies
// Update all active bodies
var i;
var body;
@ -68,19 +89,17 @@ var World = new Class({
var hash = {};
var size = this.cellSize;
// Update all active bodies
for (i = 0; i < len; i++)
{
body = bodies[i];
if (body.enabled)
{
body.update(delta);
body.update(this.delta);
}
}
// Run collision against them all now they're in the new positions
// Run collision against them all now they're in the new positions from the udpate
for (i = 0; i < len; i++)
{

View file

@ -0,0 +1,29 @@
var Class = require('../../../utils/Class');
var Event = require('../../../events/Event');
var CollideEvent = new Class({
Extends: Event,
initialize:
function CollideEvent (bodyA, bodyB)
{
Event.call(this, 'COLLIDE_EVENT');
// The first body involved in the collision]
this.bodyA = bodyA;
// The second body involved in the collision]
this.bodyB = bodyB;
// The Game Object associated with bodyA (if any)
this.gameObjectA = bodyA.gameObject;
// The Game Object associated with bodyB (if any)
this.gameObjectB = bodyB.gameObject;
}
});
module.exports = CollideEvent;

View file

@ -0,0 +1,7 @@
// Phaser.Physics.Impact.Events
module.exports = {
COLLIDE: require('./CollideEvent')
};