Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
natebeck committed Feb 19, 2011
2 parents 8671093 + 626dd7c commit 8b5d8bd
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 14 deletions.
75 changes: 75 additions & 0 deletions src/com/pblabs/engine/core/NameManager.as
Expand Up @@ -74,6 +74,77 @@ package com.pblabs.engine.core
_objects[object.name] = null;
delete _objects[object.name];
}

unregisterAliases(object);
}


/**
* Register an alias for an IPBObject
* @param object The object to register the alias for
* @param alias The alias to register
**/
public function registerAlias(object:IPBObject, alias:String):void
{
if(alias == null || alias == "")
{
Logger.warn(this, "registerAlias", "Attempt made to register invalid alias '"+alias+"'");
return;
}

if(_objects[alias])
{
Logger.warn(this, "registerAlias","A PBObject with the name '" + alias + "' already exists ");
return;
}

_objects[alias] = object;

//Store the alias in the registeredAliases map
if(!_registeredAliases[object])
_registeredAliases[object] = [];
_registeredAliases[object].push(alias);
}

/**
* Unregister an alias for an IPBObject
* @param object The object to register the alias for.
* @param alias The alias to unregister.
**/
public function unregisterAlias(object:IPBObject, alias:String):void
{
// Check if the alias exists, points to the IPBObject and is an alias.
if(_objects[alias] && _objects[alias] == object
&& _registeredAliases[object] && _registeredAliases[object].indexOf(alias) > -1)
{
_objects[alias] = null;
delete _objects[alias];

_registeredAliases[object].splice(_registeredAliases[object].indexOf(alias), 1);
if(_registeredAliases[object].length < 1)
delete _registeredAliases[object];
}
else
{
Logger.warn(this, "unregisterAlias", "Attempt made to unregister alias '"+alias+"' but the object registered is not the same or the alias is not a registered alias.");
return;
}
}
/**
* Unregister all aliases for an IPBObject
* @param object The IPBObject to unregister.
**/
public function unregisterAliases(object:IPBObject):void{
if(!_registeredAliases[object])
return;

for each(var alias:String in _registeredAliases[object])
{
_objects[alias] = null;
delete _objects[alias];
}

delete _registeredAliases[object];
}

/**
Expand Down Expand Up @@ -165,5 +236,9 @@ package com.pblabs.engine.core
}

private var _objects:Dictionary = new Dictionary();

//Map from PBObject -> String[] that contains all registered aliases
private var _registeredAliases:Dictionary = new Dictionary();

}
}
11 changes: 7 additions & 4 deletions src/com/pblabs/engine/core/TemplateManager.as
Expand Up @@ -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");

Expand Down Expand Up @@ -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)
Expand All @@ -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");

Expand All @@ -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");
Expand Down
24 changes: 15 additions & 9 deletions src/com/pblabs/rendering2D/AnimationController.as
Expand Up @@ -9,14 +9,14 @@
package com.pblabs.rendering2D
{
import com.pblabs.engine.PBE;
import com.pblabs.engine.components.AnimatedComponent;
import com.pblabs.engine.core.ProcessManager;
import com.pblabs.engine.debug.Logger;
import com.pblabs.engine.debug.Profiler;
import com.pblabs.engine.entity.PropertyReference;
import com.pblabs.rendering2D.spritesheet.SpriteContainerComponent;

import flash.events.Event;
import com.pblabs.engine.components.AnimatedComponent;
import com.pblabs.engine.core.ProcessManager;
import com.pblabs.engine.debug.Logger;
import com.pblabs.engine.debug.Profiler;
import com.pblabs.engine.entity.PropertyReference;
import com.pblabs.rendering2D.spritesheet.SpriteContainerComponent;

import flash.events.Event;
import flash.utils.Dictionary;

/**
Expand Down Expand Up @@ -94,6 +94,12 @@ package com.pblabs.rendering2D
*/
public var changeAnimationEvent:String;

