phaser/v3/src/gameobjects/group/Group.js

538 lines
12 KiB
JavaScript
Raw Normal View History

2017-11-02 17:04:02 +00:00
var Actions = require('../../actions/');
2017-03-22 23:16:44 +00:00
var Class = require('../../utils/Class');
var GetValue = require('../../utils/object/GetValue');
var Range = require('../../utils/array/Range');
2017-11-02 17:04:02 +00:00
var Set = require('../../structs/Set');
var Sprite = require('../sprite/Sprite');
2017-03-22 23:16:44 +00:00
var Group = new Class({
2017-03-22 23:16:44 +00:00
initialize:
// children can be either an array of children, or a config object
// config can be either a config object, or undefined if passed as the children argument instead
function Group (scene, children, config)
2017-03-22 23:16:44 +00:00
{
if (config === undefined && !Array.isArray(children) && typeof children === 'object')
{
config = children;
children = null;
}
this.scene = scene;
this.children = new Set(children);
this.isParent = true;
this.classType = GetValue(config, 'classType', Sprite);
this.createCallback = GetValue(config, 'createCallback', null);
this.removeCallback = GetValue(config, 'removeCallback', null);
if (config)
{
this.createMultiple(config);
}
2017-03-22 23:16:44 +00:00
},
2017-06-27 15:21:40 +00:00
update: function (time, delta)
{
// Because a Group child may mess with the length of the Group during its update
var temp = this.children.entries.slice();
for (var i = 0; i < temp.length; i++)
{
if (temp[i].update(time, delta) === false)
{
break;
}
}
},
// Group management methods:
2017-03-22 23:16:44 +00:00
add: function (child)
2017-03-23 00:07:41 +00:00
{
this.children.set(child);
2017-03-23 00:07:41 +00:00
if (this.createCallback)
{
this.createCallback.call(this, child);
}
2017-03-23 00:07:41 +00:00
return this;
2017-03-22 23:16:44 +00:00
},
addMultiple: function (children)
2017-03-22 23:16:44 +00:00
{
if (Array.isArray(children))
2017-03-22 23:16:44 +00:00
{
for (var i = 0; i < children.length; i++)
{
this.add(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
},
create: function (x, y, key, frame, visible)
{
if (visible === undefined) { visible = true; }
var child = new this.classType(this.scene, x, y, key, frame);
this.scene.sys.displayList.add(child);
if (child.preUpdate)
{
this.scene.sys.updateList.add(child);
}
child.visible = visible;
this.add(child);
return child;
},
createFromConfig: function (options)
{
this.classType = GetValue(options, 'classType', this.classType);
var key = GetValue(options, 'key', undefined);
var frame = GetValue(options, 'frame', null);
var visible = GetValue(options, 'visible', true);
var entries = [];
// Can't do anything without at least a key
if (key === undefined)
{
return entries;
}
else
{
if (!Array.isArray(key))
{
key = [ key ];
}
if (!Array.isArray(frame))
{
frame = [ frame ];
}
}
// Build an array of key frame pairs to loop through
var repeat = GetValue(options, 'repeat', 0);
var randomKey = GetValue(options, 'randomKey', false);
var randomFrame = GetValue(options, 'randomFrame', false);
var yoyo = GetValue(options, 'yoyo', false);
var quantity = GetValue(options, 'frameQuantity', 1);
var max = GetValue(options, 'max', 0);
// If a grid is set we use that to override the quantity?
var range = Range(key, frame, {
max: max,
qty: quantity,
random: randomKey,
randomB: randomFrame,
repeat: repeat,
yoyo: yoyo
});
for (var c = 0; c < range.length; c++)
{
entries.push(this.create(0, 0, range[c].a, range[c].b, visible));
}
// Post-creation options (applied only to those items created in this call):
2017-03-28 13:01:35 +00:00
var x = GetValue(options, 'setXY.x', 0);
var y = GetValue(options, 'setXY.y', 0);
var stepX = GetValue(options, 'setXY.stepX', 0);
var stepY = GetValue(options, 'setXY.stepY', 0);
2017-03-28 13:01:35 +00:00
Actions.SetXY(entries, x, y, stepX, stepY);
2017-03-28 13:01:35 +00:00
var rotation = GetValue(options, 'setRotation.value', 0);
var stepRotation = GetValue(options, 'setRotation.step', 0);
2017-03-28 13:01:35 +00:00
Actions.SetRotation(entries, rotation, stepRotation);
2017-03-28 13:01:35 +00:00
var scaleX = GetValue(options, 'setScale.x', 1);
var scaleY = GetValue(options, 'setScale.y', scaleX);
var stepScaleX = GetValue(options, 'setScale.stepX', 0);
var stepScaleY = GetValue(options, 'setScale.stepY', 0);
2017-03-28 23:44:08 +00:00
Actions.SetScale(entries, scaleX, scaleY, stepScaleX, stepScaleY);
var alpha = GetValue(options, 'setAlpha.value', 1);
var stepAlpha = GetValue(options, 'setAlpha.step', 0);
2017-03-28 13:01:35 +00:00
Actions.SetAlpha(entries, alpha, stepAlpha);
2017-03-28 13:01:35 +00:00
2017-07-13 01:35:29 +00:00
var hitArea = GetValue(options, 'hitArea', null);
var hitAreaCallback = GetValue(options, 'hitAreaCallback', null);
if (hitArea)
{
Actions.SetHitArea(entries, hitArea, hitAreaCallback);
}
var grid = GetValue(options, 'gridAlign', false);
if (grid)
{
Actions.GridAlign(entries, grid);
}
return entries;
},
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;
},
remove: function (child)
2017-03-23 00:07:41 +00:00
{
this.children.delete(child);
2017-03-23 00:07:41 +00:00
return this;
2017-03-22 23:16:44 +00:00
},
clear: function ()
2017-03-23 00:07:41 +00:00
{
this.children.clear();
2017-03-23 00:07:41 +00:00
return this;
2017-03-22 23:16:44 +00:00
},
contains: function (child)
{
return this.children.contains(child);
},
2017-04-07 14:44:04 +00:00
getChildren: function ()
{
return this.children.entries;
},
getLength: function ()
{
return this.children.size;
},
getFirst: function (compare, index)
{
if (index === undefined) { index = 0; }
return Actions.GetFirst(this.children.entries, compare, index);
},
destroy: function ()
{
this.children.clear();
2017-03-22 23:16:44 +00:00
this.scene = undefined;
this.children = undefined;
},
// Child related methods
angle: function (value)
{
Actions.Angle(this.children.entries, value);
return this;
},
gridAlign: function (options)
{
Actions.GridAlign(this.children.entries, options);
return this;
},
2017-03-28 13:01:35 +00:00
incAlpha: function (value, step)
{
Actions.IncAlpha(this.children.entries, value, step);
return this;
},
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;
},
placeOnCircle: function (circle, startAngle, endAngle)
{
Actions.PlaceOnCircle(this.children.entries, circle, startAngle, endAngle);
return this;
},
placeOnEllipse: function (ellipse, startAngle, endAngle)
{
Actions.PlaceOnEllipse(this.children.entries, ellipse, startAngle, endAngle);
return this;
},
placeOnLine: function (line)
{
Actions.PlaceOnLine(this.children.entries, line);
return this;
},
placeOnRectangle: function (rect, shift)
{
Actions.PlaceOnRectangle(this.children.entries, rect, shift);
return this;
},
2017-03-30 23:46:19 +00:00
placeOnTriangle: function (triangle, stepRate)
{
Actions.PlaceOnTriangle(this.children.entries, triangle, stepRate);
return this;
},
2017-04-08 00:59:44 +00:00
playAnimation: function (key, startFrame)
{
Actions.PlayAnimation(this.children.entries, key, startFrame);
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;
},
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-29 00:03:24 +00:00
Actions.Rotate(this.children.entries, value, step);
return this;
},
rotateAround: function (point, angle)
{
Actions.RotateAround(this.children.entries, point, angle);
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;
},
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;
},
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 13:01:35 +00:00
Actions.SetXY(this.children.entries, x, y, stepX, stepY);
return this;
},
setY: function (value, step)
{
Actions.SetY(this.children.entries, value, step);
return this;
},
setDepth: function (value, step)
{
Actions.SetDepth(this.children.entries, value, step);
return this;
},
shiftPosition: function (x, y, direction, output)
{
Actions.ShiftPosition(this.children.entries, x, y, direction, output);
return this;
},
smootherStep: function (property, min, max, inc)
{
Actions.SmootherStep(this.children.entries, property, min, max, inc);
return this;
},
smoothStep: function (property, min, max, inc)
{
Actions.SmoothStep(this.children.entries, property, min, max, inc);
return this;
},
spread: function (property, min, max, inc)
{
Actions.Spread(this.children.entries, property, min, max, inc);
return this;
},
toggleVisible: function ()
{
Actions.ToggleVisible(this.children.entries);
return this;
}
2017-03-22 23:16:44 +00:00
});
module.exports = Group;