2014-02-06 00:19:46 +00:00
|
|
|
/* @author Mat Groves http://matgroves.com/ @Doormat23
|
2013-08-28 06:02:55 +00:00
|
|
|
*/
|
|
|
|
|
2014-02-06 00:19:46 +00:00
|
|
|
/**
|
2014-03-31 10:04:02 +00:00
|
|
|
*
|
2014-02-06 00:19:46 +00:00
|
|
|
* @class Rope
|
|
|
|
* @constructor
|
2014-02-12 01:25:36 +00:00
|
|
|
* @param texture {Texture} The texture to use
|
|
|
|
* @param points {Array}
|
2014-03-31 10:04:02 +00:00
|
|
|
*
|
2014-02-06 00:19:46 +00:00
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
PIXI.Rope = function(texture, points)
|
|
|
|
{
|
2013-12-23 04:19:52 +00:00
|
|
|
PIXI.Strip.call( this, texture );
|
|
|
|
this.points = points;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
this.verticies = new Float32Array(points.length * 4);
|
|
|
|
this.uvs = new Float32Array(points.length * 4);
|
|
|
|
this.colors = new Float32Array(points.length * 2);
|
|
|
|
this.indices = new Uint16Array(points.length * 2);
|
|
|
|
}
|
|
|
|
catch(error)
|
|
|
|
{
|
|
|
|
this.verticies = new Array(points.length * 4);
|
|
|
|
this.uvs = new Array(points.length * 4);
|
|
|
|
this.colors = new Array(points.length * 2);
|
|
|
|
this.indices = new Array(points.length * 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.refresh();
|
|
|
|
};
|
2013-08-28 06:02:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
// constructor
|
|
|
|
PIXI.Rope.prototype = Object.create( PIXI.Strip.prototype );
|
|
|
|
PIXI.Rope.prototype.constructor = PIXI.Rope;
|
|
|
|
|
2014-02-06 00:19:46 +00:00
|
|
|
/*
|
2014-03-31 10:04:02 +00:00
|
|
|
* Refreshes
|
2014-02-06 00:19:46 +00:00
|
|
|
*
|
|
|
|
* @method refresh
|
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
PIXI.Rope.prototype.refresh = function()
|
|
|
|
{
|
2013-12-23 04:19:52 +00:00
|
|
|
var points = this.points;
|
|
|
|
if(points.length < 1) return;
|
2013-11-06 04:51:23 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
var uvs = this.uvs;
|
2013-11-06 04:51:23 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
var lastPoint = points[0];
|
|
|
|
var indices = this.indices;
|
|
|
|
var colors = this.colors;
|
2013-11-06 04:51:23 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
this.count-=0.2;
|
2013-11-06 04:51:23 +00:00
|
|
|
|
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
uvs[0] = 0;
|
|
|
|
uvs[1] = 1;
|
|
|
|
uvs[2] = 0;
|
|
|
|
uvs[3] = 1;
|
2013-11-06 04:51:23 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
colors[0] = 1;
|
|
|
|
colors[1] = 1;
|
2013-11-06 04:51:23 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
indices[0] = 0;
|
|
|
|
indices[1] = 1;
|
2013-11-06 04:51:23 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
var total = points.length,
|
|
|
|
point, index, amount;
|
2013-11-06 04:51:23 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
for (var i = 1; i < total; i++)
|
|
|
|
{
|
2013-11-06 04:51:23 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
point = points[i];
|
|
|
|
index = i * 4;
|
|
|
|
// time to do some smart drawing!
|
|
|
|
amount = i / (total-1);
|
2013-11-06 04:51:23 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
if(i%2)
|
|
|
|
{
|
|
|
|
uvs[index] = amount;
|
|
|
|
uvs[index+1] = 0;
|
2013-11-06 04:51:23 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
uvs[index+2] = amount;
|
|
|
|
uvs[index+3] = 1;
|
2013-11-06 04:51:23 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
uvs[index] = amount;
|
|
|
|
uvs[index+1] = 0;
|
2013-11-06 04:51:23 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
uvs[index+2] = amount;
|
|
|
|
uvs[index+3] = 1;
|
|
|
|
}
|
2013-11-06 04:51:23 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
index = i * 2;
|
|
|
|
colors[index] = 1;
|
|
|
|
colors[index+1] = 1;
|
2013-11-06 04:51:23 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
index = i * 2;
|
|
|
|
indices[index] = index;
|
|
|
|
indices[index + 1] = index + 1;
|
2013-11-06 04:51:23 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
lastPoint = point;
|
|
|
|
}
|
|
|
|
};
|
2013-08-28 06:02:55 +00:00
|
|
|
|
2014-02-06 00:19:46 +00:00
|
|
|
/*
|
|
|
|
* Updates the object transform for rendering
|
|
|
|
*
|
|
|
|
* @method updateTransform
|
|
|
|
* @private
|
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
PIXI.Rope.prototype.updateTransform = function()
|
|
|
|
{
|
2013-11-06 04:51:23 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
var points = this.points;
|
|
|
|
if(points.length < 1)return;
|
2013-11-06 04:51:23 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
var lastPoint = points[0];
|
|
|
|
var nextPoint;
|
|
|
|
var perp = {x:0, y:0};
|
2013-11-06 04:51:23 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
this.count-=0.2;
|
2013-11-06 04:51:23 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
var verticies = this.verticies;
|
|
|
|
verticies[0] = lastPoint.x + perp.x;
|
|
|
|
verticies[1] = lastPoint.y + perp.y; //+ 200
|
|
|
|
verticies[2] = lastPoint.x - perp.x;
|
|
|
|
verticies[3] = lastPoint.y - perp.y;//+200
|
|
|
|
// time to do some smart drawing!
|
2013-11-06 04:51:23 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
var total = points.length,
|
|
|
|
point, index, ratio, perpLength, num;
|
2013-11-06 04:51:23 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
for (var i = 1; i < total; i++)
|
|
|
|
{
|
|
|
|
point = points[i];
|
|
|
|
index = i * 4;
|
2013-11-06 04:51:23 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
if(i < points.length-1)
|
|
|
|
{
|
|
|
|
nextPoint = points[i+1];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nextPoint = point;
|
|
|
|
}
|
2013-11-06 04:51:23 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
perp.y = -(nextPoint.x - lastPoint.x);
|
|
|
|
perp.x = nextPoint.y - lastPoint.y;
|
2013-11-06 04:51:23 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
ratio = (1 - (i / (total-1))) * 10;
|
2013-11-06 04:51:23 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
if(ratio > 1) ratio = 1;
|
2013-11-06 04:51:23 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
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;
|
2013-11-06 04:51:23 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
perp.x *= num;
|
|
|
|
perp.y *= num;
|
2013-11-06 04:51:23 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
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;
|
2013-08-28 06:02:55 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
lastPoint = point;
|
|
|
|
}
|
2013-11-06 04:51:23 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
PIXI.DisplayObjectContainer.prototype.updateTransform.call( this );
|
|
|
|
};
|
2014-02-06 00:19:46 +00:00
|
|
|
/*
|
2014-03-31 10:04:02 +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
|
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
PIXI.Rope.prototype.setTexture = function(texture)
|
|
|
|
{
|
2013-12-23 04:19:52 +00:00
|
|
|
// stop current texture
|
|
|
|
this.texture = texture;
|
|
|
|
this.updateFrame = true;
|
|
|
|
};
|