2015-03-23 23:27:14 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2015 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2015-03-01 07:00:17 +00:00
|
|
|
/**
|
|
|
|
* Core Component Features.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
*/
|
2015-02-17 05:15:04 +00:00
|
|
|
Phaser.Component.Core = function () {};
|
|
|
|
|
2015-02-23 04:44:11 +00:00
|
|
|
/**
|
|
|
|
* Installs / registers mixin components.
|
|
|
|
*
|
|
|
|
* The `this` context should be that of the applicable object instance or prototype.
|
|
|
|
*
|
2015-03-23 23:27:14 +00:00
|
|
|
* @method
|
2015-02-23 04:44:11 +00:00
|
|
|
* @protected
|
|
|
|
*/
|
2015-02-17 05:15:04 +00:00
|
|
|
Phaser.Component.Core.install = function (components) {
|
|
|
|
|
2015-02-23 04:44:11 +00:00
|
|
|
// Always install 'Core' first
|
|
|
|
Phaser.Utils.mixinPrototype(this, Phaser.Component.Core.prototype);
|
|
|
|
|
2015-02-17 05:15:04 +00:00
|
|
|
this.components = {};
|
|
|
|
|
|
|
|
for (var i = 0; i < components.length; i++)
|
|
|
|
{
|
|
|
|
var id = components[i];
|
2015-03-11 01:44:46 +00:00
|
|
|
var replace = false;
|
2015-02-17 05:15:04 +00:00
|
|
|
|
2015-03-11 01:44:46 +00:00
|
|
|
if (id === 'Destroy')
|
|
|
|
{
|
|
|
|
replace = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Phaser.Utils.mixinPrototype(this, Phaser.Component[id].prototype, replace);
|
2015-02-17 05:15:04 +00:00
|
|
|
|
|
|
|
this.components[id] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2015-02-23 04:44:11 +00:00
|
|
|
/**
|
|
|
|
* Initializes the mixin components.
|
|
|
|
*
|
|
|
|
* The `this` context should be an instance of the component mixin target.
|
|
|
|
*
|
2015-03-23 23:27:14 +00:00
|
|
|
* @method
|
2015-02-23 04:44:11 +00:00
|
|
|
* @protected
|
|
|
|
*/
|
2015-02-17 05:15:04 +00:00
|
|
|
Phaser.Component.Core.init = function (game, x, y, key, frame) {
|
|
|
|
|
|
|
|
this.game = game;
|
|
|
|
|
|
|
|
this.key = key;
|
|
|
|
|
|
|
|
this.position.set(x, y);
|
|
|
|
this.world = new Phaser.Point(x, y);
|
|
|
|
this.previousPosition = new Phaser.Point(x, y);
|
|
|
|
|
|
|
|
this.events = new Phaser.Events(this);
|
|
|
|
|
|
|
|
this._bounds = new Phaser.Rectangle();
|
|
|
|
|
2015-03-01 01:49:45 +00:00
|
|
|
if (this.components.PhysicsBody)
|
|
|
|
{
|
|
|
|
// Enable-body checks for hasOwnProperty; makes sure to lift property from prototype.
|
|
|
|
this.body = this.body;
|
|
|
|
}
|
|
|
|
|
2015-02-17 05:15:04 +00:00
|
|
|
if (this.components.Animation)
|
|
|
|
{
|
|
|
|
this.animations = new Phaser.AnimationManager(this);
|
|
|
|
}
|
|
|
|
|
2015-02-17 15:48:54 +00:00
|
|
|
if (this.components.LoadTexture && key !== null)
|
2015-02-17 05:15:04 +00:00
|
|
|
{
|
|
|
|
this.loadTexture(key, frame);
|
|
|
|
}
|
|
|
|
|
2015-03-24 21:35:09 +00:00
|
|
|
if (this.components.FixedToCamera)
|
|
|
|
{
|
|
|
|
this.cameraOffset = new Phaser.Point(x, y);
|
|
|
|
}
|
|
|
|
|
2015-02-17 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.Component.Core.preUpdate = function () {
|
|
|
|
|
|
|
|
this.previousPosition.set(this.world.x, this.world.y);
|
|
|
|
this.previousRotation = this.rotation;
|
|
|
|
|
|
|
|
if (!this.exists || !this.parent.exists)
|
|
|
|
{
|
|
|
|
this.renderOrderID = -1;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.world.setTo(this.game.camera.x + this.worldTransform.tx, this.game.camera.y + this.worldTransform.ty);
|
|
|
|
|
|
|
|
if (this.visible)
|
|
|
|
{
|
|
|
|
this.renderOrderID = this.game.stage.currentRenderOrderID++;
|
|
|
|
}
|
|
|
|
|
2015-05-12 12:01:03 +00:00
|
|
|
if (this.texture)
|
|
|
|
{
|
|
|
|
this.texture.requiresReTint = false;
|
|
|
|
}
|
2015-05-08 03:12:20 +00:00
|
|
|
|
2015-02-17 05:15:04 +00:00
|
|
|
if (this.animations)
|
|
|
|
{
|
|
|
|
this.animations.update();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.body)
|
|
|
|
{
|
|
|
|
this.body.preUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < this.children.length; i++)
|
|
|
|
{
|
|
|
|
this.children[i].preUpdate();
|
|
|
|
}
|
|
|
|
|
2015-02-25 00:59:27 +00:00
|
|
|
return true;
|
|
|
|
|
2015-02-17 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.Component.Core.prototype = {
|
|
|
|
|
|
|
|
/**
|
2015-03-23 23:27:14 +00:00
|
|
|
* A reference to the currently running Game.
|
|
|
|
* @property {Phaser.Game} game
|
2015-02-17 05:15:04 +00:00
|
|
|
*/
|
|
|
|
game: null,
|
|
|
|
|
|
|
|
/**
|
2015-03-23 23:27:14 +00:00
|
|
|
* A user defined name given to this Game Object.
|
|
|
|
* This value isn't ever used internally by Phaser, it is meant as a game level property.
|
|
|
|
* @property {string} name
|
2015-02-17 05:15:04 +00:00
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
name: '',
|
|
|
|
|
|
|
|
/**
|
2015-03-23 23:27:14 +00:00
|
|
|
* The components this Game Object has installed.
|
|
|
|
* @property {object} components
|
2015-02-17 05:15:04 +00:00
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
components: {},
|
|
|
|
|
|
|
|
/**
|
2015-03-23 23:27:14 +00:00
|
|
|
* The z depth of this Game Object within its parent Group.
|
|
|
|
* No two objects in a Group can have the same z value.
|
|
|
|
* This value is adjusted automatically whenever the Group hierarchy changes.
|
|
|
|
* @property {number} z
|
2015-02-17 05:15:04 +00:00
|
|
|
*/
|
|
|
|
z: 0,
|
|
|
|
|
|
|
|
/**
|
2015-03-23 23:27:14 +00:00
|
|
|
* All Phaser Game Objects have an Events class which contains all of the events that are dispatched when certain things happen to this
|
|
|
|
* Game Object, or any of its components.
|
|
|
|
* @see Phaser.Events
|
|
|
|
* @property {Phaser.Events} events
|
2015-02-17 05:15:04 +00:00
|
|
|
*/
|
|
|
|
events: undefined,
|
|
|
|
|
|
|
|
/**
|
2015-03-23 23:27:14 +00:00
|
|
|
* If the Game Object is enabled for animation (such as a Phaser.Sprite) this is a reference to its AnimationManager instance.
|
|
|
|
* Through it you can create, play, pause and stop animations.
|
|
|
|
* @see Phaser.AnimationManager
|
|
|
|
* @property {Phaser.AnimationManager} animations
|
2015-02-17 05:15:04 +00:00
|
|
|
*/
|
|
|
|
animations: undefined,
|
|
|
|
|
|
|
|
/**
|
2015-03-23 23:27:14 +00:00
|
|
|
* The key of the image or texture used by this Game Object during rendering.
|
2015-05-08 03:12:20 +00:00
|
|
|
* If it is a string it's the string used to retrieve the texture from the Phaser Image Cache.
|
|
|
|
* It can also be an instance of a RenderTexture, BitmapData, Video or PIXI.Texture.
|
2015-03-23 23:27:14 +00:00
|
|
|
* If a Game Object is created without a key it is automatically assigned the key `__default` which is a 32x32 transparent PNG stored within the Cache.
|
2015-05-08 03:12:20 +00:00
|
|
|
* If a Game Object is given a key which doesn't exist in the Image Cache it is re-assigned the key `__missing` which is a 32x32 PNG of a green box with a line through it.
|
|
|
|
* @property {string|Phaser.RenderTexture|Phaser.BitmapData|Phaser.Video|PIXI.Texture} key
|
2015-02-17 05:15:04 +00:00
|
|
|
*/
|
|
|
|
key: '',
|
|
|
|
|
|
|
|
/**
|
2015-03-23 23:27:14 +00:00
|
|
|
* The world coordinates of this Game Object in pixels.
|
|
|
|
* Depending on where in the display list this Game Object is placed this value can differ from `position`,
|
|
|
|
* which contains the x/y coordinates relative to the Game Objects parent.
|
|
|
|
* @property {Phaser.Point} world
|
2015-02-17 05:15:04 +00:00
|
|
|
*/
|
|
|
|
world: null,
|
|
|
|
|
|
|
|
/**
|
2015-03-23 23:27:14 +00:00
|
|
|
* A debug flag designed for use with `Game.enableStep`.
|
|
|
|
* @property {boolean} debug
|
2015-02-17 05:15:04 +00:00
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
debug: false,
|
|
|
|
|
|
|
|
/**
|
2015-03-23 23:27:14 +00:00
|
|
|
* The position the Game Object was located in the previous frame.
|
|
|
|
* @property {Phaser.Point} previousPosition
|
2015-02-17 05:15:04 +00:00
|
|
|
* @readOnly
|
|
|
|
*/
|
|
|
|
previousPosition: null,
|
|
|
|
|
|
|
|
/**
|
2015-03-23 23:27:14 +00:00
|
|
|
* The rotation the Game Object was in set to in the previous frame. Value is in radians.
|
|
|
|
* @property {number} previousRotation
|
2015-02-17 05:15:04 +00:00
|
|
|
* @readOnly
|
|
|
|
*/
|
|
|
|
previousRotation: 0,
|
|
|
|
|
|
|
|
/**
|
2015-03-23 23:27:14 +00:00
|
|
|
* The render order ID is used internally by the renderer and Input Manager and should not be modified.
|
|
|
|
* This property is mostly used internally by the renderers, but is exposed for the use of plugins.
|
|
|
|
* @property {number} renderOrderID
|
2015-02-17 05:15:04 +00:00
|
|
|
* @readOnly
|
|
|
|
*/
|
|
|
|
renderOrderID: 0,
|
|
|
|
|
|
|
|
/**
|
2015-03-23 23:27:14 +00:00
|
|
|
* A Game Object is considered `fresh` if it has just been created or reset and is yet to receive a renderer transform update.
|
|
|
|
* This property is mostly used internally by the physics systems, but is exposed for the use of plugins.
|
|
|
|
* @property {boolean} fresh
|
2015-02-17 05:15:04 +00:00
|
|
|
* @readOnly
|
|
|
|
*/
|
|
|
|
fresh: true,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Rectangle} _bounds - Internal cache var.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_bounds: null,
|
|
|
|
|
2015-02-24 22:57:59 +00:00
|
|
|
/**
|
|
|
|
* @property {boolean} _exists - Internal cache var.
|
|
|
|
* @private
|
|
|
|
*/
|
2015-02-25 02:49:19 +00:00
|
|
|
_exists: true,
|
2015-02-24 22:57:59 +00:00
|
|
|
|
2015-02-17 05:15:04 +00:00
|
|
|
/**
|
2015-03-23 23:27:14 +00:00
|
|
|
* Controls if this Game Object is processed by the core game loop.
|
|
|
|
* If this Game Object has a physics body it also controls if its physics body is updated or not.
|
|
|
|
* When `exists` is set to `false` it will remove its physics body from the physics world if it has one.
|
|
|
|
* It also toggles the `visible` property to false as well.
|
2015-02-17 05:15:04 +00:00
|
|
|
*
|
2015-03-23 23:27:14 +00:00
|
|
|
* Setting `exists` to true will add its physics body back in to the physics world, if it has one.
|
|
|
|
* It will also set the `visible` property to `true`.
|
2015-02-24 22:57:59 +00:00
|
|
|
*
|
2015-03-23 23:27:14 +00:00
|
|
|
* @property {boolean} exists
|
2015-02-24 22:57:59 +00:00
|
|
|
*/
|
|
|
|
exists: {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
|
|
|
|
return this._exists;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value) {
|
|
|
|
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
this._exists = true;
|
|
|
|
|
|
|
|
if (this.body && this.body.type === Phaser.Physics.P2JS)
|
|
|
|
{
|
|
|
|
this.body.addToWorld();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.visible = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this._exists = false;
|
|
|
|
|
|
|
|
if (this.body && this.body.type === Phaser.Physics.P2JS)
|
|
|
|
{
|
|
|
|
this.body.removeFromWorld();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.visible = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-03-01 07:00:17 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2015-03-23 23:27:14 +00:00
|
|
|
* Override this method in your own custom objects to handle any update requirements.
|
|
|
|
* It is called immediately after `preUpdate` and before `postUpdate`.
|
|
|
|
* Remember if this Game Object has any children you should call update on those too.
|
2015-03-01 07:00:17 +00:00
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
update: function() {
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2015-03-23 23:27:14 +00:00
|
|
|
* Internal method called by the World postUpdate cycle.
|
2015-03-01 07:00:17 +00:00
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
postUpdate: function() {
|
|
|
|
|
2015-05-21 14:24:39 +00:00
|
|
|
if (this.customRender)
|
2015-03-01 07:00:17 +00:00
|
|
|
{
|
|
|
|
this.key.render();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.components.PhysicsBody)
|
|
|
|
{
|
|
|
|
Phaser.Component.PhysicsBody.postUpdate.call(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.components.FixedToCamera)
|
|
|
|
{
|
|
|
|
Phaser.Component.FixedToCamera.postUpdate.call(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < this.children.length; i++)
|
|
|
|
{
|
|
|
|
this.children[i].postUpdate();
|
|
|
|
}
|
|
|
|
|
2015-02-17 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|