phaser/src/pixi/extras/Rope.js

174 lines
3.6 KiB
JavaScript
Raw Normal View History

2014-10-10 19:36:04 +00:00
/**
* @author Mat Groves http://matgroves.com/ @Doormat23
* @copyright Mat Groves, Rovanion Luckey
*/
2014-02-06 00:19:46 +00:00
/**
2014-10-10 19:36:04 +00:00
*
2014-02-06 00:19:46 +00:00
* @class Rope
* @constructor
2014-08-28 22:11:13 +00:00
* @extends Strip
2014-10-10 19:36:04 +00:00
* @param {Texture} texture - The texture to use on the rope.
* @param {Array} points - An array of {PIXI.Point}.
2014-02-06 00:19:46 +00:00
*/
PIXI.Rope = function(texture, points)
{
PIXI.Strip.call( this, texture );
this.points = points;
this.verticies = new PIXI.Float32Array(points.length * 4);
this.uvs = new PIXI.Float32Array(points.length * 4);
this.colors = new PIXI.Float32Array(points.length * 2);
this.indices = new PIXI.Uint16Array(points.length * 2);
2014-10-10 19:36:04 +00:00
this.refresh();
};
// constructor
PIXI.Rope.prototype = Object.create( PIXI.Strip.prototype );
PIXI.Rope.prototype.constructor = PIXI.Rope;
2014-02-06 00:19:46 +00:00
/*
2014-10-10 19:36:04 +00:00
* Refreshes
2014-02-06 00:19:46 +00:00
*
* @method refresh
*/
PIXI.Rope.prototype.refresh = function()
{
var points = this.points;
if(points.length < 1) return;
var uvs = this.uvs;
var lastPoint = points[0];
var indices = this.indices;
var colors = this.colors;
this.count-=0.2;
uvs[0] = 0;
uvs[1] = 0;
uvs[2] = 0;
uvs[3] = 1;
colors[0] = 1;
colors[1] = 1;
indices[0] = 0;
indices[1] = 1;
var total = points.length,
point, index, amount;
for (var i = 1; i < total; i++)
{
point = points[i];
index = i * 4;
// time to do some smart drawing!
amount = i / (total-1);
if(i%2)
{
uvs[index] = amount;
uvs[index+1] = 0;
uvs[index+2] = amount;
uvs[index+3] = 1;
}
else
{
uvs[index] = amount;
uvs[index+1] = 0;
uvs[index+2] = amount;
uvs[index+3] = 1;
}
index = i * 2;
colors[index] = 1;
colors[index+1] = 1;
index = i * 2;
indices[index] = index;
indices[index + 1] = index + 1;
lastPoint = point;
}
};
2014-02-06 00:19:46 +00:00
/*
* Updates the object transform for rendering
*
* @method updateTransform
* @private
*/
PIXI.Rope.prototype.updateTransform = function()
{
var points = this.points;
if(points.length < 1)return;
var lastPoint = points[0];
var nextPoint;
var perp = {x:0, y:0};
this.count-=0.2;
var verticies = this.verticies;
var total = points.length,
point, index, ratio, perpLength, num;
for (var i = 0; i < total; i++)
{
point = points[i];
index = i * 4;
if(i < points.length-1)
{
nextPoint = points[i+1];
}
else
{
nextPoint = point;
}
perp.y = -(nextPoint.x - lastPoint.x);
perp.x = nextPoint.y - lastPoint.y;
ratio = (1 - (i / (total-1))) * 10;
if(ratio > 1) ratio = 1;
perpLength = Math.sqrt(perp.x * perp.x + perp.y * perp.y);
num = this.texture.height / 2; //(20 + Math.abs(Math.sin((i + this.count) * 0.3) * 50) )* ratio;
perp.x /= perpLength;
perp.y /= perpLength;
perp.x *= num;
perp.y *= num;
verticies[index] = point.x + perp.x;
verticies[index+1] = point.y + perp.y;
verticies[index+2] = point.x - perp.x;
verticies[index+3] = point.y - perp.y;
lastPoint = point;
}
PIXI.DisplayObjectContainer.prototype.updateTransform.call( this );
};
2014-02-06 00:19:46 +00:00
/*
2014-10-10 19:36:04 +00:00
* Sets the texture that the Rope will use
2014-02-06 00:19:46 +00:00
*
* @method setTexture
* @param texture {Texture} the texture that will be used
*/
PIXI.Rope.prototype.setTexture = function(texture)
{
// stop current texture
this.texture = texture;
2014-07-03 09:49:58 +00:00
//this.updateFrame = true;
};