Added Phaser.Graphics and fixed the Out of World Bounds call.

This commit is contained in:
Richard Davey
2013-09-09 17:01:59 +01:00
parent 30fc4099c6
commit 60f0e8967f
8 changed files with 669 additions and 123 deletions
+6
View File
@@ -85,4 +85,10 @@ Phaser.GameObjectFactory.prototype = {
},
graphics: function (x, y) {
return this.world.add(new Phaser.Graphics(this.game, x, y));
},
};
+154
View File
@@ -0,0 +1,154 @@
Phaser.Graphics = function (game, x, y) {
// If exists = false then the Sprite isn't updated by the core game loop or physics subsystem at all
this.exists = true;
// This is a handy little var your game can use to determine if a sprite is alive or not, it doesn't effect rendering
this.alive = true;
this.group = null;
this.name = '';
this.game = game;
PIXI.DisplayObjectContainer.call(this);
this.position.x = x;
this.position.y = y;
// Replaces the PIXI.Point with a slightly more flexible one
this.scale = new Phaser.Point(1, 1);
// Influence of camera movement upon the position
this.scrollFactor = new Phaser.Point(1, 1);
// A mini cache for storing all of the calculated values
this._cache = {
dirty: false,
// Transform cache
a00: 1, a01: 0, a02: x, a10: 0, a11: 1, a12: y, id: 1,
// The previous calculated position inc. camera x/y and scrollFactor
x: -1, y: -1,
// The actual scale values based on the worldTransform
scaleX: 1, scaleY: 1
};
this._cache.x = this.x - (this.game.world.camera.x * this.scrollFactor.x);
this._cache.y = this.y - (this.game.world.camera.y * this.scrollFactor.y);
this.renderable = true;
/**
* The alpha of the fill of this graphics object
*
* @property fillAlpha
* @type Number
*/
this.fillAlpha = 1;
/**
* The width of any lines drawn
*
* @property lineWidth
* @type Number
*/
this.lineWidth = 0;
/**
* The color of any lines drawn
*
* @property lineColor
* @type String
*/
this.lineColor = "black";
/**
* Graphics data
*
* @property graphicsData
* @type Array
* @private
*/
this.graphicsData = [];
/**
* Current path
*
* @property currentPath
* @type Object
* @private
*/
this.currentPath = {points:[]};
};
Phaser.Graphics.prototype = Phaser.Utils.extend(true, PIXI.Graphics.prototype, PIXI.DisplayObjectContainer.prototype, Phaser.Sprite.prototype);
Phaser.Graphics.prototype.constructor = Phaser.Graphics;
// Add our own custom methods
/**
* Automatically called by World.update
*/
Phaser.Graphics.prototype.update = function() {
if (!this.exists)
{
return;
}
this._cache.dirty = false;
this._cache.x = this.x - (this.game.world.camera.x * this.scrollFactor.x);
this._cache.y = this.y - (this.game.world.camera.y * this.scrollFactor.y);
if (this.position.x != this._cache.x || this.position.y != this._cache.y)
{
this.position.x = this._cache.x;
this.position.y = this._cache.y;
this._cache.dirty = true;
}
}
Object.defineProperty(Phaser.Graphics.prototype, 'angle', {
get: function() {
return Phaser.Math.radToDeg(this.rotation);
},
set: function(value) {
this.rotation = Phaser.Math.degToRad(value);
}
});
Object.defineProperty(Phaser.Graphics.prototype, 'x', {
get: function() {
return this.position.x;
},
set: function(value) {
this.position.x = value;
}
});
Object.defineProperty(Phaser.Graphics.prototype, 'y', {
get: function() {
return this.position.y;
},
set: function(value) {
this.position.y = value;
}
});
+24 -5
View File
@@ -142,6 +142,9 @@ Phaser.Sprite = function (game, x, y, key, frame) {
// Set-up the physics body
this.body = new Phaser.Physics.Arcade.Body(this);
// World bounds check
this.inWorld = Phaser.Rectangle.intersects(this.bounds, this.game.world.bounds);
this.inWorldThreshold = 0;
this._outOfBoundsFired = false;
};
@@ -280,7 +283,7 @@ Phaser.Sprite.prototype.reset = function(x, y) {
this.exists = true;
this.visible = true;
this._outOfBoundsFired = false;
}
Phaser.Sprite.prototype.updateBounds = function() {
@@ -306,11 +309,27 @@ Phaser.Sprite.prototype.updateBounds = function() {
this._cache.boundsX = this._cache.x;
this._cache.boundsY = this._cache.y;
// Check world bounds
if (this._outOfBoundsFired == false && Phaser.Rectangle.intersects(this.bounds, this.game.world.bounds, 100) == false)
if (this.inWorld == false)
{
this.events.onOutOfBounds.dispatch(this);
this._outOfBoundsFired = true;
// Sprite WAS out of the screen, is it still?
this.inWorld = Phaser.Rectangle.intersects(this.bounds, this.game.world.bounds, this.inWorldThreshold);
if (this.inWorld)
{
// It's back again, reset the OOB check
this._outOfBoundsFired = false;
}
}
else
{
// Sprite WAS in the screen, has it now left?
this.inWorld = Phaser.Rectangle.intersects(this.bounds, this.game.world.bounds, this.inWorldThreshold);
if (this.inWorld == false)
{
this.events.onOutOfBounds.dispatch(this);
this._outOfBoundsFired = true;
}
}
}