Lots of updates to Impact physics system

New Sprite and Image handlers with helper methods.
Updated motion, world and new Factory class.
This commit is contained in:
Richard Davey 2017-08-15 23:38:35 +01:00
parent 8cf216a530
commit cc781c5ee2
6 changed files with 142 additions and 28 deletions

View file

@ -0,0 +1,42 @@
var Class = require('../../utils/Class');
var ImpactImage = require('./ImpactImage');
var ImpactSprite = require('./ImpactSprite');
var Factory = new Class({
initialize:
function Factory (world)
{
this.world = world;
this.sys = world.scene.sys;
},
body: function (x, y, sizeX, sizeY)
{
return this.world.create(x, y, sizeX, sizeY);
},
image: function (x, y, key, frame)
{
var image = new ImpactImage(this.world, x, y, key, frame);
this.sys.displayList.add(image);
return image;
},
sprite: function (x, y, key, frame)
{
var sprite = new ImpactSprite(this.world, x, y, key, frame);
this.sys.displayList.add(sprite);
this.sys.updateList.add(sprite);
return sprite;
}
});
module.exports = Factory;

View file

@ -0,0 +1,38 @@
var Class = require('../../utils/Class');
var Components = require('./Components');
var Image = require('../../gameobjects/image/Image');
var ImpactImage = new Class({
Extends: Image,
Mixins: [
Components.Acceleration,
Components.BodyType,
Components.Bounce,
Components.CheckAgainst,
Components.Collides,
Components.Velocity
],
initialize:
function ImpactImage (world, x, y, texture, frame)
{
Image.call(this, world.scene, x, y, texture, frame);
this.body = world.create(x, y, this.width, this.height);
this.body.gameObject = this;
// Local references to the Body properties
this.vel = this.body.vel;
this.accel = this.body.accel;
this.friction = this.body.friction;
this.maxVel = this.body.maxVel;
}
});
module.exports = ImpactImage;

View file

@ -0,0 +1,38 @@
var Class = require('../../utils/Class');
var Components = require('./Components');
var Sprite = require('../../gameobjects/sprite/Sprite');
var ImpactSprite = new Class({
Extends: Sprite,
Mixins: [
Components.Acceleration,
Components.BodyType,
Components.Bounce,
Components.CheckAgainst,
Components.Collides,
Components.Velocity
],
initialize:
function ImpactSprite (world, x, y, texture, frame)
{
Sprite.call(this, world.scene, x, y, texture, frame);
this.body = world.create(x, y, this.width, this.height);
this.body.gameObject = this;
// Local references to the Body properties
this.vel = this.body.vel;
this.accel = this.body.accel;
this.friction = this.body.friction;
this.maxVel = this.body.maxVel;
}
});
module.exports = ImpactSprite;

View file

@ -67,18 +67,7 @@ var UpdateMotion = function (body, res)
body.standing = true;
}
}
if (window.dumpit)
{
console.group('slope');
console.log(s);
console.log('pos', res.pos.x, res.pos.y);
console.log('vel', body.vel.x, body.vel.y);
console.groupEnd();
}
}
// body.pos = res.pos;
body.pos.x = res.pos.x;
body.pos.y = res.pos.y;

View file

@ -1,22 +1,24 @@
// Phaser.Physics.Impact.World
var Class = require('../../utils/Class');
var Set = require('../../structs/Set');
var Body = require('./Body');
var Solver = require('./Solver');
var CollisionMap = require('./CollisionMap');
var Class = require('../../utils/Class');
var COLLIDES = require('./COLLIDES');
var CollisionMap = require('./CollisionMap');
var Set = require('../../structs/Set');
var Solver = require('./Solver');
var TYPE = require('./TYPE');
var World = new Class({
initialize:
function World (gravity, cellSize)
function World (scene, gravity, cellSize)
{
if (gravity === undefined) { gravity = 0; }
if (cellSize === undefined) { cellSize = 64; }
this.scene = scene;
this.bodies = new Set();
this.gravity = gravity;
@ -52,22 +54,27 @@ var World = new Class({
// Update all bodies
this.bodies.iterate(function (body)
var i;
var bodies = this.bodies.entries;
var len = bodies.length;
var hash = {};
var size = this.cellSize;
// Update all active bodies
for (i = 0; i < len; i++)
{
var body = bodies[i];
if (body.enabled)
{
body.update(delta);
}
});
}
// Run collision against them all
// Run collision against them all now they're in the new positions
var hash = {};
var size = this.cellSize;
var bodies = this.bodies.entries;
for (var i = 0; i < bodies.length; i++)
for (i = 0; i < len; i++)
{
var body = bodies[i];
@ -85,9 +92,8 @@ var World = new Class({
// Check the body against the spatial hash
checkHash: function (body, hash, size)
{
// console.log('checkHash');
var checked = {};
var xmin = Math.floor(body.pos.x / size);
var ymin = Math.floor(body.pos.y / size);
var xmax = Math.floor((body.pos.x + body.size.x) / size) + 1;

View file

@ -18,6 +18,7 @@ module.exports = {
COLLIDES: require('./COLLIDES'),
CollisionMap: require('./CollisionMap'),
TYPE: require('./TYPE'),
World: require('./World')
World: require('./World'),
Factory: require('./Factory')
};