2017-06-22 00:47:35 +01:00
|
|
|
// Phaser.Physics.Impact.Body
|
|
|
|
|
|
|
|
var Class = require('../../utils/Class');
|
2017-06-22 16:22:21 +01:00
|
|
|
var GetVelocity = require('./GetVelocity');
|
2017-06-22 00:47:35 +01:00
|
|
|
var UpdateMotion = require('./UpdateMotion');
|
|
|
|
var COLLIDES = require('./COLLIDES');
|
|
|
|
var TYPE = require('./TYPE');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An Impact.js compatible physics body.
|
|
|
|
* This re-creates the properties you'd get on an Entity and the math needed to update them.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
*/
|
|
|
|
|
|
|
|
var Body = new Class({
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2017-06-22 02:40:10 +01:00
|
|
|
function Body (world, x, y, sx, sy)
|
2017-06-22 00:47:35 +01:00
|
|
|
{
|
2017-06-22 02:40:10 +01:00
|
|
|
if (sx === undefined) { sx = 16; }
|
|
|
|
if (sy === undefined) { sy = 16; }
|
|
|
|
|
2017-06-22 00:47:35 +01:00
|
|
|
this.world = world;
|
|
|
|
|
|
|
|
this.enabled = true;
|
|
|
|
|
2017-06-22 02:40:10 +01:00
|
|
|
this.size = { x: sx, y: sy };
|
2017-06-22 00:47:35 +01:00
|
|
|
this.pos = { x: x, y: y };
|
2017-06-22 16:22:21 +01:00
|
|
|
this.last = { x: x, y: y };
|
2017-06-22 00:47:35 +01:00
|
|
|
this.vel = { x: 0, y: 0 };
|
|
|
|
this.accel = { x: 0, y: 0 };
|
|
|
|
this.friction = { x: 0, y: 0 };
|
|
|
|
this.maxVel = { x: 100, y: 100 };
|
|
|
|
this.gravityFactor = 1;
|
|
|
|
this.standing = false;
|
|
|
|
this.bounciness = 0;
|
|
|
|
this.minBounceVelocity = 40;
|
2017-06-22 16:22:21 +01:00
|
|
|
|
|
|
|
this.accelGround = 0;
|
|
|
|
this.accelAir = 0;
|
|
|
|
this.jumpSpeed = 0;
|
2017-06-22 00:47:35 +01:00
|
|
|
|
|
|
|
this.type = TYPE.NONE;
|
|
|
|
this.checkAgainst = TYPE.NONE;
|
|
|
|
this.collides = COLLIDES.NEVER;
|
|
|
|
|
|
|
|
// min 44 deg, max 136 deg
|
|
|
|
this.slopeStanding = { min: 0.767944870877505, max: 2.3736477827122884 };
|
|
|
|
},
|
|
|
|
|
2017-06-22 16:22:21 +01:00
|
|
|
reset: function ()
|
|
|
|
{
|
|
|
|
// TODO
|
|
|
|
},
|
|
|
|
|
2017-06-22 00:47:35 +01:00
|
|
|
update: function (delta)
|
|
|
|
{
|
|
|
|
this.last.x = this.pos.x;
|
|
|
|
this.last.y = this.pos.y;
|
|
|
|
|
|
|
|
this.vel.y += this.world.gravity * delta * this.gravityFactor;
|
|
|
|
|
2017-06-22 16:22:21 +01:00
|
|
|
this.vel.x = GetVelocity(delta, this.vel.x, this.accel.x, this.friction.x, this.maxVel.x);
|
|
|
|
this.vel.y = GetVelocity(delta, this.vel.y, this.accel.y, this.friction.y, this.maxVel.y);
|
2017-06-22 00:47:35 +01:00
|
|
|
|
2017-06-23 18:08:22 +01:00
|
|
|
if (window.dumpit)
|
|
|
|
{
|
|
|
|
console.log('');
|
|
|
|
console.log('UPDATE: pos', this.pos.x, this.pos.y, 'vel', this.vel.x, this.vel.y);
|
|
|
|
}
|
|
|
|
|
2017-06-22 00:47:35 +01:00
|
|
|
var mx = this.vel.x * delta;
|
|
|
|
var my = this.vel.y * delta;
|
|
|
|
|
2017-06-22 02:40:10 +01:00
|
|
|
var res = this.world.collisionMap.trace(this.pos.x, this.pos.y, mx, my, this.size.x, this.size.y);
|
2017-06-22 00:47:35 +01:00
|
|
|
|
2017-06-27 02:35:38 +01:00
|
|
|
if (this.handleMovementTrace(res))
|
|
|
|
{
|
|
|
|
UpdateMotion(this, res);
|
|
|
|
}
|
2017-06-23 18:08:22 +01:00
|
|
|
|
|
|
|
if (window.dumpit)
|
|
|
|
{
|
|
|
|
console.log('END res', res.pos.x, res.pos.y);
|
|
|
|
}
|
2017-06-22 00:47:35 +01:00
|
|
|
},
|
|
|
|
|
2017-06-27 02:35:38 +01:00
|
|
|
handleMovementTrace: function (res)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2017-06-22 00:47:35 +01:00
|
|
|
skipHash: function ()
|
|
|
|
{
|
|
|
|
return (!this.enabled || (this.type === 0 && this.checkAgainst === 0 && this.collides === 0));
|
|
|
|
},
|
|
|
|
|
|
|
|
touches: function (other)
|
|
|
|
{
|
|
|
|
return !(
|
|
|
|
this.pos.x >= other.pos.x + other.size.x ||
|
|
|
|
this.pos.x + this.size.x <= other.pos.x ||
|
|
|
|
this.pos.y >= other.pos.y + other.size.y ||
|
|
|
|
this.pos.y + this.size.y <= other.pos.y
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2017-06-22 02:40:10 +01:00
|
|
|
setBounce: function (value)
|
|
|
|
{
|
|
|
|
this.bounciness = value;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-06-22 00:47:35 +01:00
|
|
|
setVelocityX: function (x)
|
|
|
|
{
|
|
|
|
this.vel.x = x;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
setVelocityY: function (y)
|
|
|
|
{
|
|
|
|
this.vel.y = y;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
setVelocity: function (x, y)
|
|
|
|
{
|
|
|
|
this.vel.x = x;
|
|
|
|
this.vel.y = y;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
setMaxVelocity: function (x, y)
|
|
|
|
{
|
|
|
|
if (y === undefined) { y = x; }
|
|
|
|
|
|
|
|
this.maxVel.x = x;
|
|
|
|
this.maxVel.y = y;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-06-22 16:22:21 +01:00
|
|
|
setAccelerationX: function (x)
|
|
|
|
{
|
|
|
|
this.accel.x = x;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
setAccelerationY: function (y)
|
|
|
|
{
|
|
|
|
this.accel.y = y;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
setAcceleration: function (x, y)
|
|
|
|
{
|
|
|
|
this.accel.x = x;
|
|
|
|
this.accel.y = y;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-06-22 00:47:35 +01:00
|
|
|
setTypeNone: function ()
|
|
|
|
{
|
|
|
|
this.type = TYPE.NONE;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
setTypeA: function ()
|
|
|
|
{
|
|
|
|
this.type = TYPE.A;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
setTypeB: function ()
|
|
|
|
{
|
|
|
|
this.type = TYPE.B;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
setCheckAgainstNone: function ()
|
|
|
|
{
|
|
|
|
this.checkAgainst = TYPE.NONE;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
setCheckAgainstA: function ()
|
|
|
|
{
|
|
|
|
this.checkAgainst = TYPE.A;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
setCheckAgainstB: function ()
|
|
|
|
{
|
|
|
|
this.checkAgainst = TYPE.B;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
setCollidesNever: function ()
|
|
|
|
{
|
|
|
|
this.collides = COLLIDES.NEVER;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
setLite: function ()
|
|
|
|
{
|
|
|
|
this.collides = COLLIDES.LITE;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
setPassive: function ()
|
|
|
|
{
|
|
|
|
this.collides = COLLIDES.PASSIVE;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
setActive: function ()
|
|
|
|
{
|
|
|
|
this.collides = COLLIDES.ACTIVE;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
setFixed: function ()
|
|
|
|
{
|
|
|
|
this.collides = COLLIDES.FIXED;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-06-22 16:22:21 +01:00
|
|
|
check: function (other)
|
|
|
|
{
|
|
|
|
// Overridden by user code
|
|
|
|
},
|
2017-06-22 00:47:35 +01:00
|
|
|
|
2017-06-22 16:22:21 +01:00
|
|
|
collideWith: function (other, axis)
|
|
|
|
{
|
|
|
|
// Overridden by user code
|
|
|
|
}
|
2017-06-22 00:47:35 +01:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = Body;
|