2014-07-15 16:40:40 +00:00
|
|
|
/**
|
2014-10-15 21:05:38 +00:00
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2015-02-25 03:36:23 +00:00
|
|
|
* @copyright 2015 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 can be scrolled and scaled and will automatically wrap on the edges as it does so.
|
|
|
|
* Please note that Ropes, as with normal Sprites, have no input handler or physics bodies by default. Both need enabling.
|
2014-10-19 23:54:56 +00:00
|
|
|
* Example usage: https://github.com/codevinsky/phaser-rope-demo/blob/master/dist/demo.js
|
2014-07-15 16:40:40 +00:00
|
|
|
*
|
|
|
|
* @class Phaser.Rope
|
|
|
|
* @constructor
|
2014-10-19 23:54:56 +00:00
|
|
|
* @extends PIXI.Rope
|
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.
|
2014-10-10 13:47:58 +00:00
|
|
|
* @param {Array} points - An array of {Phaser.Point}.
|
2014-07-15 16:40:40 +00:00
|
|
|
*/
|
2014-07-15 19:01:15 +00:00
|
|
|
Phaser.Rope = function (game, x, y, key, frame, points) {
|
2014-07-15 16:40:40 +00:00
|
|
|
|
|
|
|
this.points = [];
|
2014-07-15 19:01:15 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Point} _scroll - Internal cache var.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._scroll = new Phaser.Point();
|
|
|
|
|
2014-07-15 19:24:47 +00:00
|
|
|
PIXI.Rope.call(this, key, this.points);
|
2014-07-15 16:40:40 +00:00
|
|
|
|
2015-02-17 06:00:41 +00:00
|
|
|
Phaser.Utils.mixinPrototype(this, Phaser.Component.Core.prototype);
|
|
|
|
|
|
|
|
var components = [
|
|
|
|
'Angle',
|
|
|
|
'Animation',
|
|
|
|
'AutoCull',
|
|
|
|
'Bounds',
|
|
|
|
'BringToTop',
|
|
|
|
'Crop',
|
|
|
|
'Delta',
|
|
|
|
'Destroy',
|
|
|
|
'FixedToCamera',
|
|
|
|
'InputEnabled',
|
|
|
|
'InWorld',
|
|
|
|
'LifeSpan',
|
|
|
|
'LoadTexture',
|
|
|
|
'Overlap',
|
|
|
|
'PhysicsBody',
|
|
|
|
'Reset',
|
|
|
|
'ScaleMinMax',
|
|
|
|
'Smoothed'
|
|
|
|
];
|
|
|
|
|
|
|
|
Phaser.Component.Core.install.call(this, components);
|
|
|
|
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-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-16 17:22:51 +00:00
|
|
|
|
2014-07-15 16:40:40 +00:00
|
|
|
if (this._scroll.x !== 0)
|
|
|
|
{
|
|
|
|
this.tilePosition.x += this._scroll.x * this.game.time.physicsElapsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._scroll.y !== 0)
|
|
|
|
{
|
|
|
|
this.tilePosition.y += this._scroll.y * this.game.time.physicsElapsed;
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
|
2015-02-17 06:00:41 +00:00
|
|
|
if (this._hasUpdateAnimation)
|
2014-07-15 16:40:40 +00:00
|
|
|
{
|
2015-02-17 06:00:41 +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, resets the tilePosition and then
|
|
|
|
* 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.
|
|
|
|
* @return (Phaser.Rope) This instance.
|
|
|
|
*/
|
|
|
|
Phaser.Rope.prototype.reset = function(x, y) {
|
|
|
|
|
2015-02-17 06:00:41 +00:00
|
|
|
Phaser.Component.Reset.prototype.reset.call(this, x, y);
|
2014-07-15 16:40:40 +00:00
|
|
|
|
|
|
|
this.tilePosition.x = 0;
|
|
|
|
this.tilePosition.y = 0;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-07-15 19:01:15 +00:00
|
|
|
/**
|
|
|
|
* A Rope will call it's 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) {
|
2015-02-16 17:22:51 +00:00
|
|
|
|
|
|
|
if (value && typeof value === 'function')
|
|
|
|
{
|
2014-07-15 19:01:15 +00:00
|
|
|
this._hasUpdateAnimation = true;
|
|
|
|
this._updateAnimation = value;
|
2015-02-16 17:22:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-15 19:01:15 +00:00
|
|
|
this._hasUpdateAnimation = false;
|
|
|
|
this._updateAnimation = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The segments that make up the rope body as an array of Phaser.Rectangles
|
|
|
|
*
|
|
|
|
* @name Phaser.Rope#segments
|
2014-11-30 11:10:52 +00:00
|
|
|
* @property {Phaser.Rectangles[]} updateAnimation - Returns an array of Phaser.Rectangles that represent the segments of the given rope
|
2014-07-15 19:01:15 +00:00
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Rope.prototype, "segments", {
|
2015-02-16 17:22:51 +00:00
|
|
|
|
2014-07-15 19:01:15 +00:00
|
|
|
get: function() {
|
2015-02-16 17:22:51 +00:00
|
|
|
|
2014-07-15 19:01:15 +00:00
|
|
|
var segments = [];
|
|
|
|
var index, x1, y1, x2, y2, width, height, rect;
|
2015-02-16 17:22:51 +00:00
|
|
|
|
|
|
|
for (var i = 0; i < this.points.length; i++)
|
|
|
|
{
|
2014-07-15 19:01:15 +00:00
|
|
|
index = i * 4;
|
2015-02-16 17:22:51 +00:00
|
|
|
|
2014-07-15 19:01:15 +00:00
|
|
|
x1 = this.verticies[index];
|
|
|
|
y1 = this.verticies[index + 1];
|
|
|
|
x2 = this.verticies[index + 4];
|
|
|
|
y2 = this.verticies[index + 3];
|
2015-02-16 17:22:51 +00:00
|
|
|
|
2014-07-15 19:01:15 +00:00
|
|
|
width = Phaser.Math.difference(x1,x2);
|
|
|
|
height = Phaser.Math.difference(y1,y2);
|
2015-02-16 17:22:51 +00:00
|
|
|
|
2014-07-15 19:01:15 +00:00
|
|
|
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
|
|
|
|
2015-02-16 17:22:51 +00:00
|
|
|
return segments;
|
2014-07-15 16:40:40 +00:00
|
|
|
}
|
|
|
|
});
|