88var Class = require ( '../../utils/Class' ) ;
99var Components = require ( '../components' ) ;
1010var GameObject = require ( '../GameObject' ) ;
11+ var List = require ( '../../structs/List' ) ;
1112var Render = require ( './ContainerRender' ) ;
1213var Vector2 = require ( '../../math/Vector2' ) ;
1314
@@ -23,10 +24,12 @@ var Vector2 = require('../../math/Vector2');
2324 *
2425 * @extends Phaser.GameObjects.Components.BlendMode
2526 * @extends Phaser.GameObjects.Components.Transform
27+ * @extends Phaser.Structs.List
2628 *
2729 * @param {Phaser.Scene } scene - The Scene to which this Game Object belongs. A Game Object can only belong to one Scene at a time.
28- * @param {number } x - The horizontal position of this Game Object in the world.
29- * @param {number } y - The vertical position of this Game Object in the world.
30+ * @param {number } [x=0] - The horizontal position of this Game Object in the world.
31+ * @param {number } [y=0] - The vertical position of this Game Object in the world.
32+ * @param {Phaser.GameObjects.GameObject[] } [children] - An optional array of Game Objects to add to this Container.
3033 */
3134var Container = new Class ( {
3235
@@ -35,32 +38,52 @@ var Container = new Class({
3538 Mixins : [
3639 Components . BlendMode ,
3740 Components . Transform ,
38- Render
41+ Render ,
42+ List
3943 ] ,
4044
4145 initialize :
4246
43- function Container ( scene , x , y )
47+ function Container ( scene , x , y , children )
4448 {
4549 GameObject . call ( this , scene , 'Container' ) ;
4650
4751 /**
48- * [description]
52+ * The parent property as used by the List methods.
4953 *
50- * @name Phaser.GameObjects.Container#parentContainer
54+ * @name Phaser.GameObjects.Container#parent
5155 * @type {Phaser.GameObjects.Container }
56+ * @private
5257 * @since 3.4.0
5358 */
54- this . parentContainer = null ;
59+ this . parent = this ;
5560
5661 /**
57- * [description]
62+ * An array holding the children of this Container.
5863 *
59- * @name Phaser.GameObjects.Container#children
64+ * @name Phaser.GameObjects.Container#list
6065 * @type {Phaser.GameObjects.GameObject[] }
6166 * @since 3.4.0
6267 */
63- this . children = [ ] ;
68+ this . list = [ ] ;
69+
70+ /**
71+ * The cursor position.
72+ *
73+ * @name Phaser.GameObjects.Container#position
74+ * @type {integer }
75+ * @since 3.4.0
76+ */
77+ this . position = 0 ;
78+
79+ /**
80+ * The parent Container of this Container, if there is one.
81+ *
82+ * @name Phaser.GameObjects.Container#parentContainer
83+ * @type {Phaser.GameObjects.Container }
84+ * @since 3.4.0
85+ */
86+ this . parentContainer = null ;
6487
6588 /**
6689 * [description]
@@ -76,73 +99,77 @@ var Container = new Class({
7699 *
77100 * @name Phaser.GameObjects.Container#tempTransformMatrix
78101 * @type {Phaser.GameObjects.Components.TransformMatrix }
102+ * @private
79103 * @since 3.4.0
80104 */
81105 this . tempTransformMatrix = new Components . TransformMatrix ( ) ;
82106
107+ /**
108+ * A callback that is invoked every time a child is added to this list.
109+ *
110+ * @name Phaser.GameObjects.Container#addCallback
111+ * @type {function }
112+ * @private
113+ * @since 3.4.0
114+ */
115+ this . addCallback = this . addHandler ;
116+
117+ /**
118+ * A callback that is invoked every time a child is removed from this list.
119+ *
120+ * @name Phaser.GameObjects.Container#removeCallback
121+ * @type {function }
122+ * @private
123+ * @since 3.4.0
124+ */
125+ this . removeCallback = this . removeHandler ;
126+
83127 this . setPosition ( x , y ) ;
128+
129+ if ( Array . isArray ( children ) )
130+ {
131+ this . addMultiple ( children ) ;
132+ }
84133 } ,
85134
86135 /**
87- * Adds the given Game Object, or array of Game Objects, to this Container .
136+ * Internal add handler .
88137 *
89- * @method Phaser.GameObjects.Container#add
138+ * @method Phaser.GameObjects.Container#addHandler
139+ * @private
90140 * @since 3.4.0
91141 *
92- * @param {(Phaser.GameObjects.GameObject|Phaser.GameObjects.GameObject[]) } gameObject - The Game Object, or array of Game Objects, to add to this Container.
93- *
94- * @return {Phaser.GameObjects.Container } This Container instance.
142+ * @param {Phaser.GameObjects.Container } list - The List the Game Object was added to (also this Container)
143+ * @param {Phaser.GameObjects.GameObject } gameObject - The Game Object that was just added to this Container.
95144 */
96- add : function ( gameObject )
145+ addHandler : function ( list , gameObject )
97146 {
98- if ( ! Array . isArray ( gameObject ) )
99- {
100- gameObject = [ gameObject ] ;
101- }
147+ gameObject . on ( 'destroy' , this . remove , this ) ;
102148
103- for ( var i = 0 ; i < gameObject . length ; i ++ )
149+ if ( gameObject . type === 'Container' )
104150 {
105- var entry = gameObject [ i ] ;
106-
107- if ( entry . type === 'Container' )
108- {
109- entry . parentContainer = this ;
110- }
111-
112- if ( this . children . indexOf ( entry ) === - 1 )
113- {
114- this . children . push ( entry ) ;
115- }
151+ gameObject . parentContainer = list ;
116152 }
117-
118- return this ;
119153 } ,
120154
121155 /**
122- * Removes a Game Object from this Container .
156+ * Internal remove handler .
123157 *
124- * @method Phaser.GameObjects.Container#remove
158+ * @method Phaser.GameObjects.Container#removeHandler
159+ * @private
125160 * @since 3.4.0
126161 *
127- * @param {Phaser.GameObjects.GameObject } gameObject - The Game Object to remove from this Container.
128- *
129- * @return {Phaser.GameObjects.Container } This Container instance.
162+ * @param {Phaser.GameObjects.Container } list - The List the Game Object was removed from (also this Container)
163+ * @param {Phaser.GameObjects.GameObject } gameObject - The Game Object that was just removed from this Container.
130164 */
131- remove : function ( gameObject )
165+ removeHandler : function ( list , gameObject )
132166 {
133- var index = this . children . indexOf ( gameObject ) ;
167+ gameObject . off ( 'destroy' , list . remove , this ) ;
134168
135- if ( index !== - 1 )
169+ if ( gameObject . type === 'Container' )
136170 {
137- if ( gameObject . type === 'Container' )
138- {
139- gameObject . parentContainer = null ;
140- }
141-
142- this . children . splice ( index , 1 ) ;
171+ gameObject . parentContainer = null ;
143172 }
144-
145- return this ;
146173 } ,
147174
148175 /**
@@ -176,6 +203,90 @@ var Container = new Class({
176203 tempMatrix . transformPoint ( source . x , source . y , output ) ;
177204
178205 return output ;
206+ } ,
207+
208+ /**
209+ *
210+ *
211+ * @method Phaser.GameObjects.Container#localToWorld
212+ * @since 3.4.0
213+ *
214+ * @param {Phaser.GameObjects.GameObject } child - The child of this Container.
215+ * @param {Phaser.Cameras.Scene2D.Camera } [camera] - The camera to transform against.
216+ * @param {(object|Phaser.Geom.Point|Phaser.Math.Vector2) } [output] - A destination object to store the transformed point in. If none given a Vector2 will be created and returned.
217+ *
218+ * @return {(object|Phaser.Geom.Point|Phaser.Math.Vector2) } The transformed point.
219+ */
220+ localToWorld : function ( child , camera , output )
221+ {
222+ if ( camera === undefined ) { camera = this . scene . sys . cameras . main ; }
223+ if ( output === undefined ) { output = new Vector2 ( ) ; }
224+
225+ if ( this . exists ( child ) )
226+ {
227+ // Do matrix magic here
228+ // See: Camera.getWorldPoint and InputManager.hitTest
229+ }
230+
231+ return output ;
232+ } ,
233+
234+ /**
235+ * Destroys this Container removing it and all children from the Display List and
236+ * severing all ties to parent resources.
237+ *
238+ * Use this to remove a Container from your game if you don't ever plan to use it again.
239+ * As long as no reference to it exists within your own code it should become free for
240+ * garbage collection by the browser.
241+ *
242+ * If you just want to temporarily disable an object then look at using the
243+ * Game Object Pool instead of destroying it, as destroyed objects cannot be resurrected.
244+ *
245+ * @method Phaser.GameObjects.Container#destroy
246+ * @since 3.4.0
247+ */
248+ destroy : function ( )
249+ {
250+ // This Game Object had already been destroyed
251+ if ( ! this . scene )
252+ {
253+ return ;
254+ }
255+
256+ if ( this . preDestroy )
257+ {
258+ this . preDestroy . call ( this ) ;
259+ }
260+
261+ this . emit ( 'destroy' , this ) ;
262+
263+ var sys = this . scene . sys ;
264+
265+ sys . displayList . remove ( this ) ;
266+
267+ if ( this . data )
268+ {
269+ this . data . destroy ( ) ;
270+
271+ this . data = undefined ;
272+ }
273+
274+ // Tell the Scene to re-sort the children
275+ sys . queueDepthSort ( ) ;
276+
277+ this . active = false ;
278+ this . visible = false ;
279+
280+ this . scene = undefined ;
281+
282+ this . removeAllListeners ( ) ;
283+
284+ this . removeAll ( ) ;
285+
286+ this . list = [ ] ;
287+
288+ this . parent = null ;
289+ this . parentContainer = null ;
179290 }
180291
181292} ) ;
0 commit comments