Skip to content

Commit 81aa09d

Browse files
committed
Added Layer actions for create, createMultiple, rotation around a fixed point, rotation by distance, and positioning around a circle.
1 parent 9268fcb commit 81aa09d

9 files changed

Lines changed: 187 additions & 54 deletions

File tree

v3/src/checksum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var CHECKSUM = {
2-
build: 'fec73390-133d-11e7-8896-516d977f6ae4'
2+
build: 'b5ac0990-1358-11e7-8a48-1f6ad0f93601'
33
};
44
module.exports = CHECKSUM;

v3/src/gameobjects/layer/Layer.js

Lines changed: 114 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,19 @@
22
var Class = require('../../utils/Class');
33
var Set = require('../../structs/Set');
44
var Actions = require('./actions/');
5+
var Sprite = require('../sprite/Sprite');
56

67
var Layer = new Class({
78

8-
Mixins: [
9-
Actions.IncX,
10-
Actions.IncY,
11-
Actions.IncXY,
12-
Actions.SetX,
13-
Actions.SetY,
14-
Actions.SetXY,
15-
Actions.Rotate,
16-
Actions.Angle,
17-
Actions.SetRotation,
18-
Actions.SetVisible,
19-
Actions.ToggleVisible,
20-
Actions.Align
21-
],
22-
239
initialize:
2410

2511
function Layer (state, children)
2612
{
2713
this.state = state;
2814

2915
this.children = new Set(children);
16+
17+
this.classType = Sprite;
3018
},
3119

3220
// Layer management methods:
@@ -51,6 +39,96 @@ var Layer = new Class({
5139
return this;
5240
},
5341

42+
create: function (x, y, key, frame, visible)
43+
{
44+
if (visible === undefined) { visible = true; }
45+
46+
var child = this.state.children.add(new this.classType(this.state, x, y, key, frame));
47+
48+
child.visible = visible;
49+
50+
return this.add(child);
51+
},
52+
53+
/**
54+
* Creates multiple Phaser.Sprite objects and adds them to the top of this Group.
55+
*
56+
* This method is useful if you need to quickly generate a pool of sprites, such as bullets.
57+
*
58+
* Use {@link #classType} to change the type of object created.
59+
*
60+
* You can provide an array as the `key` and / or `frame` arguments. When you do this
61+
* it will create `quantity` Sprites for every key (and frame) in the arrays.
62+
*
63+
* For example:
64+
*
65+
* `createMultiple(25, ['ball', 'carrot'])`
66+
*
67+
* In the above code there are 2 keys (ball and carrot) which means that 50 sprites will be
68+
* created in total, 25 of each. You can also have the `frame` as an array:
69+
*
70+
* `createMultiple(5, 'bricks', [0, 1, 2, 3])`
71+
*
72+
* In the above there is one key (bricks), which is a sprite sheet. The frames array tells
73+
* this method to use frames 0, 1, 2 and 3. So in total it will create 20 sprites, because
74+
* the quantity was set to 5, so that is 5 brick sprites of frame 0, 5 brick sprites with
75+
* frame 1, and so on.
76+
*
77+
* If you set both the key and frame arguments to be arrays then understand it will create
78+
* a total quantity of sprites equal to the size of both arrays times each other. I.e.:
79+
*
80+
* `createMultiple(20, ['diamonds', 'balls'], [0, 1, 2])`
81+
*
82+
* The above will create 20 'diamonds' of frame 0, 20 with frame 1 and 20 with frame 2.
83+
* It will then create 20 'balls' of frame 0, 20 with frame 1 and 20 with frame 2.
84+
* In total it will have created 120 sprites.
85+
*
86+
* By default the Sprites will have their `exists` property set to `false`, and they will be
87+
* positioned at 0x0, relative to the `Group.x / y` values.
88+
*
89+
* If `Group.enableBody` is set, then a physics body will be created on the objects, so long as one does not already exist.
90+
*
91+
* If `Group.inputEnableChildren` is set, then an Input Handler will be created on the objects, so long as one does not already exist.
92+
*
93+
* @method Phaser.Group#createMultiple
94+
* @param {integer} quantity - The number of Sprites to create.
95+
* @param {string|array} key - The Cache key of the image that the Sprites will use. Or an Array of keys. See the description for details on how the quantity applies when arrays are used.
96+
* @param {integer|string|array} [frame=0] - If the Sprite image contains multiple frames you can specify which one to use here. Or an Array of frames. See the description for details on how the quantity applies when arrays are used.
97+
* @param {boolean} [exists=false] - The default exists state of the Sprite.
98+
* @return {array} An array containing all of the Sprites that were created.
99+
*/
100+
createMultiple: function (quantity, key, frame, visible)
101+
{
102+
if (frame === undefined) { frame = null; }
103+
if (visible === undefined) { visible = true; }
104+
105+
if (!Array.isArray(key))
106+
{
107+
key = [ key ];
108+
}
109+
110+
if (!Array.isArray(frame))
111+
{
112+
frame = [ frame ];
113+
}
114+
115+
var _this = this;
116+
var entries = [];
117+
118+
key.forEach(function (singleKey)
119+
{
120+
frame.forEach(function (singleFrame)
121+
{
122+
for (var i = 0; i < quantity; i++)
123+
{
124+
entries.push(_this.create(0, 0, singleKey, singleFrame, visible));
125+
}
126+
});
127+
});
128+
129+
return entries;
130+
},
131+
54132
remove: function (child)
55133
{
56134
this.children.delete(child);
@@ -71,7 +149,27 @@ var Layer = new Class({
71149

72150
this.state = undefined;
73151
this.children = undefined;
74-
}
152+
},
153+
154+
// Child related methods
155+
156+
align: Actions.Align,
157+
angle: Actions.Angle,
158+
incX: Actions.IncX,
159+
incXY: Actions.IncXY,
160+
incY: Actions.IncY,
161+
positionAroundCircle: Actions.PositionAroundCircle,
162+
rotate: Actions.Rotate,
163+
rotateAround: Actions.RotateAround,
164+
rotateAroundDistance: Actions.RotateAroundDistance,
165+
setRotation: Actions.SetRotation,
166+
setVisible: Actions.SetVisible,
167+
setX: Actions.SetX,
168+
setXY: Actions.SetXY,
169+
setY: Actions.SetY,
170+
toggleVisible: Actions.ToggleVisible
171+
172+
75173
});
76174

77175
module.exports = Layer;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var PositionAroundCircle = function (circle, startAngle, endAngle)
2+
{
3+
if (startAngle === undefined) { startAngle = 0; }
4+
if (endAngle === undefined) { endAngle = 6.28; }
5+
6+
var children = this.children.entries;
7+
8+
var angle = startAngle;
9+
var angleStep = (endAngle - startAngle) / children.length;
10+
11+
for (var i = 0; i < children.length; i++)
12+
{
13+
children[i].x = circle.x + (circle.radius * Math.cos(angle));
14+
children[i].y = circle.y + (circle.radius * Math.sin(angle));
15+
16+
angle += angleStep;
17+
}
18+
};
19+
20+
module.exports = PositionAroundCircle;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 (point, angle)
7+
{
8+
var x = point.x;
9+
var y = point.y;
10+
var children = this.children.entries;
11+
12+
for (var i = 0; i < children.length; i++)
13+
{
14+
var child = children[i];
15+
16+
RotateAroundDistance(child, x, y, angle, Math.max(1, DistanceBetween(child.x, child.y, x, y)));
17+
}
18+
19+
return this;
20+
};
21+
22+
module.exports = RotateAround;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 (point, angle, distance)
8+
{
9+
var x = point.x;
10+
var y = point.y;
11+
var children = this.children.entries;
12+
13+
for (var i = 0; i < children.length; i++)
14+
{
15+
MathRotateAroundDistance(children[i], x, y, angle, distance);
16+
}
17+
18+
return this;
19+
};
20+
21+
module.exports = RotateAroundDistance;

v3/src/gameobjects/layer/actions/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ module.exports = {
55
IncY: require('./IncY'),
66
Align: require('./Align'),
77
Angle: require('./Angle'),
8+
PositionAroundCircle: require('./PositionAroundCircle'),
89
Rotate: require('./Rotate'),
10+
RotateAround: require('./RotateAround'),
11+
RotateAroundDistance: require('./RotateAroundDistance'),
912
SetRotation: require('./SetRotation'),
1013
SetVisible: require('./SetVisible'),
1114
SetX: require('./SetX'),

v3/src/geom/circle/CircumferencePoint.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Returns a Point object containing the coordinates of a point on the circumference of the Circle based on the given angle.
33
* @method Phaser.Circle.circumferencePoint
44
* @param {Phaser.Circle} a - The first Circle object.
5-
* @param {number} angle - The angle in radians (unless asDegrees is true) to return the point from.
5+
* @param {number} angle - The angle in radians to return the point from.
66
* @param {Phaser.Point} [out] - An optional Point object to put the result in to. If none specified a new Point object will be created.
77
* @return {Phaser.Point} The Point object holding the result.
88
*/

v3/src/math/RotateAroundDistance.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
// p = Point or any object with public x/y properties
1+
// p = Point or any object with public x/y properties, the item to be rotated
2+
// x/y = the coordinate to rotate around
3+
// angle = radians
4+
// distance = in px
25

36
var RotateAroundDistance = function (point, x, y, angle, distance)
47
{

v3/src/textures/Texture.js

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -146,43 +146,9 @@ Texture.prototype = {
146146
*/
147147
destroy: function ()
148148
{
149-
// Need to iterate though the TextureSources, and unload each one
150-
// then clear out the frames
151-
152-
/*
153-
if (this.source)
154-
{
155-
Phaser.CanvasPool.removeByCanvas(this.source);
156-
}
157-
158-
this.source = null;
159-
*/
160-
}
161-
162-
};
163-
164-
/**
165-
* Helper function that creates a base texture from the given canvas element.
166-
*
167-
* @static
168-
* @method fromCanvas
169-
* @param canvas {Canvas} The canvas element source of the texture
170-
* @param scaleMode {Number} See {{#crossLink "PIXI/scaleModes:property"}}Phaser.scaleModes{{/crossLink}} for possible values
171-
* @return {BaseTexture}
172-
Phaser.Texture.fromCanvas = function (canvas, scaleMode)
173-
{
174-
if (canvas.width === 0)
175-
{
176-
canvas.width = 1;
149+
// TODO
177150
}
178151

179-
if (canvas.height === 0)
180-
{
181-
canvas.height = 1;
182-
}
183-
184-
return new Phaser.Texture(canvas, scaleMode);
185152
};
186-
*/
187153

188154
module.exports = Texture;

0 commit comments

Comments
 (0)