2017-06-21 23:47:35 +00:00
|
|
|
// Phaser.Physics.Impact.Body
|
|
|
|
|
|
|
|
var Class = require('../../utils/Class');
|
|
|
|
var COLLIDES = require('./COLLIDES');
|
2017-08-15 22:36:28 +00:00
|
|
|
var GetVelocity = require('./GetVelocity');
|
2017-06-21 23:47:35 +00:00
|
|
|
var TYPE = require('./TYPE');
|
2017-08-15 22:36:28 +00:00
|
|
|
var UpdateMotion = require('./UpdateMotion');
|
2017-06-21 23:47:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An Impact.js compatible physics body.
|
|
|
|
* This re-creates the properties you'd get on an Entity and the math needed to update them.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var Body = new Class({
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2017-06-22 01:40:10 +00:00
|
|
|
function Body (world, x, y, sx, sy)
|
2017-06-21 23:47:35 +00:00
|
|
|
{
|
2017-06-22 01:40:10 +00:00
|
|
|
if (sx === undefined) { sx = 16; }
|
2017-08-15 22:36:28 +00:00
|
|
|
if (sy === undefined) { sy = sx; }
|
2017-06-22 01:40:10 +00:00
|
|
|
|
2017-06-21 23:47:35 +00:00
|
|
|
this.world = world;
|
|
|
|
|
2017-08-15 22:36:28 +00:00
|
|
|
this.gameObject = null;
|
|
|
|
|
2017-06-21 23:47:35 +00:00
|
|
|
this.enabled = true;
|
|
|
|
|
2017-06-27 13:44:12 +00:00
|
|
|
this.parent;
|
2017-08-15 22:36:28 +00:00
|
|
|
|
2017-08-16 13:02:10 +00:00
|
|
|
this.id = world.getNextID();
|
|
|
|
|
2017-06-27 13:44:12 +00:00
|
|
|
this.name = '';
|
|
|
|
|
2017-06-22 01:40:10 +00:00
|
|
|
this.size = { x: sx, y: sy };
|
2017-08-16 18:31:59 +00:00
|
|
|
this.offset = { x: 0, y: 0 };
|
2017-06-21 23:47:35 +00:00
|
|
|
this.pos = { x: x, y: y };
|
2017-06-22 15:22:21 +00:00
|
|
|
this.last = { x: x, y: y };
|
2017-06-21 23:47:35 +00:00
|
|
|
this.vel = { x: 0, y: 0 };
|
|
|
|
this.accel = { x: 0, y: 0 };
|
|
|
|
this.friction = { x: 0, y: 0 };
|
2017-08-16 21:51:46 +00:00
|
|
|
this.maxVel = { x: world.defaults.maxVelocityX, y: world.defaults.maxVelocityY };
|
2017-08-15 22:36:28 +00:00
|
|
|
|
2017-06-21 23:47:35 +00:00
|
|
|
this.standing = false;
|
2017-08-15 22:36:28 +00:00
|
|
|
|
2017-08-16 21:51:46 +00:00
|
|
|
this.gravityFactor = world.defaults.gravityFactor;
|
|
|
|
this.bounciness = world.defaults.bounciness;
|
|
|
|
this.minBounceVelocity = world.defaults.minBounceVelocity;
|
2017-06-22 15:22:21 +00:00
|
|
|
|
|
|
|
this.accelGround = 0;
|
|
|
|
this.accelAir = 0;
|
|
|
|
this.jumpSpeed = 0;
|
2017-06-21 23:47:35 +00:00
|
|
|
|
|
|
|
this.type = TYPE.NONE;
|
|
|
|
this.checkAgainst = TYPE.NONE;
|
|
|
|
this.collides = COLLIDES.NEVER;
|
2017-08-16 16:15:35 +00:00
|
|
|
|
2017-08-16 22:14:30 +00:00
|
|
|
this.debugShowBody = world.defaults.debugShowBody;
|
|
|
|
this.debugShowVelocity = world.defaults.debugShowVelocity;
|
2017-08-16 21:51:46 +00:00
|
|
|
this.debugBodyColor = world.defaults.bodyDebugColor;
|
2017-06-21 23:47:35 +00:00
|
|
|
|
|
|
|
// min 44 deg, max 136 deg
|
|
|
|
this.slopeStanding = { min: 0.767944870877505, max: 2.3736477827122884 };
|
|
|
|
},
|
|
|
|
|
2017-08-16 15:30:38 +00:00
|
|
|
reset: function (x, y)
|
2017-06-22 15:22:21 +00:00
|
|
|
{
|
2017-08-16 15:30:38 +00:00
|
|
|
this.pos = { x: x, y: y };
|
|
|
|
this.last = { x: x, y: y };
|
|
|
|
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.standing = false;
|
|
|
|
|
|
|
|
this.gravityFactor = 1;
|
|
|
|
this.bounciness = 0;
|
|
|
|
this.minBounceVelocity = 40;
|
|
|
|
|
|
|
|
this.accelGround = 0;
|
|
|
|
this.accelAir = 0;
|
|
|
|
this.jumpSpeed = 0;
|
|
|
|
|
|
|
|
this.type = TYPE.NONE;
|
|
|
|
this.checkAgainst = TYPE.NONE;
|
|
|
|
this.collides = COLLIDES.NEVER;
|
2017-06-22 15:22:21 +00:00
|
|
|
},
|
|
|
|
|
2017-08-16 16:15:35 +00:00
|
|
|
update: function (delta, drawDebug)
|
2017-06-21 23:47:35 +00:00
|
|
|
{
|
2017-08-16 18:31:59 +00:00
|
|
|
var pos = this.pos;
|
|
|
|
|
|
|
|
this.last.x = pos.x;
|
|
|
|
this.last.y = pos.y;
|
2017-06-21 23:47:35 +00:00
|
|
|
|
|
|
|
this.vel.y += this.world.gravity * delta * this.gravityFactor;
|
|
|
|
|
2017-06-22 15:22:21 +00: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-21 23:47:35 +00:00
|
|
|
|
|
|
|
var mx = this.vel.x * delta;
|
|
|
|
var my = this.vel.y * delta;
|
|
|
|
|
2017-08-16 18:31:59 +00:00
|
|
|
var res = this.world.collisionMap.trace(pos.x, pos.y, mx, my, this.size.x, this.size.y);
|
2017-06-21 23:47:35 +00:00
|
|
|
|
2017-06-27 01:35:38 +00:00
|
|
|
if (this.handleMovementTrace(res))
|
|
|
|
{
|
|
|
|
UpdateMotion(this, res);
|
|
|
|
}
|
2017-06-23 17:08:22 +00:00
|
|
|
|
2017-08-16 18:31:59 +00:00
|
|
|
var go = this.gameObject;
|
|
|
|
|
|
|
|
if (go)
|
2017-06-23 17:08:22 +00:00
|
|
|
{
|
2017-08-16 18:31:59 +00:00
|
|
|
go.setPosition((pos.x - this.offset.x) + go.displayOriginX, (pos.y - this.offset.y) + go.displayOriginY);
|
2017-06-23 17:08:22 +00:00
|
|
|
}
|
2017-08-16 16:15:35 +00:00
|
|
|
|
2017-08-16 19:08:05 +00:00
|
|
|
if (drawDebug)
|
2017-08-16 16:15:35 +00:00
|
|
|
{
|
2017-08-16 19:08:05 +00:00
|
|
|
var graphic = this.world.debugGraphic;
|
|
|
|
|
|
|
|
if (this.debugShowBody)
|
|
|
|
{
|
|
|
|
graphic.lineStyle(1, this.debugBodyColor, 1);
|
|
|
|
graphic.strokeRect(pos.x, pos.y, this.size.x, this.size.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.debugShowVelocity)
|
|
|
|
{
|
|
|
|
var x = pos.x + this.size.x / 2;
|
|
|
|
var y = pos.y + this.size.y / 2;
|
|
|
|
|
2017-08-16 21:51:46 +00:00
|
|
|
graphic.lineStyle(1, this.world.defaults.velocityDebugColor, 1);
|
2017-08-16 19:08:05 +00:00
|
|
|
graphic.lineBetween(x, y, x + this.vel.x, y + this.vel.y);
|
|
|
|
}
|
2017-08-16 16:15:35 +00:00
|
|
|
}
|
2017-06-21 23:47:35 +00: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-08-17 00:20:40 +00:00
|
|
|
setSize: function (x, y, width, height)
|
2017-06-21 23:47:35 +00:00
|
|
|
{
|
2017-08-17 00:20:40 +00:00
|
|
|
this.pos.x = x;
|
|
|
|
this.pos.y = y;
|
|
|
|
this.size.x = width;
|
|
|
|
this.size.y = height;
|
2017-06-21 23:47:35 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-08-15 23:30:12 +00:00
|
|
|
toJSON: function ()
|
|
|
|
{
|
|
|
|
var output = {
|
|
|
|
name: this.name,
|
|
|
|
size: { x: this.size.x, y: this.size.y },
|
|
|
|
pos: { x: this.pos.x, y: this.pos.y },
|
|
|
|
vel: { x: this.vel.x, y: this.vel.y },
|
|
|
|
accel: { x: this.accel.x, y: this.accel.y },
|
|
|
|
friction: { x: this.friction.x, y: this.friction.y },
|
|
|
|
maxVel: { x: this.maxVel.x, y: this.maxVel.y },
|
|
|
|
gravityFactor: this.gravityFactor,
|
|
|
|
bounciness: this.bounciness,
|
|
|
|
minBounceVelocity: this.minBounceVelocity,
|
|
|
|
type: this.type,
|
|
|
|
checkAgainst: this.checkAgainst,
|
|
|
|
collides: this.collides
|
|
|
|
};
|
|
|
|
|
|
|
|
return output;
|
|
|
|
},
|
|
|
|
|
|
|
|
fromJSON: function (config)
|
|
|
|
{
|
|
|
|
// TODO
|
|
|
|
},
|
|
|
|
|
2017-08-16 16:15:35 +00:00
|
|
|
// Can be overridden by user code
|
2017-06-22 15:22:21 +00:00
|
|
|
check: function (other)
|
|
|
|
{
|
|
|
|
},
|
2017-06-21 23:47:35 +00:00
|
|
|
|
2017-08-16 16:15:35 +00:00
|
|
|
// Can be overridden by user code
|
2017-06-22 15:22:21 +00:00
|
|
|
collideWith: function (other, axis)
|
|
|
|
{
|
2017-08-16 16:15:35 +00:00
|
|
|
if (this.gameObject && this.gameObject._collideCallback)
|
|
|
|
{
|
|
|
|
this.gameObject._collideCallback.call(this.gameObject._callbackScope, this, other, axis);
|
|
|
|
}
|
2017-08-16 15:30:38 +00:00
|
|
|
},
|
|
|
|
|
2017-08-16 16:15:35 +00:00
|
|
|
// Can be overridden by user code but must return a boolean
|
2017-08-16 15:30:38 +00:00
|
|
|
handleMovementTrace: function (res)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy: function ()
|
|
|
|
{
|
|
|
|
this.enabled = false;
|
|
|
|
|
|
|
|
this.world = null;
|
|
|
|
|
|
|
|
this.gameObject = null;
|
|
|
|
|
|
|
|
this.parent = null;
|
2017-06-22 15:22:21 +00:00
|
|
|
}
|
2017-06-21 23:47:35 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = Body;
|