From 69a34df0ab4af4375ad1bd6e7f7fa085cfcdb135 Mon Sep 17 00:00:00 2001 From: Felipe Alfonso Date: Wed, 22 Mar 2017 20:16:44 -0300 Subject: [PATCH] Layer refactoring --- v3/src/gameobjects/index.js | 4 +- v3/src/gameobjects/layer/Layer.js | 122 +++++++++++++++++++++++ v3/src/gameobjects/layer/LayerFactory.js | 24 +++++ 3 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 v3/src/gameobjects/layer/Layer.js create mode 100644 v3/src/gameobjects/layer/LayerFactory.js diff --git a/v3/src/gameobjects/index.js b/v3/src/gameobjects/index.js index 483b8dec6..f9147c2c1 100644 --- a/v3/src/gameobjects/index.js +++ b/v3/src/gameobjects/index.js @@ -9,6 +9,7 @@ require('./bitmaptext/static/BitmapTextFactory'); require('./bitmaptext/dynamic/DynamicBitmapTextFactory'); require('./graphics/GraphicsFactory'); require('./text/static/TextFactory'); +require('./layer/LayerFactory'); // Phaser.GameObjects @@ -23,6 +24,7 @@ module.exports = { Graphics: require('./graphics/Graphics.js'), Image: require('./image/Image'), Sprite: require('./sprite/Sprite'), - Text: require('./text/static/Text') + Text: require('./text/static/Text'), + Layer: require('./layer/Layer') }; diff --git a/v3/src/gameobjects/layer/Layer.js b/v3/src/gameobjects/layer/Layer.js new file mode 100644 index 000000000..242710e9f --- /dev/null +++ b/v3/src/gameobjects/layer/Layer.js @@ -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; \ No newline at end of file diff --git a/v3/src/gameobjects/layer/LayerFactory.js b/v3/src/gameobjects/layer/LayerFactory.js new file mode 100644 index 000000000..e78aa5dd0 --- /dev/null +++ b/v3/src/gameobjects/layer/LayerFactory.js @@ -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);