phaser/src/gameobjects/Rope.js

210 lines
6 KiB
JavaScript
Raw Normal View History

2014-07-15 16:40:40 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
2016-04-04 21:15:01 +00:00
* @copyright 2016 Photon Storm Ltd, Richard Davey
2014-07-15 16:40:40 +00:00
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* A Rope is a Sprite that has a repeating texture.
*
* The texture will automatically wrap on the edges as it moves.
*
* Please note that Ropes cannot have an input handler.
2014-07-15 16:40:40 +00:00
*
* @class Phaser.Rope
* @constructor
* @extends PIXI.Rope
* @extends Phaser.Component.Core
* @extends Phaser.Component.Angle
* @extends Phaser.Component.Animation
* @extends Phaser.Component.AutoCull
* @extends Phaser.Component.Bounds
* @extends Phaser.Component.BringToTop
* @extends Phaser.Component.Crop
* @extends Phaser.Component.Delta
* @extends Phaser.Component.Destroy
* @extends Phaser.Component.FixedToCamera
* @extends Phaser.Component.InWorld
* @extends Phaser.Component.LifeSpan
* @extends Phaser.Component.LoadTexture
* @extends Phaser.Component.Overlap
* @extends Phaser.Component.PhysicsBody
* @extends Phaser.Component.Reset
* @extends Phaser.Component.ScaleMinMax
* @extends Phaser.Component.Smoothed
2014-07-15 16:40:40 +00:00
* @param {Phaser.Game} game - A reference to the currently running game.
* @param {number} x - The x coordinate (in world space) to position the Rope at.
* @param {number} y - The y coordinate (in world space) to position the Rope at.
* @param {string|Phaser.RenderTexture|Phaser.BitmapData|PIXI.Texture} key - This is the image or texture used by the Rope during rendering. It can be a string which is a reference to the Cache entry, or an instance of a RenderTexture or PIXI.Texture.
* @param {string|number} frame - If this Rope is using part of a sprite sheet or texture atlas you can specify the exact frame to use by giving a string or numeric index.
* @param {Array} points - An array of {Phaser.Point}.
2014-07-15 16:40:40 +00:00
*/
Phaser.Rope = function (game, x, y, key, frame, points) {
2014-07-15 16:40:40 +00:00
this.points = [];
this.points = points;
this._hasUpdateAnimation = false;
this._updateAnimationCallback = null;
2014-07-15 16:40:40 +00:00
x = x || 0;
y = y || 0;
key = key || null;
frame = frame || null;
/**
* @property {number} type - The const type of this object.
* @readonly
*/
this.type = Phaser.ROPE;
* PIXI.Texture.fromImage, PIXI.BaseTexture.fromImage and PIXI.Sprite.fromImage have all been removed. They should never have actually been used, as they bypass the Phaser Loader, and don't factor in CORs or any other advanced loader settings. * The PIXI.BaseTexture.imageUrl property has been removed, as it was never actually populated. * The PIXI.BaseTexture._UID property has been removed, as it was never actually used internally. * All references to PIXI.BaseTextureCache have been removed (primarily from BaseTexture.destroy and Texture.destroy), as the BaseTextureCache was never used internally by Phaser, or by our custom version of Pixi. * PIXI.TextureCache has been removed. It was only ever used by the __default and __missing images that Phaser generates on start-up. It wasn't used internally by Phaser anywhere else, and the only references Pixi has to it have all been removed. If you need it in your own game, please refactor it to avoid it, or re-create the object on the PIXI global object. * Canvases created by `BaseTexture.fromCanvas` no longer have the `_pixiId` property attached to them, as this was never used internally by Phaser or Pixi. * PIXI.BaseTexture.updateSourceImage is now deprecated. Please use `Sprite.loadTexture` instead. * The property PIXI.BaseTextureCacheIdGenerator has been removed, as it is no longer used internally by Phaser or Pixi. * PIXI.Texture.addTextureToCache has been removed. The PIXI Texture Cache was never actually used by Phaser, and was leading to complications internally. * PIXI.Texture.removeTextureFromCache has been removed. The PIXI Texture Cache was never actually used by Phaser, and was leading to complications internally. * PIXI.Texture.fromFrame and PIXI.Sprite.fromFrame have been removed. They relied on the PIXI Texture Cache, which was never actually used by Phaser, and was never used internally by Pixi either. * The property PIXI.TextureCacheIdGenerator has been removed, as it was not used internally. * The property PIXI.FrameCache has been removed, as it was not used internally.
2016-07-06 20:47:27 +00:00
PIXI.Rope.call(this, Phaser.Cache.DEFAULT, this.points);
2014-07-15 16:40:40 +00:00
Phaser.Component.Core.init.call(this, game, x, y, key, frame);
2014-07-15 16:40:40 +00:00
};
Phaser.Rope.prototype = Object.create(PIXI.Rope.prototype);
Phaser.Rope.prototype.constructor = Phaser.Rope;
2015-03-23 19:19:07 +00:00
Phaser.Component.Core.install.call(Phaser.Rope.prototype, [
'Angle',
'Animation',
'AutoCull',
'Bounds',
'BringToTop',
'Crop',
'Delta',
'Destroy',
'FixedToCamera',
'InWorld',
'LifeSpan',
'LoadTexture',
'Overlap',
'PhysicsBody',
'Reset',
'ScaleMinMax',
'Smoothed'
2015-03-23 19:19:07 +00:00
]);
2015-02-25 02:49:50 +00:00
Phaser.Rope.prototype.preUpdatePhysics = Phaser.Component.PhysicsBody.preUpdate;
Phaser.Rope.prototype.preUpdateLifeSpan = Phaser.Component.LifeSpan.preUpdate;
Phaser.Rope.prototype.preUpdateInWorld = Phaser.Component.InWorld.preUpdate;
Phaser.Rope.prototype.preUpdateCore = Phaser.Component.Core.preUpdate;
2014-07-15 16:40:40 +00:00
/**
* Automatically called by World.preUpdate.
*
* @method Phaser.Rope#preUpdate
* @memberof Phaser.Rope
*/
Phaser.Rope.prototype.preUpdate = function() {
2015-02-25 02:49:50 +00:00
if (!this.preUpdatePhysics() || !this.preUpdateLifeSpan() || !this.preUpdateInWorld())
{
return false;
}
2014-07-15 16:40:40 +00:00
2015-02-25 02:49:50 +00:00
return this.preUpdateCore();
2014-07-15 16:40:40 +00:00
};
/**
* Override and use this function in your own custom objects to handle any update requirements you may have.
*
* @method Phaser.Rope#update
* @memberof Phaser.Rope
*/
Phaser.Rope.prototype.update = function() {
if (this._hasUpdateAnimation)
2014-07-15 16:40:40 +00:00
{
this.updateAnimation.call(this);
2014-07-15 16:40:40 +00:00
}
};
/**
* Resets the Rope. This places the Rope at the given x/y world coordinates and then
2014-07-15 16:40:40 +00:00
* sets alive, exists, visible and renderable all to true. Also resets the outOfBounds state.
* If the Rope has a physics body that too is reset.
*
* @method Phaser.Rope#reset
* @memberof Phaser.Rope
* @param {number} x - The x coordinate (in world space) to position the Sprite at.
* @param {number} y - The y coordinate (in world space) to position the Sprite at.
2015-10-27 08:10:14 +00:00
* @return {Phaser.Rope} This instance.
2014-07-15 16:40:40 +00:00
*/
Phaser.Rope.prototype.reset = function(x, y) {
Phaser.Component.Reset.prototype.reset.call(this, x, y);
2014-07-15 16:40:40 +00:00
return this;
};
/**
* A Rope will call its updateAnimation function on each update loop if it has one.
*
* @name Phaser.Rope#updateAnimation
* @property {function} updateAnimation - Set to a function if you'd like the rope to animate during the update phase. Set to false or null to remove it.
*/
Object.defineProperty(Phaser.Rope.prototype, "updateAnimation", {
get: function () {
return this._updateAnimation;
},
set: function (value) {
if (value && typeof value === 'function')
{
this._hasUpdateAnimation = true;
this._updateAnimation = value;
}
else
{
this._hasUpdateAnimation = false;
this._updateAnimation = null;
}
}
});
/**
* The segments that make up the rope body as an array of Phaser.Rectangles
*
* @name Phaser.Rope#segments
* @property {Phaser.Rectangles[]} updateAnimation - Returns an array of Phaser.Rectangles that represent the segments of the given rope
*/
Object.defineProperty(Phaser.Rope.prototype, "segments", {
get: function() {
var segments = [];
var index, x1, y1, x2, y2, width, height, rect;
for (var i = 0; i < this.points.length; i++)
{
index = i * 4;
x1 = this.vertices[index] * this.scale.x;
y1 = this.vertices[index + 1] * this.scale.y;
x2 = this.vertices[index + 4] * this.scale.x;
y2 = this.vertices[index + 3] * this.scale.y;
width = Phaser.Math.difference(x1, x2);
height = Phaser.Math.difference(y1, y2);
x1 += this.world.x;
y1 += this.world.y;
rect = new Phaser.Rectangle(x1, y1, width, height);
segments.push(rect);
}
2014-07-15 16:40:40 +00:00
return segments;
2014-07-15 16:40:40 +00:00
}
2014-07-15 16:40:40 +00:00
});