2017-03-22 23:16:44 +00:00
|
|
|
|
|
|
|
var Class = require('../../utils/Class');
|
2017-03-27 15:59:51 +00:00
|
|
|
var Set = require('../../structs/Set');
|
2017-03-28 13:01:35 +00:00
|
|
|
var GetObjectValue = require('../../utils/object/GetObjectValue');
|
2017-03-28 22:38:08 +00:00
|
|
|
var Range = require('../../utils/array/Range');
|
2017-03-28 12:20:39 +00:00
|
|
|
var Actions = require('../../actions/');
|
2017-03-28 02:09:59 +00:00
|
|
|
var Sprite = require('../sprite/Sprite');
|
2017-03-22 23:16:44 +00:00
|
|
|
|
|
|
|
var Layer = new Class({
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2017-03-27 23:05:08 +00:00
|
|
|
function Layer (state, children)
|
2017-03-22 23:16:44 +00:00
|
|
|
{
|
2017-03-27 23:05:08 +00:00
|
|
|
this.state = state;
|
|
|
|
|
2017-03-27 15:59:51 +00:00
|
|
|
this.children = new Set(children);
|
2017-03-28 02:09:59 +00:00
|
|
|
|
|
|
|
this.classType = Sprite;
|
2017-03-22 23:16:44 +00:00
|
|
|
},
|
|
|
|
|
2017-03-27 15:59:51 +00:00
|
|
|
// Layer management methods:
|
2017-03-22 23:16:44 +00:00
|
|
|
|
2017-03-27 15:59:51 +00:00
|
|
|
add: function (child)
|
2017-03-23 00:07:41 +00:00
|
|
|
{
|
2017-03-27 15:59:51 +00:00
|
|
|
this.children.set(child);
|
2017-03-23 00:07:41 +00:00
|
|
|
|
|
|
|
return this;
|
2017-03-22 23:16:44 +00:00
|
|
|
},
|
|
|
|
|
2017-03-27 15:59:51 +00:00
|
|
|
addMultiple: function (children)
|
2017-03-22 23:16:44 +00:00
|
|
|
{
|
2017-03-27 15:59:51 +00:00
|
|
|
if (Array.isArray(children))
|
2017-03-22 23:16:44 +00:00
|
|
|
{
|
2017-03-27 15:59:51 +00:00
|
|
|
for (var i = 0; i < children.length; i++)
|
|
|
|
{
|
|
|
|
this.children.set(children[i]);
|
|
|
|
}
|
2017-03-22 23:16:44 +00:00
|
|
|
}
|
2017-03-23 00:07:41 +00:00
|
|
|
|
|
|
|
return this;
|
2017-03-22 23:16:44 +00:00
|
|
|
},
|
|
|
|
|
2017-03-28 02:09:59 +00:00
|
|
|
create: function (x, y, key, frame, visible)
|
|
|
|
{
|
|
|
|
if (visible === undefined) { visible = true; }
|
|
|
|
|
|
|
|
var child = this.state.children.add(new this.classType(this.state, x, y, key, frame));
|
|
|
|
|
|
|
|
child.visible = visible;
|
|
|
|
|
2017-03-28 23:09:16 +00:00
|
|
|
this.add(child);
|
|
|
|
|
|
|
|
return child;
|
2017-03-28 02:09:59 +00:00
|
|
|
},
|
|
|
|
|
2017-03-29 00:12:14 +00:00
|
|
|
createFromConfig: function (options)
|
2017-03-28 02:09:59 +00:00
|
|
|
{
|
2017-03-29 00:12:14 +00:00
|
|
|
var key = GetObjectValue(options, 'key', undefined);
|
|
|
|
var frame = GetObjectValue(options, 'frame', null);
|
2017-03-28 13:01:35 +00:00
|
|
|
var visible = GetObjectValue(options, 'visible', true);
|
2017-03-28 02:09:59 +00:00
|
|
|
|
2017-03-29 00:12:14 +00:00
|
|
|
var entries = [];
|
|
|
|
|
|
|
|
// Can't do anything without at least a key
|
|
|
|
if (key === undefined)
|
2017-03-28 02:09:59 +00:00
|
|
|
{
|
2017-03-29 00:12:14 +00:00
|
|
|
return entries;
|
2017-03-28 02:09:59 +00:00
|
|
|
}
|
2017-03-29 00:12:14 +00:00
|
|
|
else
|
2017-03-28 02:09:59 +00:00
|
|
|
{
|
2017-03-29 00:12:14 +00:00
|
|
|
if (!Array.isArray(key))
|
|
|
|
{
|
|
|
|
key = [ key ];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Array.isArray(frame))
|
|
|
|
{
|
|
|
|
frame = [ frame ];
|
|
|
|
}
|
2017-03-28 02:09:59 +00:00
|
|
|
}
|
|
|
|
|
2017-03-28 22:38:08 +00:00
|
|
|
// Build an array of key frame pairs to loop through
|
|
|
|
|
|
|
|
var repeat = GetObjectValue(options, 'repeat', 0);
|
|
|
|
var randomKey = GetObjectValue(options, 'randomKey', false);
|
|
|
|
var randomFrame = GetObjectValue(options, 'randomFrame', false);
|
|
|
|
var yoyo = GetObjectValue(options, 'yoyo', false);
|
2017-03-29 00:34:46 +00:00
|
|
|
var quantity = GetObjectValue(options, 'frameQuantity', 1);
|
2017-03-28 22:38:08 +00:00
|
|
|
var max = GetObjectValue(options, 'max', 0);
|
|
|
|
|
|
|
|
var range = Range(key, frame, {
|
|
|
|
max: max,
|
|
|
|
qty: quantity,
|
|
|
|
random: randomKey,
|
|
|
|
randomB: randomFrame,
|
|
|
|
repeat: repeat,
|
|
|
|
yoyo: yoyo
|
2017-03-28 02:09:59 +00:00
|
|
|
});
|
|
|
|
|
2017-03-29 00:12:14 +00:00
|
|
|
for (var c = 0; c < range.length; c++)
|
2017-03-28 22:38:08 +00:00
|
|
|
{
|
2017-03-29 00:12:14 +00:00
|
|
|
entries.push(this.create(0, 0, range[c].a, range[c].b, visible));
|
2017-03-28 22:38:08 +00:00
|
|
|
}
|
|
|
|
|
2017-03-28 23:09:16 +00:00
|
|
|
// Post-creation options (applied only to those items created in this call):
|
2017-03-28 13:01:35 +00:00
|
|
|
|
2017-03-28 22:38:08 +00:00
|
|
|
var x = GetObjectValue(options, 'setXY.x', 0);
|
|
|
|
var y = GetObjectValue(options, 'setXY.y', 0);
|
|
|
|
var stepX = GetObjectValue(options, 'setXY.stepX', 0);
|
|
|
|
var stepY = GetObjectValue(options, 'setXY.stepY', 0);
|
2017-03-28 13:01:35 +00:00
|
|
|
|
2017-03-28 23:09:16 +00:00
|
|
|
Actions.SetXY(entries, x, y, stepX, stepY);
|
2017-03-28 13:01:35 +00:00
|
|
|
|
2017-03-28 22:38:08 +00:00
|
|
|
var rotation = GetObjectValue(options, 'setRotation.value', 0);
|
|
|
|
var stepRotation = GetObjectValue(options, 'setRotation.step', 0);
|
2017-03-28 13:01:35 +00:00
|
|
|
|
2017-03-28 23:09:16 +00:00
|
|
|
Actions.SetRotation(entries, rotation, stepRotation);
|
2017-03-28 13:01:35 +00:00
|
|
|
|
2017-03-28 23:44:08 +00:00
|
|
|
var scaleX = GetObjectValue(options, 'setScale.x', 1);
|
2017-03-29 00:03:24 +00:00
|
|
|
var scaleY = GetObjectValue(options, 'setScale.y', scaleX);
|
2017-03-28 23:44:08 +00:00
|
|
|
var stepScaleX = GetObjectValue(options, 'setScale.stepX', 0);
|
|
|
|
var stepScaleY = GetObjectValue(options, 'setScale.stepY', 0);
|
|
|
|
|
|
|
|
Actions.SetScale(entries, scaleX, scaleY, stepScaleX, stepScaleY);
|
|
|
|
|
2017-03-28 22:38:08 +00:00
|
|
|
var alpha = GetObjectValue(options, 'setAlpha.value', 1);
|
|
|
|
var stepAlpha = GetObjectValue(options, 'setAlpha.step', 0);
|
2017-03-28 13:01:35 +00:00
|
|
|
|
2017-03-28 23:09:16 +00:00
|
|
|
Actions.SetAlpha(entries, alpha, stepAlpha);
|
2017-03-28 13:01:35 +00:00
|
|
|
|
2017-03-28 02:09:59 +00:00
|
|
|
return entries;
|
|
|
|
},
|
|
|
|
|
2017-03-29 00:12:14 +00:00
|
|
|
createMultiple: function (config)
|
|
|
|
{
|
|
|
|
if (!Array.isArray(config))
|
|
|
|
{
|
|
|
|
config = [ config ];
|
|
|
|
}
|
|
|
|
|
|
|
|
var output = [];
|
|
|
|
|
|
|
|
for (var i = 0; i < config.length; i++)
|
|
|
|
{
|
|
|
|
var entries = this.createFromConfig(config[i]);
|
|
|
|
|
|
|
|
output = output.concat(entries);
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
},
|
|
|
|
|
2017-03-27 15:59:51 +00:00
|
|
|
remove: function (child)
|
2017-03-23 00:07:41 +00:00
|
|
|
{
|
2017-03-27 15:59:51 +00:00
|
|
|
this.children.delete(child);
|
2017-03-23 00:07:41 +00:00
|
|
|
|
|
|
|
return this;
|
2017-03-22 23:16:44 +00:00
|
|
|
},
|
|
|
|
|
2017-03-27 15:59:51 +00:00
|
|
|
clear: function ()
|
2017-03-23 00:07:41 +00:00
|
|
|
{
|
2017-03-27 15:59:51 +00:00
|
|
|
this.children.clear();
|
2017-03-23 00:07:41 +00:00
|
|
|
|
|
|
|
return this;
|
2017-03-22 23:16:44 +00:00
|
|
|
},
|
|
|
|
|
2017-03-27 23:05:08 +00:00
|
|
|
destroy: function ()
|
|
|
|
{
|
|
|
|
this.children.clear();
|
2017-03-22 23:16:44 +00:00
|
|
|
|
2017-03-27 23:05:08 +00:00
|
|
|
this.state = undefined;
|
|
|
|
this.children = undefined;
|
2017-03-28 02:09:59 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// Child related methods
|
|
|
|
|
2017-03-28 12:20:39 +00:00
|
|
|
angle: function (value)
|
|
|
|
{
|
|
|
|
Actions.Angle(this.children.entries, value);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-03-29 00:34:46 +00:00
|
|
|
gridAlign: function (options)
|
2017-03-28 12:20:39 +00:00
|
|
|
{
|
2017-03-29 00:34:46 +00:00
|
|
|
Actions.GridAlign(this.children.entries, options);
|
2017-03-28 12:20:39 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-03-28 13:01:35 +00:00
|
|
|
incAlpha: function (value, step)
|
|
|
|
{
|
|
|
|
Actions.IncAlpha(this.children.entries, value, step);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-03-28 12:20:39 +00:00
|
|
|
incX: function (value)
|
|
|
|
{
|
|
|
|
Actions.IncX(this.children.entries, value);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
incXY: function (x, y)
|
|
|
|
{
|
|
|
|
Actions.IncXY(this.children.entries, x, y);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
incY: function (value)
|
|
|
|
{
|
|
|
|
Actions.IncY(this.children.entries, value);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-03-29 15:04:51 +00:00
|
|
|
placeOnCircle: function (circle, startAngle, endAngle)
|
2017-03-28 12:20:39 +00:00
|
|
|
{
|
2017-03-29 15:04:51 +00:00
|
|
|
Actions.PlaceOnCircle(this.children.entries, circle, startAngle, endAngle);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
placeOnLine: function (line)
|
|
|
|
{
|
|
|
|
Actions.PlaceOnLine(this.children.entries, line);
|
2017-03-28 12:20:39 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-03-29 19:27:36 +00:00
|
|
|
placeOnRectangle: function (rect, shift)
|
2017-03-29 16:11:26 +00:00
|
|
|
{
|
2017-03-29 19:27:36 +00:00
|
|
|
Actions.PlaceOnRectangle(this.children.entries, rect, shift);
|
2017-03-29 16:11:26 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-03-30 23:46:19 +00:00
|
|
|
placeOnTriangle: function (triangle, stepRate)
|
|
|
|
{
|
|
|
|
Actions.PlaceOnTriangle(this.children.entries, triangle, stepRate);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-03-28 13:57:42 +00:00
|
|
|
randomCircle: function (circle)
|
|
|
|
{
|
|
|
|
Actions.RandomCircle(this.children.entries, circle);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
randomEllipse: function (ellipse)
|
|
|
|
{
|
|
|
|
Actions.RandomEllipse(this.children.entries, ellipse);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
randomLine: function (line)
|
|
|
|
{
|
|
|
|
Actions.RandomLine(this.children.entries, line);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
randomRectangle: function (rect)
|
|
|
|
{
|
|
|
|
Actions.RandomRectangle(this.children.entries, rect);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-03-31 00:21:55 +00:00
|
|
|
randomTriangle: function (triangle)
|
|
|
|
{
|
|
|
|
Actions.RandomTriangle(this.children.entries, triangle);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-03-29 00:03:24 +00:00
|
|
|
rotate: function (value, step)
|
2017-03-28 12:20:39 +00:00
|
|
|
{
|
2017-03-29 00:03:24 +00:00
|
|
|
Actions.Rotate(this.children.entries, value, step);
|
2017-03-28 12:20:39 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
rotateAround: function (point, angle)
|
|
|
|
{
|
|
|
|
Actions.RotateAround(this.children.entries, point, angle);
|
2017-03-28 02:09:59 +00:00
|
|
|
|
2017-03-28 12:20:39 +00:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
rotateAroundDistance: function (point, angle, distance)
|
|
|
|
{
|
|
|
|
Actions.RotateAroundDistance(this.children.entries, point, angle, distance);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-03-28 13:01:35 +00:00
|
|
|
setAlpha: function (value, step)
|
|
|
|
{
|
|
|
|
Actions.SetAlpha(this.children.entries, value, step);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-03-28 23:12:33 +00:00
|
|
|
setOrigin: function (x, y)
|
|
|
|
{
|
|
|
|
Actions.SetOrigin(this.children.entries, x, y);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-03-28 13:30:43 +00:00
|
|
|
scaleX: function (value)
|
|
|
|
{
|
|
|
|
Actions.ScaleX(this.children.entries, value);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
scaleXY: function (x, y)
|
|
|
|
{
|
|
|
|
Actions.ScaleXY(this.children.entries, x, y);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
scaleY: function (value)
|
|
|
|
{
|
|
|
|
Actions.ScaleY(this.children.entries, value);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-03-28 12:20:39 +00:00
|
|
|
setRotation: function (value, step)
|
|
|
|
{
|
|
|
|
Actions.SetRotation(this.children.entries, value, step);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-03-28 13:30:43 +00:00
|
|
|
setScale: function (x, y, stepX, stepY)
|
|
|
|
{
|
|
|
|
Actions.SetScale(this.children.entries, x, y, stepX, stepY);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
setScaleX: function (value, step)
|
|
|
|
{
|
|
|
|
Actions.SetScaleX(this.children.entries, value, step);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
setScaleY: function (value, step)
|
|
|
|
{
|
|
|
|
Actions.SetScaleY(this.children.entries, value, step);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-03-28 12:20:39 +00:00
|
|
|
setVisible: function (value)
|
|
|
|
{
|
|
|
|
Actions.SetVisible(this.children.entries, value);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
setX: function (value, step)
|
|
|
|
{
|
|
|
|
Actions.SetX(this.children.entries, value, step);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-03-28 13:01:35 +00:00
|
|
|
setXY: function (x, y, stepX, stepY)
|
2017-03-28 12:20:39 +00:00
|
|
|
{
|
2017-03-28 13:01:35 +00:00
|
|
|
Actions.SetXY(this.children.entries, x, y, stepX, stepY);
|
2017-03-28 12:20:39 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
setY: function (value, step)
|
|
|
|
{
|
|
|
|
Actions.SetY(this.children.entries, value, step);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-03-28 15:05:01 +00:00
|
|
|
smootherStep: function (property, min, max, inc)
|
2017-03-28 14:33:20 +00:00
|
|
|
{
|
2017-03-28 15:05:01 +00:00
|
|
|
Actions.SmootherStep(this.children.entries, property, min, max, inc);
|
2017-03-28 14:33:20 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-03-28 15:05:01 +00:00
|
|
|
smoothStep: function (property, min, max, inc)
|
2017-03-28 14:33:20 +00:00
|
|
|
{
|
2017-03-28 15:05:01 +00:00
|
|
|
Actions.SmoothStep(this.children.entries, property, min, max, inc);
|
2017-03-28 14:33:20 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-03-28 15:05:01 +00:00
|
|
|
spread: function (property, min, max, inc)
|
2017-03-28 14:33:20 +00:00
|
|
|
{
|
2017-03-28 15:05:01 +00:00
|
|
|
Actions.Spread(this.children.entries, property, min, max, inc);
|
2017-03-28 14:33:20 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-03-28 12:20:39 +00:00
|
|
|
toggleVisible: function ()
|
|
|
|
{
|
|
|
|
Actions.ToggleVisible(this.children.entries);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2017-03-28 02:09:59 +00:00
|
|
|
|
2017-03-22 23:16:44 +00:00
|
|
|
});
|
|
|
|
|
2017-03-23 00:07:41 +00:00
|
|
|
module.exports = Layer;
|