Skip to content

Commit f883cb4

Browse files
committed
Fixed issue with not enough Rope segments being provided.
Allow to pass in integer to split Rope into.
1 parent 78d009c commit f883cb4

2 files changed

Lines changed: 53 additions & 20 deletions

File tree

src/gameobjects/rope/Rope.js

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ var Vector2 = require('../../math/Vector2');
3737
* @param {number} y - The vertical position of this Game Object in the world.
3838
* @param {string} texture - The key of the Texture this Game Object will use to render with, as stored in the Texture Manager.
3939
* @param {(string|integer|null)} [frame] - An optional frame from the Texture this Game Object is rendering with.
40-
* @param {Phaser.Types.Math.Vector2Like[]} [points] - An array containing the vertices data for this Rope. If none is provided a simple quad is created. See `setPoints` to set this post-creation.
40+
* @param {(integer|Phaser.Types.Math.Vector2Like[])} [points] - An array containing the vertices data for this Rope, or a number that indicates how many segments to split the texture frame into. If none is provided a simple quad is created. See `setPoints` to set this post-creation.
4141
* @param {number[]} [colors] - An optional array containing the color data for this Rope. You should provide one color value per pair of vertices.
4242
* @param {number[]} [alphas] - An optional array containing the alpha data for this Rope. You should provide one alpha value per pair of vertices.
4343
*/
@@ -64,7 +64,7 @@ var Rope = new Class({
6464
{
6565
if (points === undefined)
6666
{
67-
points = [ { x: 0, y: 0 } ];
67+
points = 2;
6868
}
6969

7070
GameObject.call(this, scene, 'Rope');
@@ -175,7 +175,10 @@ var Rope = new Class({
175175
this.setSizeToFrame();
176176
this.initPipeline('TextureTintStripPipeline');
177177

178-
this.resizeArrays(points.length);
178+
if (Array.isArray(points))
179+
{
180+
this.resizeArrays(points.length);
181+
}
179182

180183
this.setPoints(points, colors, alphas);
181184

@@ -438,42 +441,79 @@ var Rope = new Class({
438441
* ]);
439442
* ```
440443
*
444+
* Or, you can provide an integer to do the same thing:
445+
*
446+
* ```javascript
447+
* rope.setPoints(4);
448+
* ```
449+
*
450+
* Which will divide the Rope into 4 equally sized segments based on the frame width.
451+
*
441452
* Note that calling this method with a different number of points than the Rope has currently will
442453
* _reset_ the color and alpha values, unless you provide them as arguments to this method.
443454
*
444-
* See also `Rope.split`.
445-
*
446455
* @method Phaser.GameObjects.Rope#setPoints
447456
* @since 3.23.0
448457
*
449-
* @param {Phaser.Math.Types.Vector2Like[]} [points] - An array of points to split the Rope into.
458+
* @param {(integer|Phaser.Types.Math.Vector2Like[])} [points] - An array containing the vertices data for this Rope, or a number that indicates how many segments to split the texture frame into. If none is provided a simple quad is created.
450459
* @param {(number|number[])} [colors] - Either a single color value, or an array of values.
451460
* @param {(number|number[])} [alphas] - Either a single alpha value, or an array of values.
452461
*
453462
* @return {this} This Game Object instance.
454463
*/
455464
setPoints: function (points, colors, alphas)
456465
{
466+
if (points === undefined) { points = 2; }
467+
468+
if (typeof points === 'number')
469+
{
470+
// Generate an array based on the points
471+
var segments = points;
472+
473+
if (segments < 2)
474+
{
475+
segments = 2;
476+
}
477+
478+
var frameSegment = this.frame.width / (segments - 1);
479+
480+
points = [];
481+
482+
for (var s = 0; s < segments; s++)
483+
{
484+
points.push({ x: s * frameSegment, y: 0 });
485+
}
486+
}
487+
457488
var total = points.length;
489+
var currentTotal = this.points.length;
458490

459491
if (total < 1)
460492
{
493+
console.warn('Rope: Not enough points given');
494+
461495
return this;
462496
}
497+
else if (total === 1)
498+
{
499+
points.unshift({ x: 0, y: 0 });
500+
total++;
501+
}
463502

464-
var currentUVs = this.uv;
465-
466-
if (this.points.length !== total)
503+
if (currentTotal !== total)
467504
{
468505
this.resizeArrays(total);
469506
}
470507

508+
var currentUVs = this.uv;
509+
471510
var u0 = this.frame.u0;
472511
var v0 = this.frame.v0;
473512
var u1 = this.frame.u1;
474513
var v1 = this.frame.v1;
514+
475515
var part = (u1 - u0) / (total - 1);
476-
516+
477517
for (var i = 0; i < total; i++)
478518
{
479519
var index = i * 4;
@@ -565,7 +605,7 @@ var Rope = new Class({
565605

566606
var lastPoint = points[0];
567607
var nextPoint;
568-
608+
569609
for (var i = 0; i < total; i++)
570610
{
571611
var point = points[i];
@@ -579,17 +619,10 @@ var Rope = new Class({
579619
{
580620
nextPoint = point;
581621
}
582-
622+
583623
perp.x = nextPoint.y - lastPoint.y;
584624
perp.y = -(nextPoint.x - lastPoint.x);
585625

586-
// var ratio = (1 - (i / (total - 1))) * 10;
587-
588-
// if (ratio > 1)
589-
// {
590-
// ratio = 1;
591-
// }
592-
593626
var perpLength = perp.length();
594627
var num = this.frame.halfHeight;
595628

src/gameobjects/rope/RopeWebGLRenderer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ var RopeWebGLRenderer = function (renderer, src, interpolationPercentage, camera
6767
var meshVerticesLength = vertices.length;
6868
var vertexCount = Math.floor(meshVerticesLength * 0.5);
6969

70-
// Because it's a triangle strip
70+
// Because it's a triangle strip and we don't want lots of degenerate triangles joining things up
7171
pipeline.flush();
7272

7373
pipeline.setTexture2D(texture, 0);

0 commit comments

Comments
 (0)