Skip to content

Commit 8b5d8bd

Browse files
committed
2 parents 8671093 + 626dd7c commit 8b5d8bd

File tree

6 files changed

+203
-14
lines changed

6 files changed

+203
-14
lines changed

src/com/pblabs/engine/core/NameManager.as

+75
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,77 @@ package com.pblabs.engine.core
7474
_objects[object.name] = null;
7575
delete _objects[object.name];
7676
}
77+
78+
unregisterAliases(object);
79+
}
80+
81+
82+
/**
83+
* Register an alias for an IPBObject
84+
* @param object The object to register the alias for
85+
* @param alias The alias to register
86+
**/
87+
public function registerAlias(object:IPBObject, alias:String):void
88+
{
89+
if(alias == null || alias == "")
90+
{
91+
Logger.warn(this, "registerAlias", "Attempt made to register invalid alias '"+alias+"'");
92+
return;
93+
}
94+
95+
if(_objects[alias])
96+
{
97+
Logger.warn(this, "registerAlias","A PBObject with the name '" + alias + "' already exists ");
98+
return;
99+
}
100+
101+
_objects[alias] = object;
102+
103+
//Store the alias in the registeredAliases map
104+
if(!_registeredAliases[object])
105+
_registeredAliases[object] = [];
106+
_registeredAliases[object].push(alias);
107+
}
108+
109+
/**
110+
* Unregister an alias for an IPBObject
111+
* @param object The object to register the alias for.
112+
* @param alias The alias to unregister.
113+
**/
114+
public function unregisterAlias(object:IPBObject, alias:String):void
115+
{
116+
// Check if the alias exists, points to the IPBObject and is an alias.
117+
if(_objects[alias] && _objects[alias] == object
118+
&& _registeredAliases[object] && _registeredAliases[object].indexOf(alias) > -1)
119+
{
120+
_objects[alias] = null;
121+
delete _objects[alias];
122+
123+
_registeredAliases[object].splice(_registeredAliases[object].indexOf(alias), 1);
124+
if(_registeredAliases[object].length < 1)
125+
delete _registeredAliases[object];
126+
}
127+
else
128+
{
129+
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.");
130+
return;
131+
}
132+
}
133+
/**
134+
* Unregister all aliases for an IPBObject
135+
* @param object The IPBObject to unregister.
136+
**/
137+
public function unregisterAliases(object:IPBObject):void{
138+
if(!_registeredAliases[object])
139+
return;
140+
141+
for each(var alias:String in _registeredAliases[object])
142+
{
143+
_objects[alias] = null;
144+
delete _objects[alias];
145+
}
146+
147+
delete _registeredAliases[object];
77148
}
78149

79150
/**
@@ -165,5 +236,9 @@ package com.pblabs.engine.core
165236
}
166237

167238
private var _objects:Dictionary = new Dictionary();
239+
240+
//Map from PBObject -> String[] that contains all registered aliases
241+
private var _registeredAliases:Dictionary = new Dictionary();
242+
168243
}
169244
}

src/com/pblabs/engine/core/TemplateManager.as

+7-4
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,11 @@ package com.pblabs.engine.core
105105
* @param name The name of the entity or template to instantiate. This
106106
* corresponds to the name attribute on the template or entity tag in the XML.
107107
*
108+
* @param entityName optional name to instantiate the entity with if name refers to a template
109+
*
108110
* @return The created entity, or null if it wasn't found.
109111
*/
110-
public function instantiateEntity(name:String):IEntity
112+
public function instantiateEntity(name:String, entityName:String = null):IEntity
111113
{
112114
Profiler.enter("instantiateEntity");
113115

@@ -135,7 +137,7 @@ package com.pblabs.engine.core
135137
return null;
136138
}
137139

138-
var entity:IEntity=instantiateEntityFromXML(xml);
140+
var entity:IEntity=instantiateEntityFromXML(xml, entityName);
139141
Profiler.exit("instantiateEntity");
140142
}
141143
catch (e:Error)
@@ -150,8 +152,9 @@ package com.pblabs.engine.core
150152