/**
* If true the animation controller always finishes an animation before going
* to the next animation.
*/
public var waitForAnimationsToFinish:Boolean = true;

/**
* Contains the currently playing animation if any.
*/
Expand Down Expand Up @@ -139,7 +145,7 @@ package com.pblabs.rendering2D

// Expire current animation if it has finished playing and it's what we
// want to keep playing.
if (_currentAnimation !== nextAnim && PBE.processManager.virtualTime > (_currentAnimationStartTime + _currentAnimationDuration))
if (_currentAnimation !== nextAnim && (!waitForAnimationsToFinish || PBE.processManager.virtualTime > (_currentAnimationStartTime + _currentAnimationDuration)))
_currentAnimation = null;

// If we do not have a current animation, start playing the next.
Expand Down
6 changes: 5 additions & 1 deletion test/src/com/pblabs/PBEngineTestSuite.as
Expand Up @@ -17,7 +17,9 @@ package com.pblabs
import com.pblabs.engine.tests.ResourceTests;
import com.pblabs.engine.tests.SanityTests;
import com.pblabs.engine.tests.UtilTests;
import com.pblabs.rendering2D.tests.Rendering2DTests;
import com.pblabs.engine.tests.RegisterAliasTests;
import com.pblabs.engine.tests.InstantiateTemplateWithEntityNameTests;
import com.pblabs.rendering2D.tests.Rendering2DTests;

/**
* @private
Expand All @@ -39,5 +41,7 @@ package com.pblabs
public var inputTests:InputTests;
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");
}
}
}
54 changes: 54 additions & 0 deletions test/src/com/pblabs/engine/tests/RegisterAliasTests.as
@@ -0,0 +1,54 @@
package com.pblabs.engine.tests
{
import com.pblabs.engine.PBE;
import com.pblabs.engine.entity.IEntity;
import com.pblabs.engine.entity.allocateEntity;

import flexunit.framework.Assert;

import mx.core.UIComponent;

import org.fluint.uiImpersonation.UIImpersonator;

/**
* Unit test for registerAlias and unregisterAlias functionality on NameManager
**/
public class RegisterAliasTests
{


[Test]
public function testRegisterAlias():void{
var entity:IEntity = allocateEntity();
entity.initialize("testEntity");

Assert.assertNull(PBE.nameManager.lookup("testAlias"));

//Register the alias and check if lookups work ok
PBE.nameManager.registerAlias(entity, "testAlias");
PBE.nameManager.registerAlias(entity, "testAlias2");
Assert.assertEquals(entity, PBE.nameManager.lookup("testAlias"));
Assert.assertEquals(entity, PBE.nameManager.lookup("testAlias2"));

//Unregister the alias
PBE.nameManager.unregisterAlias(entity, "testAlias");
Assert.assertNull(PBE.nameManager.lookup("testAlias"));
Assert.assertEquals(entity, PBE.nameManager.lookup("testAlias2"));
PBE.nameManager.unregisterAlias(entity, "testAlias2");
Assert.assertNull(PBE.nameManager.lookup("testAlias2"));

//Try to unregister the entityname, this should not remove the entity registration
PBE.nameManager.unregisterAlias(entity, "testEntity");
Assert.assertEquals(entity, PBE.nameManager.lookup("testEntity"));

//Re-register the alias and destroy the entity
PBE.nameManager.registerAlias(entity, "testAlias");
PBE.nameManager.registerAlias(entity, "testAlias2");
Assert.assertEquals(entity, PBE.nameManager.lookup("testAlias"));
Assert.assertEquals(entity, PBE.nameManager.lookup("testAlias2"));
entity.destroy();
Assert.assertNull(PBE.nameManager.lookup("testAlias"));
Assert.assertNull(PBE.nameManager.lookup("testAlias2"));
}
}
}

0 comments on commit 8b5d8bd

Please sign in to comment.