2013-05-28 20:38:37 +00:00
|
|
|
/// <reference path="../../core/Vec2.ts" />
|
|
|
|
/// <reference path="../../core/Point.ts" />
|
2013-05-30 04:34:35 +00:00
|
|
|
/// <reference path="../../math/Vec2Utils.ts" />
|
2013-05-29 20:39:08 +00:00
|
|
|
/// <reference path="../../physics/AABB.ts" />
|
2013-05-28 20:38:37 +00:00
|
|
|
|
2013-05-28 08:37:32 +00:00
|
|
|
/**
|
|
|
|
* Phaser - Components - Physics
|
|
|
|
*/
|
|
|
|
|
|
|
|
module Phaser.Components {
|
|
|
|
|
|
|
|
export class Physics {
|
|
|
|
|
2013-05-29 20:39:08 +00:00
|
|
|
constructor(parent: Sprite) {
|
|
|
|
|
2013-05-30 04:34:35 +00:00
|
|
|
this.game = parent.game;
|
2013-05-29 20:39:08 +00:00
|
|
|
this._sprite = parent;
|
|
|
|
|
2013-05-30 04:34:35 +00:00
|
|
|
this.gravity = Vec2Utils.clone(this.game.world.physics.gravity);
|
|
|
|
this.drag = Vec2Utils.clone(this.game.world.physics.drag);
|
|
|
|
this.bounce = Vec2Utils.clone(this.game.world.physics.bounce);
|
|
|
|
this.friction = Vec2Utils.clone(this.game.world.physics.friction);
|
|
|
|
|
2013-05-30 02:54:51 +00:00
|
|
|
this.velocity = new Vec2;
|
|
|
|
this.acceleration = new Vec2;
|
2013-05-30 00:09:27 +00:00
|
|
|
|
2013-05-30 04:34:35 +00:00
|
|
|
this.shape = this.game.world.physics.add(new Phaser.Physics.AABB(this.game, this._sprite, this._sprite.x, this._sprite.y, this._sprite.width, this._sprite.height));
|
2013-05-29 20:39:08 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private _sprite: Sprite;
|
|
|
|
|
2013-05-30 04:34:35 +00:00
|
|
|
public game: Game;
|
2013-05-30 02:54:51 +00:00
|
|
|
public shape: Phaser.Physics.IPhysicsShape;
|
2013-05-29 20:39:08 +00:00
|
|
|
|
2013-05-28 08:37:32 +00:00
|
|
|
/**
|
|
|
|
* Whether this object will be moved by impacts with other objects or not.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
public immovable: bool;
|
|
|
|
|
|
|
|
/**
|
2013-05-30 00:09:27 +00:00
|
|
|
* Set this to false if you want to skip the automatic movement stuff
|
2013-05-28 08:37:32 +00:00
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
public moves: bool = true;
|
|
|
|
|
2013-05-30 02:54:51 +00:00
|
|
|
public gravity: Vec2;
|
2013-05-30 00:09:27 +00:00
|
|
|
public drag: Vec2;
|
|
|
|
public bounce: Vec2;
|
|
|
|
public friction: Vec2;
|
|
|
|
public velocity: Vec2;
|
|
|
|
public acceleration: Vec2;
|
2013-05-28 20:38:37 +00:00
|
|
|
|
2013-05-28 08:37:32 +00:00
|
|
|
/**
|
|
|
|
* Internal function for updating the position and speed of this object.
|
|
|
|
*/
|
|
|
|
public update() {
|
|
|
|
|
2013-05-29 20:39:08 +00:00
|
|
|
if (this.moves)
|
|
|
|
{
|
2013-05-30 02:54:51 +00:00
|
|
|
this._sprite.x = (this.shape.position.x - this.shape.bounds.halfWidth) - this.shape.offset.x;
|
|
|
|
this._sprite.y = (this.shape.position.y - this.shape.bounds.halfHeight) - this.shape.offset.y;
|
2013-05-29 20:39:08 +00:00
|
|
|
}
|
2013-05-28 20:38:37 +00:00
|
|
|
}
|
2013-05-28 08:37:32 +00:00
|
|
|
|
2013-05-28 20:38:37 +00:00
|
|
|
/**
|
2013-05-30 00:09:27 +00:00
|
|
|
* Render debug infos. (including name, bounds info, position and some other properties)
|
|
|
|
* @param x {number} X position of the debug info to be rendered.
|
|
|
|
* @param y {number} Y position of the debug info to be rendered.
|
|
|
|
* @param [color] {number} color of the debug info to be rendered. (format is css color string)
|
|
|
|
*/
|
|
|
|
public renderDebugInfo(x: number, y: number, color?: string = 'rgb(255,255,255)') {
|
|
|
|
|
|
|
|
this._sprite.texture.context.fillStyle = color;
|
|
|
|
this._sprite.texture.context.fillText('Sprite: (' + this._sprite.frameBounds.width + ' x ' + this._sprite.frameBounds.height + ')', x, y);
|
|
|
|
this._sprite.texture.context.fillText('x: ' + this._sprite.frameBounds.x.toFixed(1) + ' y: ' + this._sprite.frameBounds.y.toFixed(1) + ' rotation: ' + this._sprite.rotation.toFixed(1), x, y + 14);
|
|
|
|
this._sprite.texture.context.fillText('vx: ' + this.velocity.x.toFixed(1) + ' vy: ' + this.velocity.y.toFixed(1), x, y + 28);
|
|
|
|
this._sprite.texture.context.fillText('ax: ' + this.acceleration.x.toFixed(1) + ' ay: ' + this.acceleration.y.toFixed(1), x, y + 42);
|
2013-05-28 20:38:37 +00:00
|
|
|
|
|
|
|
}
|
2013-05-28 08:37:32 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|