@@ -10,6 +10,8 @@ var log = debug('core:a-entity');
1010var error = debug ( 'core:a-entity:error' ) ;
1111var registerElement = re . registerElement ;
1212
13+ var AEntity ;
14+
1315/**
1416 * Entity element definition.
1517 * Entities represent all elements that are part of the scene, and always have
@@ -25,7 +27,7 @@ var registerElement = re.registerElement;
2527 * @member {object} object3D - three.js object.
2628 * @member {array} states
2729 */
28- var proto = {
30+ var proto = Object . create ( ANode . prototype , {
2931 defaults : {
3032 value : {
3133 position : '' ,
@@ -37,6 +39,7 @@ var proto = {
3739
3840 createdCallback : {
3941 value : function ( ) {
42+ this . isEntity = true ;
4043 this . states = [ ] ;
4144 this . components = { } ;
4245 this . object3D = new THREE . Mesh ( ) ;
@@ -52,7 +55,9 @@ var proto = {
5255 attachedCallback : {
5356 value : function ( ) {
5457 this . addToParent ( ) ;
55- this . load ( ) ;
58+ if ( ! this . isScene ) {
59+ this . load ( ) ;
60+ }
5661 }
5762 } ,
5863
@@ -155,16 +160,18 @@ var proto = {
155160
156161 load : {
157162 value : function ( ) {
158- // To prevent calling load more than once
159163 if ( this . hasLoaded ) { return ; }
160- // Handle to the associated DOM element
161164 this . object3D . el = this ;
162- // It attaches itself to the threejs parent object3D
165+
166+ // Attach to parent object3D.
163167 this . addToParent ( ) ;
164- // Components initialization
165- this . updateComponents ( ) ;
166- // Call the parent class
167- ANode . prototype . load . call ( this ) ;
168+
169+ if ( this . isScene ) {
170+ ANode . prototype . load . call ( this , this . updateComponents . bind ( this ) ) ;
171+ } else {
172+ ANode . prototype . load . call ( this , this . updateComponents . bind ( this ) ,
173+ function ( el ) { return el . isEntity ; } ) ;
174+ }
168175 } ,
169176 writable : window . debug
170177 } ,
@@ -175,6 +182,25 @@ var proto = {
175182 }
176183 } ,
177184
185+ /**
186+ * @returns {array } Direct children that are entities.
187+ */
188+ getChildEntities : {
189+ value : function ( ) {
190+ var children = this . children ;
191+ var childEntities = [ ] ;
192+
193+ for ( var i = 0 ; i < this . children . length ; i ++ ) {
194+ var child = children [ i ] ;
195+ if ( child instanceof AEntity ) {
196+ childEntities . push ( child ) ;
197+ }
198+ }
199+
200+ return childEntities ;
201+ }
202+ } ,
203+
178204 /**
179205 * Check if a component is defined for an entity, including defaults and mixins.
180206 *
@@ -297,7 +323,6 @@ var proto = {
297323 newData = component . parse ( newData ) ;
298324 }
299325 // Component already initialized. Update component.
300- // TODO: update component attribute more granularly.
301326 component . updateAttributes ( newData ) ;
302327 return ;
303328 }
@@ -341,9 +366,10 @@ var proto = {
341366 value : function ( attr , oldVal , newVal ) {
342367 var component = components [ attr ] ;
343368 oldVal = oldVal || this . getAttribute ( attr ) ;
344- // When creating objects programatically and setting attributes, the object is not part
345- // of the scene until is inserted into the DOM.
346- if ( ! this . hasLoaded ) { return ; }
369+ // When creating entities programatically and setting attributes, it is not part
370+ // of the scene until it is inserted into the DOM. This does not apply to scenes as
371+ // scenes depend on its child entities to load.
372+ if ( ! this . hasLoaded && ! this . isScene ) { return ; }
347373 if ( attr === 'mixin' ) {
348374 this . updateStateMixins ( newVal , oldVal ) ;
349375 this . updateComponents ( ) ;
@@ -465,8 +491,9 @@ var proto = {
465491 return is ;
466492 }
467493 }
468- } ;
494+ } ) ;
469495
470- module . exports = registerElement ( 'a-entity' , {
471- prototype : Object . create ( ANode . prototype , proto )
496+ AEntity = registerElement ( 'a-entity' , {
497+ prototype : proto
472498} ) ;
499+ module . exports = AEntity ;
0 commit comments