Permalink
Browse files

Merge branch 'bgarde-instantiate_template_with_entity_name'

  • Loading branch information...
2 parents d446f3b + e92bec0 commit 626dd7c5e979e178f8bf7a9ff97ab0df000ec0f2 @bengarney bengarney committed Jan 11, 2011
@@ -105,9 +105,11 @@ package com.pblabs.engine.core
* @param name The name of the entity or template to instantiate. This
* corresponds to the name attribute on the template or entity tag in the XML.
*
+ * @param entityName optional name to instantiate the entity with if name refers to a template
+ *
* @return The created entity, or null if it wasn't found.
*/
- public function instantiateEntity(name:String):IEntity
+ public function instantiateEntity(name:String, entityName:String = null):IEntity
{
Profiler.enter("instantiateEntity");
@@ -135,7 +137,7 @@ package com.pblabs.engine.core
return null;
}
- var entity:IEntity=instantiateEntityFromXML(xml);
+ var entity:IEntity=instantiateEntityFromXML(xml, entityName);
Profiler.exit("instantiateEntity");
}
catch (e:Error)
@@ -150,8 +152,9 @@ package com.pblabs.engine.core
/**
* Given an XML literal, construct a valid entity from it.
+ * @param entityName optional name to instantiate the entity with if the xml is a template
*/
- public function instantiateEntityFromXML(xml:XML):IEntity
+ public function instantiateEntityFromXML(xml:XML, entityName:String = null ):IEntity
{
Profiler.enter("instantiateEntityFromXML");
@@ -160,7 +163,7 @@ package com.pblabs.engine.core
// Get at the name...
var name:String = xml.attribute("name");
if (xml.name() == "template")
- name = "";
+ name = (entityName == null) ? "" : entityName;
// And the alias...
var alias:String=xml.attribute("alias");
@@ -18,7 +18,8 @@ package com.pblabs
import com.pblabs.engine.tests.SanityTests;
import com.pblabs.engine.tests.UtilTests;
import com.pblabs.engine.tests.RegisterAliasTests;
- import com.pblabs.rendering2D.tests.Rendering2DTests;
+ import com.pblabs.engine.tests.InstantiateTemplateWithEntityNameTests;
+ import com.pblabs.rendering2D.tests.Rendering2DTests;
/**
* @private
@@ -41,5 +42,6 @@ package com.pblabs
public var entityRegistrationTests:EntityRegistrationTests;
public var groupAndSetTests:GroupAndSetTests;
public var registerAliasTests:RegisterAliasTests;
+ public var instantiateTemplateWithEntityNameTests:InstantiateTemplateWithEntityNameTests;
}
}
@@ -0,0 +1,47 @@
+package com.pblabs.engine.tests
+{
+ import com.pblabs.engine.PBE;
+ import com.pblabs.engine.entity.IEntity;
+
+ import flexunit.framework.Assert;
+
+ public class InstantiateTemplateWithEntityNameTests
+ {
+
+ public var templateXML:XML = <template name="testTemplate" />;
+ public var entityXML:XML = <entity name="testEntity" />;
+
+ [Test]
+ public function testInstantiateTemplateWithEntityName():void{
+ //Register xml templates to test with
+ PBE.templateManager.addXML(templateXML, "testTemplateXML", 0);
+ PBE.templateManager.addXML(entityXML, "testEntityXML", 0);
+
+ var entity:IEntity;
+
+ //Test the default behaviour
+ entity = PBE.templateManager.instantiateEntity("testTemplate");
+ Assert.assertEquals("", entity.name);
+ entity.destroy();
+
+ //Test whether the entity name is used upon a template
+ entity = PBE.templateManager.instantiateEntity("testTemplate", "entityName");
+ Assert.assertEquals("entityName", entity.name);
+ entity.destroy();
+
+ //Test the default behaviour for an entity
+ entity = PBE.templateManager.instantiateEntity("testEntity");
+ Assert.assertEquals("testEntity", entity.name);
+ entity.destroy();
+
+ //Test whether the entityname is not overriden when passing an entityName
+ entity = PBE.templateManager.instantiateEntity("testEntity", "overrideEntityName");
+ Assert.assertEquals("testEntity", entity.name);
+ entity.destroy();
+
+ //Cleanup xml
+ PBE.templateManager.removeXML("testTemplateXML");
+ PBE.templateManager.removeXML("testEntityXML");
+ }
+ }
+}

0 comments on commit 626dd7c

Please sign in to comment.