Skip to content

Commit 93d9e97

Browse files
committed
Split the Layer Actions out into their own namespace, because they can now be used from anywhere (just pass in an array of Game Objects). Renamed Align to GridAlign. Added step argument to SetX/Y/Rotation.
1 parent 81aa09d commit 93d9e97

33 files changed

Lines changed: 311 additions & 249 deletions

v3/src/actions/Angle.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var Angle = function (items, value)
2+
{
3+
for (var i = 0; i < items.length; i++)
4+
{
5+
items[i].angle += value;
6+
}
7+
8+
return items;
9+
};
10+
11+
module.exports = Angle;
Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
var CONST = require('../../../utils/align/const');
2-
var AlignIn = require('../../../utils/align/AlignIn');
3-
var Zone = require('../../zone/Zone');
1+
var CONST = require('../utils/align/const');
2+
var AlignIn = require('../utils/align/AlignIn');
3+
var Zone = require('../gameobjects/zone/Zone');
44

55
var tempZone = new Zone({}, 0, 0, 1, 1);
66

@@ -53,32 +53,21 @@ var tempZone = new Zone({}, 0, 0, 1, 1);
5353
* @param {integer} cellWidth - The width of each grid cell, in pixels.
5454
* @param {integer} cellHeight - The height of each grid cell, in pixels.
5555
* @param {integer} [position] - The position constant. One of `Phaser.TOP_LEFT` (default), `Phaser.TOP_CENTER`, `Phaser.TOP_RIGHT`, `Phaser.LEFT_CENTER`, `Phaser.CENTER`, `Phaser.RIGHT_CENTER`, `Phaser.BOTTOM_LEFT`, `Phaser.BOTTOM_CENTER` or `Phaser.BOTTOM_RIGHT`.
56-
* @param {integer} [offset=0] - Optional index to start the alignment from. Defaults to zero, the first child in the Group, but can be set to any valid child index value.
5756
* @return {boolean} True if the Group children were aligned, otherwise false.
5857
*/
59-
var Align = function (width, height, cellWidth, cellHeight, position, offset)
58+
var GridAlign = function (items, width, height, cellWidth, cellHeight, position)
6059
{
6160
if (position === undefined) { position = CONST.TOP_LEFT; }
62-
if (offset === undefined) { offset = 0; }
63-
64-
var children = this.children.entries;
65-
66-
if (children.length === 0 || offset > children.length || (width === -1 && height === -1))
67-
{
68-
return false;
69-
}
7061

7162
tempZone.setPosition(0, 0);
7263
tempZone.setSize(cellWidth, cellHeight);
7364

7465
var w = (width * cellWidth);
7566
var h = (height * cellHeight);
7667

77-
for (var i = offset; i < children.length; i++)
68+
for (var i = 0; i < items.length; i++)
7869
{
79-
var child = children[i];
80-
81-
AlignIn(child, tempZone, position);
70+
AlignIn(items[i], tempZone, position);
8271

8372
if (width === -1)
8473
{
@@ -114,15 +103,14 @@ var Align = function (width, height, cellWidth, cellHeight, position, offset)
114103

115104
if (tempZone.y === h)
116105
{
117-
// We've hit the column limit, so return, even if there are children left
118-
return true;
106+
// We've hit the column limit, so return, even if there are items left
107+
break;
119108
}
120109
}
121110
}
122111
}
123112

124-
return true;
113+
return items;
125114
};
126115

127-
module.exports = Align;
128-
116+
module.exports = GridAlign;

v3/src/actions/IncX.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var IncX = function (items, value)
2+
{
3+
for (var i = 0; i < items.length; i++)
4+
{
5+
items[i].x += value;
6+
}
7+
8+
return items;
9+
};
10+
11+
module.exports = IncX;

v3/src/actions/IncXY.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var IncXY = function (items, x, y)
2+
{
3+
for (var i = 0; i < items.length; i++)
4+
{
5+
items[i].x += x;
6+
items[i].y += y;
7+
}
8+
9+
return items;
10+
};
11+
12+
module.exports = IncXY;

v3/src/actions/IncY.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var IncY = function (items, value)
2+
{
3+
for (var i = 0; i < items.length; i++)
4+
{
5+
items[i].y += value;
6+
}
7+
8+
return items;
9+
};
10+
11+
module.exports = IncY;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var PositionAroundCircle = function (items, circle, startAngle, endAngle)
2+
{
3+
if (startAngle === undefined) { startAngle = 0; }
4+
if (endAngle === undefined) { endAngle = 6.28; }
5+
6+
var angle = startAngle;
7+
var angleStep = (endAngle - startAngle) / items.length;
8+
9+
for (var i = 0; i < items.length; i++)
10+
{
11+
items[i].x = circle.x + (circle.radius * Math.cos(angle));
12+
items[i].y = circle.y + (circle.radius * Math.sin(angle));
13+
14+
angle += angleStep;
15+
}
16+
17+
return items;
18+
};
19+
20+
module.exports = PositionAroundCircle;

v3/src/actions/Rotate.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var Rotate = function (items, value)
2+
{
3+
for (var i = 0; i < items.length; i++)
4+
{
5+
items[i].rotation += value;
6+
}
7+
8+
return items;
9+
};
10+
11+
module.exports = Rotate;

v3/src/actions/RotateAround.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var RotateAroundDistance = require('../math/RotateAroundDistance');
2+
var DistanceBetween = require('../math/distance/DistanceBetween');
3+
4+
// point = any object with public x/y properties
5+
6+
var RotateAround = function (items, point, angle)
7+
{
8+
var x = point.x;
9+
var y = point.y;
10+
11+
for (var i = 0; i < items.length; i++)
12+
{
13+
var item = items[i];
14+
15+
RotateAroundDistance(item, x, y, angle, Math.max(1, DistanceBetween(item.x, item.y, x, y)));
16+
}
17+
18+
return items;
19+
};
20+
21+
module.exports = RotateAround;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var MathRotateAroundDistance = require('../math/RotateAroundDistance');
2+
3+
// point = any object with public x/y properties
4+
// angle = radians
5+
// distance = px
6+
7+
var RotateAroundDistance = function (items, point, angle, distance)
8+
{
9+
var x = point.x;
10+
var y = point.y;
11+
12+
for (var i = 0; i < items.length; i++)
13+
{
14+
MathRotateAroundDistance(items[i], x, y, angle, distance);
15+
}
16+
17+
return items;
18+
};
19+
20+
module.exports = RotateAroundDistance;

v3/src/actions/SetRotation.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var SetRotation = function (items, value, step)
2+
{
3+
if (step === undefined) { step = 0; }
4+
5+
for (var i = 0; i < items.length; i++)
6+
{
7+
items[i].rotation = value + (i * step);
8+
}
9+
10+
return items;
11+
};
12+
13+
module.exports = SetRotation;

0 commit comments

Comments
 (0)