From 2cb5be5da62d5f0e0495239a0e6e6b9ca4612ca5 Mon Sep 17 00:00:00 2001 From: Bart van de Garde Date: Tue, 21 Dec 2010 22:22:34 +0100 Subject: [PATCH 1/9] Added feature to animationcontroller to wait (or not wait which is new) for animations to finish before switching tot the next animation --- .../pblabs/rendering2D/AnimationController.as | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/com/pblabs/rendering2D/AnimationController.as b/src/com/pblabs/rendering2D/AnimationController.as index 3aee541..4075421 100644 --- a/src/com/pblabs/rendering2D/AnimationController.as +++ b/src/com/pblabs/rendering2D/AnimationController.as @@ -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; /** @@ -93,6 +93,11 @@ package com.pblabs.rendering2D * currentAnimationReference. */ 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. @@ -139,7 +144,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. From e115a5804b2ba03573206bc32238f0acdb01b895 Mon Sep 17 00:00:00 2001 From: Bart van de Garde Date: Tue, 21 Dec 2010 22:48:35 +0100 Subject: [PATCH 2/9] fixed spacing, unaware that tab size of 4 was used and spaces are used --- src/com/pblabs/rendering2D/AnimationController.as | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/com/pblabs/rendering2D/AnimationController.as b/src/com/pblabs/rendering2D/AnimationController.as index 4075421..0a040af 100644 --- a/src/com/pblabs/rendering2D/AnimationController.as +++ b/src/com/pblabs/rendering2D/AnimationController.as @@ -93,11 +93,12 @@ package com.pblabs.rendering2D * currentAnimationReference. */ public var changeAnimationEvent:String; - /** - * If true the animation controller always finishes an animation before going - * to the next animation. - */ - public var waitForAnimationsToFinish:Boolean = true; + + /** + * 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. From ea29a5bebcc780f11442fd1240d2c4b81b68a5b1 Mon Sep 17 00:00:00 2001 From: Bart van de Garde Date: Wed, 22 Dec 2010 11:41:12 +0100 Subject: [PATCH 3/9] Included register alias patch code, needs to be reviewed and tested --- src/com/pblabs/engine/core/NameManager.as | 35 +++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/com/pblabs/engine/core/NameManager.as b/src/com/pblabs/engine/core/NameManager.as index ac96b75..1bfc7b3 100644 --- a/src/com/pblabs/engine/core/NameManager.as +++ b/src/com/pblabs/engine/core/NameManager.as @@ -75,6 +75,41 @@ package com.pblabs.engine.core delete _objects[object.name]; } } + + + /** + * 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 unvalid alias : '"+alias+"'"); + return; + } + + if(_objects[alias]){ + Logger.warn(this, "registerAlias","A PBObject with the name " + alias + " already exists "); + return; + } + + _objects[alias] = object; + } + + /** + * 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{ + if(_objects[alias] && _objects[alias] == object){ + _objects[alias] = null; + delete _objects[alias]; + } else { + Logger.warn(this, "unregisterAlias", "Attempt made to unregister alias '"+alias+"' but the object registered is not the same"); + return; + } + } /** * Looks up a PBObject with the specified name. From 9a84a10ee297718b65acb95c7d9977bf2879e1ca Mon Sep 17 00:00:00 2001 From: Bart van de Garde Date: Wed, 22 Dec 2010 12:06:08 +0100 Subject: [PATCH 4/9] Added patch to use entity name on initializing a template, needs testing --- src/com/pblabs/engine/core/TemplateManager.as | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/com/pblabs/engine/core/TemplateManager.as b/src/com/pblabs/engine/core/TemplateManager.as index 4a80112..34847b6 100644 --- a/src/com/pblabs/engine/core/TemplateManager.as +++ b/src/com/pblabs/engine/core/TemplateManager.as @@ -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 if the xml is based on 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 */ - 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"); From 1666a0f90bbc7208d3f56fa8784134e3e3e78797 Mon Sep 17 00:00:00 2001 From: Bart van de Garde Date: Mon, 3 Jan 2011 11:29:01 +0100 Subject: [PATCH 5/9] Made sure aliases are removed upon destroy of a PBObject, added unit test for registerAlias functionality --- src/com/pblabs/engine/core/NameManager.as | 36 ++++++++++++- test/src/com/pblabs/PBEngineTestSuite.as | 2 + .../pblabs/engine/tests/RegisterAliasTests.as | 54 +++++++++++++++++++ 3 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 test/src/com/pblabs/engine/tests/RegisterAliasTests.as diff --git a/src/com/pblabs/engine/core/NameManager.as b/src/com/pblabs/engine/core/NameManager.as index 1bfc7b3..f6246ab 100644 --- a/src/com/pblabs/engine/core/NameManager.as +++ b/src/com/pblabs/engine/core/NameManager.as @@ -74,6 +74,9 @@ package com.pblabs.engine.core _objects[object.name] = null; delete _objects[object.name]; } + + + unregisterAliasses(object); } @@ -94,6 +97,11 @@ package com.pblabs.engine.core } _objects[alias] = object; + + //Store the alias in the registeredAliases map + if(!_registeredAliases[object]) + _registeredAliases[object] = []; + _registeredAliases[object].push(alias); } /** @@ -102,14 +110,34 @@ package com.pblabs.engine.core * @param alias The alias to unregister **/ public function unregisterAlias(object:IPBObject, alias:String):void{ - if(_objects[alias] && _objects[alias] == object){ + // Check if the alias excists, 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"); + 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 aliasses for an IPBObject + * @param object The IPBObject to unregister al aliasses for + **/ + public function unregisterAliasses(object:IPBObject):void{ + if(!_registeredAliases[object]) + return; + + for each(var alias:String in _registeredAliases[object]){ + _objects[alias] = null; + delete _objects[alias]; + } + delete _registeredAliases[object]; + } /** * Looks up a PBObject with the specified name. @@ -200,5 +228,9 @@ package com.pblabs.engine.core } private var _objects:Dictionary = new Dictionary(); + + //Map from PBObject -> String[] that contains all registered aliasses + private var _registeredAliases:Dictionary = new Dictionary(); + } } \ No newline at end of file diff --git a/test/src/com/pblabs/PBEngineTestSuite.as b/test/src/com/pblabs/PBEngineTestSuite.as index c3cde2b..2c584b2 100644 --- a/test/src/com/pblabs/PBEngineTestSuite.as +++ b/test/src/com/pblabs/PBEngineTestSuite.as @@ -17,6 +17,7 @@ package com.pblabs import com.pblabs.engine.tests.ResourceTests; import com.pblabs.engine.tests.SanityTests; import com.pblabs.engine.tests.UtilTests; + import com.pblabs.engine.tests.RegisterAliasTests; import com.pblabs.rendering2D.tests.Rendering2DTests; /** @@ -39,5 +40,6 @@ package com.pblabs public var inputTests:InputTests; public var entityRegistrationTests:EntityRegistrationTests; public var groupAndSetTests:GroupAndSetTests; + public var registerAliasTests:RegisterAliasTests; } } \ No newline at end of file diff --git a/test/src/com/pblabs/engine/tests/RegisterAliasTests.as b/test/src/com/pblabs/engine/tests/RegisterAliasTests.as new file mode 100644 index 0000000..ec46244 --- /dev/null +++ b/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")); + } + } +} \ No newline at end of file From 10b9dd6aa2a8f3bd6d898197e36e1922b89ce9dc Mon Sep 17 00:00:00 2001 From: Bart van de Garde Date: Mon, 3 Jan 2011 11:34:50 +0100 Subject: [PATCH 6/9] Corrected whitespace and indentation --- src/com/pblabs/engine/core/NameManager.as | 70 +++++++++---------- .../pblabs/engine/tests/RegisterAliasTests.as | 12 ++-- 2 files changed, 41 insertions(+), 41 deletions(-) diff --git a/src/com/pblabs/engine/core/NameManager.as b/src/com/pblabs/engine/core/NameManager.as index f6246ab..420eed2 100644 --- a/src/com/pblabs/engine/core/NameManager.as +++ b/src/com/pblabs/engine/core/NameManager.as @@ -78,52 +78,52 @@ package com.pblabs.engine.core unregisterAliasses(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 unvalid alias : '"+alias+"'"); - return; - } - - if(_objects[alias]){ - Logger.warn(this, "registerAlias","A PBObject with the name " + alias + " already exists "); - return; - } - - _objects[alias] = 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 unvalid 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{ + } + + /** + * 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 excists, 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]; + 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; - } - } + } 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 aliasses for an IPBObject * @param object The IPBObject to unregister al aliasses for diff --git a/test/src/com/pblabs/engine/tests/RegisterAliasTests.as b/test/src/com/pblabs/engine/tests/RegisterAliasTests.as index ec46244..e256d58 100644 --- a/test/src/com/pblabs/engine/tests/RegisterAliasTests.as +++ b/test/src/com/pblabs/engine/tests/RegisterAliasTests.as @@ -9,14 +9,14 @@ package com.pblabs.engine.tests import mx.core.UIComponent; import org.fluint.uiImpersonation.UIImpersonator; - + /** - * Unit test for registerAlias and unregisterAlias functionality on NameManager - **/ + * Unit test for registerAlias and unregisterAlias functionality on NameManager + **/ public class RegisterAliasTests { - + [Test] public function testRegisterAlias():void{ var entity:IEntity = allocateEntity(); @@ -29,14 +29,14 @@ package com.pblabs.engine.tests 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")); From 4b3fb3a47bec6a78a1481ef6f3dc820cc157e254 Mon Sep 17 00:00:00 2001 From: Bart van de Garde Date: Mon, 3 Jan 2011 11:57:28 +0100 Subject: [PATCH 7/9] Corrected documentation --- src/com/pblabs/engine/core/TemplateManager.as | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/com/pblabs/engine/core/TemplateManager.as b/src/com/pblabs/engine/core/TemplateManager.as index 34847b6..0b18e70 100644 --- a/src/com/pblabs/engine/core/TemplateManager.as +++ b/src/com/pblabs/engine/core/TemplateManager.as @@ -105,7 +105,7 @@ 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 if the xml is based on a template + * @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. */ @@ -152,7 +152,7 @@ package com.pblabs.engine.core /** * Given an XML literal, construct a valid entity from it. - * @param entityName optional name to instantiate the entity with + * @param entityName optional name to instantiate the entity with if the xml is a template */ public function instantiateEntityFromXML(xml:XML, entityName:String = null ):IEntity { From 4c1f617d913a7cb87ecd5970ade55a20d0a80c19 Mon Sep 17 00:00:00 2001 From: Bart van de Garde Date: Mon, 3 Jan 2011 12:09:34 +0100 Subject: [PATCH 8/9] Added unit test for instantiating a template with an entity name --- test/src/com/pblabs/PBEngineTestSuite.as | 2 + .../InstantiateTemplateWithEntityNameTests.as | 47 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 test/src/com/pblabs/engine/tests/InstantiateTemplateWithEntityNameTests.as diff --git a/test/src/com/pblabs/PBEngineTestSuite.as b/test/src/com/pblabs/PBEngineTestSuite.as index c3cde2b..9168e8b 100644 --- a/test/src/com/pblabs/PBEngineTestSuite.as +++ b/test/src/com/pblabs/PBEngineTestSuite.as @@ -17,6 +17,7 @@ package com.pblabs import com.pblabs.engine.tests.ResourceTests; import com.pblabs.engine.tests.SanityTests; import com.pblabs.engine.tests.UtilTests; + import com.pblabs.engine.tests.InstantiateTemplateWithEntityNameTests; import com.pblabs.rendering2D.tests.Rendering2DTests; /** @@ -39,5 +40,6 @@ package com.pblabs public var inputTests:InputTests; public var entityRegistrationTests:EntityRegistrationTests; public var groupAndSetTests:GroupAndSetTests; + public var instantiateTemplateWithEntityNameTests:InstantiateTemplateWithEntityNameTests; } } \ No newline at end of file diff --git a/test/src/com/pblabs/engine/tests/InstantiateTemplateWithEntityNameTests.as b/test/src/com/pblabs/engine/tests/InstantiateTemplateWithEntityNameTests.as new file mode 100644 index 0000000..c8b0f3f --- /dev/null +++ b/test/src/com/pblabs/engine/tests/InstantiateTemplateWithEntityNameTests.as @@ -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 =