Layer refactoring

This commit is contained in:
Felipe Alfonso 2017-03-22 20:16:44 -03:00
parent 657ea7ffa7
commit 69a34df0ab
3 changed files with 149 additions and 1 deletions

View file

@ -9,6 +9,7 @@ require('./bitmaptext/static/BitmapTextFactory');
require('./bitmaptext/dynamic/DynamicBitmapTextFactory'); require('./bitmaptext/dynamic/DynamicBitmapTextFactory');
require('./graphics/GraphicsFactory'); require('./graphics/GraphicsFactory');
require('./text/static/TextFactory'); require('./text/static/TextFactory');
require('./layer/LayerFactory');
// Phaser.GameObjects // Phaser.GameObjects
@ -23,6 +24,7 @@ module.exports = {
Graphics: require('./graphics/Graphics.js'), Graphics: require('./graphics/Graphics.js'),
Image: require('./image/Image'), Image: require('./image/Image'),
Sprite: require('./sprite/Sprite'), Sprite: require('./sprite/Sprite'),
Text: require('./text/static/Text') Text: require('./text/static/Text'),
Layer: require('./layer/Layer')
}; };

View file

@ -0,0 +1,122 @@
var Class = require('../../utils/Class');
var Layer = new Class({
initialize:
function Layer()
{
this.children = [];
Array.prototype.push.apply(this.children, arguments);
},
add: function (child)
{
var children = this.children;
var index = children.indexOf(child);
if (index < 0)
{
children.push(child);
}
},
addArray: function (childrenArray)
{
var length = childrenArray.length;
for (var index = 0; index < length; ++index)
{
this.add(childrenArray[index]);
}
},
addX: function (value) {
var children = this.children;
var length = children.length;
for (var index = 0; index < length; ++index)
{
children[index].x += value;
}
},
addY: function (value)
{
var children = this.children;
var length = children.length;
for (var index = 0; index < length; ++index)
{
children[index].y += value;
}
},
addPosition: function (x, y)
{
var children = this.children;
var length = children.length;
for (var index = 0; index < length; ++index)
{
children[index].x += x;
children[index].y += y;
}
},
rotate: function (value) {
var children = this.children;
var length = children.length;
for (var index = 0; index < length; ++index)
{
children[index].rotation += value;
}
},
setX: function (value) {
var children = this.children;
var length = children.length;
for (var index = 0; index < length; ++index)
{
children[index].x = value;
}
},
setY: function (value)
{
var children = this.children;
var length = children.length;
for (var index = 0; index < length; ++index)
{
children[index].y = value;
}
},
setRotation: function (value) {
var children = this.children;
var length = children.length;
for (var index = 0; index < length; ++index)
{
children[index].rotation = value;
}
},
setVisible: function (value) {
var children = this.children;
var length = children.length;
for (var index = 0; index < length; ++index)
{
children[index].visible = value;
}
},
toggleVisible: function ()
{
var children = this.children;
var length = children.length;
for (var index = 0; index < length; ++index)
{
children[index].visible = !children[index].visible;
}
}
});
module.exports = Layer;

View file

@ -0,0 +1,24 @@
var Layer = require('./Layer');
var FactoryContainer = require('../../gameobjects/FactoryContainer');
var LayerFactory = {
KEY: 'layer',
add: function ()
{
var layer = new Layer();
layer.addArray(Array.prototype.slice.apply(arguments));
return layer;
},
make: function ()
{
var layer = new Layer();
layer.addArray(Array.prototype.slice.apply(arguments));
return layer;
}
};
module.exports = FactoryContainer.register(LayerFactory);