151153
/**
152154
* Given an XML literal, construct a valid entity from it.
155+
* @param entityName optional name to instantiate the entity with if the xml is a template
153156
*/
154-
public function instantiateEntityFromXML(xml:XML):IEntity
157+
public function instantiateEntityFromXML(xml:XML, entityName:String = null ):IEntity
155158
{
156159
Profiler.enter("instantiateEntityFromXML");
157160

@@ -160,7 +163,7 @@ package com.pblabs.engine.core
160163
// Get at the name...
161164
var name:String = xml.attribute("name");
162165
if (xml.name() == "template")
163-
name = "";
166+
name = (entityName == null) ? "" : entityName;
164167

165168
// And the alias...
166169
var alias:String=xml.attribute("alias");

src/com/pblabs/rendering2D/AnimationController.as

+15-9
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
package com.pblabs.rendering2D
1010
{
1111
import com.pblabs.engine.PBE;
12-
import com.pblabs.engine.components.AnimatedComponent;
13-
import com.pblabs.engine.core.ProcessManager;
14-
import com.pblabs.engine.debug.Logger;
15-
import com.pblabs.engine.debug.Profiler;
16-
import com.pblabs.engine.entity.PropertyReference;
17-
import com.pblabs.rendering2D.spritesheet.SpriteContainerComponent;
18-
19-
import flash.events.Event;
12+
import com.pblabs.engine.components.AnimatedComponent;
13+
import com.pblabs.engine.core.ProcessManager;
14+
import com.pblabs.engine.debug.Logger;
15+
import com.pblabs.engine.debug.Profiler;
16+
import com.pblabs.engine.entity.PropertyReference;
17+
import com.pblabs.rendering2D.spritesheet.SpriteContainerComponent;
18+
19+
import flash.events.Event;
2020
import flash.utils.Dictionary;
2121

2222
/**
@@ -94,6 +94,12 @@ package com.pblabs.rendering2D
9494
*/
9595
public var changeAnimationEvent:String;
9696

97+
/**
98+
* If true the animation controller always finishes an animation before going
99+
* to the next animation.
100+
*/
101+
public var waitForAnimationsToFinish:Boolean = true;
102+
97103
/**
98104
* Contains the currently playing animation if any.
99105
*/
@@ -139,7 +145,7 @@ package com.pblabs.rendering2D
139145

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

145151
// If we do not have a current animation, start playing the next.

test/src/com/pblabs/PBEngineTestSuite.as

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ package com.pblabs
1717
import com.pblabs.engine.tests.ResourceTests;
1818
import com.pblabs.engine.tests.SanityTests;
1919
import com.pblabs.engine.tests.UtilTests;
20-
import com.pblabs.rendering2D.tests.Rendering2DTests;
20+
import com.pblabs.engine.tests.RegisterAliasTests;
21+
import com.pblabs.engine.tests.InstantiateTemplateWithEntityNameTests;
22+
import com.pblabs.rendering2D.tests.Rendering2DTests;
2123

2224
/**
2325
* @private
@@ -39,5 +41,7 @@ package com.pblabs
3941
public var inputTests:InputTests;
4042
public var entityRegistrationTests:EntityRegistrationTests;
4143
public var groupAndSetTests:GroupAndSetTests;
44+
public var registerAliasTests:RegisterAliasTests;
45+
public var instantiateTemplateWithEntityNameTests:InstantiateTemplateWithEntityNameTests;
4246
}
4347
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.pblabs.engine.tests
2+
{
3+
import com.pblabs.engine.PBE;
4+
import com.pblabs.engine.entity.IEntity;
5+
6+
import flexunit.framework.Assert;
7+
8+
public class InstantiateTemplateWithEntityNameTests
9+
{
10+
11+
public var templateXML:XML = <template name="testTemplate" />;
12+
public var entityXML:XML = <entity name="testEntity" />;
13+
14+
[Test]
15+
public function testInstantiateTemplateWithEntityName():void{
16+
//Register xml templates to test with
17+
PBE.templateManager.addXML(templateXML, "testTemplateXML", 0);
18+
PBE.templateManager.addXML(entityXML, "testEntityXML", 0);
19+
20+
var entity:IEntity;
21+
22+
//Test the default behaviour
23+
entity = PBE.templateManager.instantiateEntity("testTemplate");
24+
Assert.assertEquals("", entity.name);
25+
entity.destroy();
26+
27+
//Test whether the entity name is used upon a template
28+
entity = PBE.templateManager.instantiateEntity("testTemplate", "entityName");
29+
Assert.assertEquals("entityName", entity.name);
30+
entity.destroy();
31+
32+
//Test the default behaviour for an entity
33+
entity = PBE.templateManager.instantiateEntity("testEntity");
34+
Assert.assertEquals("testEntity", entity.name);
35+
entity.destroy();
36+
37+
//Test whether the entityname is not overriden when passing an entityName
38+
entity = PBE.templateManager.instantiateEntity("testEntity", "overrideEntityName");
39+
Assert.assertEquals("testEntity", entity.name);
40+
entity.destroy();
41+
42+
//Cleanup xml
43+
PBE.templateManager.removeXML("testTemplateXML");
44+
PBE.templateManager.removeXML("testEntityXML");
45+
}
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.pblabs.engine.tests
2+
{
3+
import com.pblabs.engine.PBE;
4+
import com.pblabs.engine.entity.IEntity;
5+
import com.pblabs.engine.entity.allocateEntity;
6+
7+
import flexunit.framework.Assert;
8+
9+
import mx.core.UIComponent;
10+
11+
import org.fluint.uiImpersonation.UIImpersonator;
12+
13+
/**
14+
* Unit test for registerAlias and unregisterAlias functionality on NameManager
15+
**/
16+
public class RegisterAliasTests
17+
{
18+
19+
20+
[Test]
21+
public function testRegisterAlias():void{
22+
var entity:IEntity = allocateEntity();
23+
entity.initialize("testEntity");
24+
25+
Assert.assertNull(PBE.nameManager.lookup("testAlias"));
26+
27+
//Register the alias and check if lookups work ok
28+
PBE.nameManager.registerAlias(entity, "testAlias");
29+
PBE.nameManager.registerAlias(entity, "testAlias2");
30+
Assert.assertEquals(entity, PBE.nameManager.lookup("testAlias"));
31+
Assert.assertEquals(entity, PBE.nameManager.lookup("testAlias2"));
32+
33+
//Unregister the alias
34+
PBE.nameManager.unregisterAlias(entity, "testAlias");
35+
Assert.assertNull(PBE.nameManager.lookup("testAlias"));
36+
Assert.assertEquals(entity, PBE.nameManager.lookup("testAlias2"));
37+
PBE.nameManager.unregisterAlias(entity, "testAlias2");
38+
Assert.assertNull(PBE.nameManager.lookup("testAlias2"));
39+
40+
//Try to unregister the entityname, this should not remove the entity registration
41+
PBE.nameManager.unregisterAlias(entity, "testEntity");
42+
Assert.assertEquals(entity, PBE.nameManager.lookup("testEntity"));
43+
44+
//Re-register the alias and destroy the entity
45+
PBE.nameManager.registerAlias(entity, "testAlias");
46+
PBE.nameManager.registerAlias(entity, "testAlias2");
47+
Assert.assertEquals(entity, PBE.nameManager.lookup("testAlias"));
48+
Assert.assertEquals(entity, PBE.nameManager.lookup("testAlias2"));
49+
entity.destroy();
50+
Assert.assertNull(PBE.nameManager.lookup("testAlias"));
51+
Assert.assertNull(PBE.nameManager.lookup("testAlias2"));
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)