From 11aa76863dce7afe6b5c6beaff1b680814c71090 Mon Sep 17 00:00:00 2001 From: Nate Beck Date: Thu, 23 Dec 2010 20:07:27 -0800 Subject: [PATCH 1/2] Adding in the new PBE namespace so users of PBE aren't confused by methods we don't actually want to support or exposed in the API. --- src/com/pblabs/engine/ns_pbe.as | 4 ++++ src/com/pblabs/engine/resource/ResourceManager.as | 13 ++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 src/com/pblabs/engine/ns_pbe.as diff --git a/src/com/pblabs/engine/ns_pbe.as b/src/com/pblabs/engine/ns_pbe.as new file mode 100644 index 00000000..64e1b08b --- /dev/null +++ b/src/com/pblabs/engine/ns_pbe.as @@ -0,0 +1,4 @@ +package com.pblabs.engine +{ + public namespace ns_pbe = "http://www.pblabs.com/2010"; +} diff --git a/src/com/pblabs/engine/resource/ResourceManager.as b/src/com/pblabs/engine/resource/ResourceManager.as index 0af99993..aad82ce6 100644 --- a/src/com/pblabs/engine/resource/ResourceManager.as +++ b/src/com/pblabs/engine/resource/ResourceManager.as @@ -11,13 +11,14 @@ package com.pblabs.engine.resource import com.pblabs.engine.PBE; import com.pblabs.engine.PBUtil; import com.pblabs.engine.debug.Logger; + import com.pblabs.engine.ns_pbe; import com.pblabs.engine.resource.provider.EmbeddedResourceProvider; import com.pblabs.engine.resource.provider.FallbackResourceProvider; import com.pblabs.engine.resource.provider.IResourceProvider; import com.pblabs.engine.serialization.TypeUtility; import flash.events.Event; - import flash.utils.Dictionary; + import flash.utils.Dictionary; /** * The resource manager handles all tasks related to using asset files (images, xml, etc) @@ -39,7 +40,7 @@ package com.pblabs.engine.resource * as via a dialog box ;). Passed the filename of the requested resource. */ public var onEmbeddedFail:Function; - + /** * Loads a resource from a file. If the resource has already been loaded or is embedded, a * reference to the existing resource will be given. The resource is not returned directly @@ -240,7 +241,7 @@ package com.pblabs.engine.resource var resourceIdentifier:String = filename.toLowerCase() + resourceType; return _resources[resourceIdentifier]; } - + /** * Properly mark a resource as failed-to-load. */ @@ -263,5 +264,11 @@ package com.pblabs.engine.resource * List of resource providers used to get resources. */ private var resourceProviders:Array = new Array(); + + /*** Helper methods for PBE not externally exposed ***/ + ns_pbe function getResources():Dictionary + { + return _resources; + } } } From 98f766112b20893a0b831a7144a41507fe6d582f Mon Sep 17 00:00:00 2001 From: Nate Beck Date: Sat, 25 Dec 2010 15:31:29 -0800 Subject: [PATCH 2/2] Fixing spacing --- src/com/pblabs/engine/entity/Entity.as | 214 ++++++++++++------------- 1 file changed, 106 insertions(+), 108 deletions(-) diff --git a/src/com/pblabs/engine/entity/Entity.as b/src/com/pblabs/engine/entity/Entity.as index 27c6afff..6795b152 100644 --- a/src/com/pblabs/engine/entity/Entity.as +++ b/src/com/pblabs/engine/entity/Entity.as @@ -9,35 +9,33 @@ package com.pblabs.engine.entity { import com.pblabs.engine.PBE; - import com.pblabs.engine.core.NameManager; import com.pblabs.engine.core.PBObject; import com.pblabs.engine.core.PBSet; - import com.pblabs.engine.core.TemplateManager; import com.pblabs.engine.debug.Logger; import com.pblabs.engine.debug.Profiler; import com.pblabs.engine.serialization.Serializer; import com.pblabs.engine.serialization.TypeUtility; - + import flash.events.Event; import flash.events.EventDispatcher; import flash.events.IEventDispatcher; import flash.utils.Dictionary; import flash.utils.getQualifiedClassName; - + /** * Default implementation of IEntity. - * + * *

Please use allocateEntity() to get at instances of Entity; this allows * us to pool Entities at a later date if needed and do other tricks. Please * program against IEntity, not Entity, to avoid dependencies.

*/ internal class Entity extends PBObject implements IEntity - { + { public function get deferring():Boolean { return _deferring; } - + public function set deferring(value:Boolean):void { if(_deferring == true && value == false) @@ -49,33 +47,33 @@ package com.pblabs.engine.entity var pc:PendingComponent = _deferredComponents.shift() as PendingComponent; pc.item.register(this, pc.name); } - + // Mark deferring as done. _deferring = false; - + // Fire off the reset. if(needReset) - doResetComponents(); + doResetComponents(); } - + _deferring = value; } - + public function get eventDispatcher():IEventDispatcher { return _eventDispatcher; } - + public override function initialize(name:String = null, alias:String = null):void - { + { // Pass control up. super.initialize(name, alias); // Resolve any pending components. deferring = false; } - - + + /** * Destroys the Entity by removing all components and unregistering it from * the name manager. @@ -87,14 +85,14 @@ package com.pblabs.engine.entity // Give listeners a chance to act before we start destroying stuff. if(_eventDispatcher.hasEventListener("EntityDestroyed")) _eventDispatcher.dispatchEvent(new Event("EntityDestroyed")); - + // Unregister our components. for each(var component:IEntityComponent in _components) { if(component.isRegistered) - component.unregister(); + component.unregister(); } - + // And remove their references from the dictionary. for (var name:String in _components) delete _components[name]; @@ -102,7 +100,7 @@ package com.pblabs.engine.entity // Get out of the NameManager and other general cleanup stuff. super.destroy(); } - + /** * Serializes an entity. Pass in the current XML stream, and it automatically * adds itself to it. @@ -112,18 +110,18 @@ package com.pblabs.engine.entity { var entityXML:XML = ; if(alias!=null) - entityXML = ; - + entityXML = ; + for each (var component:IEntityComponent in _components) - { + { var componentXML:XML = ; Serializer.instance.serialize(component, componentXML); entityXML.appendChild(componentXML); } - xml.appendChild(entityXML); + xml.appendChild(entityXML); } - + public function deserialize(xml:XML, registerComponents:Boolean = true):void { // Note what entity we're deserializing to the Serializer. @@ -132,7 +130,7 @@ package com.pblabs.engine.entity // Push the deferred state. var oldDefer:Boolean = deferring; deferring = true; - + // Process each component tag in the xml. for each (var componentXML:XML in xml.*) { @@ -142,11 +140,11 @@ package com.pblabs.engine.entity Logger.error(this, "deserialize", "Found unexpected tag '" + componentXML.name().toString() + "', only is valid, ignoring tag. Error in entity '" + name + "'."); continue; } - + var componentName:String = componentXML.attribute("name"); var componentClassName:String = componentXML.attribute("type"); var component:IEntityComponent = null; - + if (componentClassName.length > 0) { // If it specifies a type, instantiate a component and add it. @@ -156,7 +154,7 @@ package com.pblabs.engine.entity Logger.error(this, "deserialize", "Unable to instantiate component " + componentName + " of type " + componentClassName + " on entity '" + name + "'."); continue; } - + if (!addComponent(component, componentName)) continue; } @@ -170,11 +168,11 @@ package com.pblabs.engine.entity continue; } } - + // Deserialize the XML into the component. Serializer.instance.deserialize(component, componentXML); } - + // Deal with set membership. var setsAttr:String = xml.attribute("sets"); if (setsAttr) @@ -188,7 +186,7 @@ package com.pblabs.engine.entity while (thisName = setNames.pop()) { var pbset:PBSet = PBE.lookup(thisName) as PBSet; - if (!pbset) + if (!pbset) { // Set doesn't exist, create a new one. pbset = new PBSet(); @@ -198,12 +196,12 @@ package com.pblabs.engine.entity pbset.add(this as PBObject); } } - } - + } + // Restore deferred state. deferring = oldDefer; } - + public function addComponent(component:IEntityComponent, componentName:String):Boolean { // Add it to the dictionary. @@ -222,13 +220,13 @@ package com.pblabs.engine.entity // We have to be careful w.r.t. adding components from another component. component.register(this, componentName); - + // Fire off the reset. doResetComponents(); - + return true; } - + public function removeComponent(component:IEntityComponent):void { // Update the dictionary. @@ -243,21 +241,21 @@ package com.pblabs.engine.entity { if((_deferredComponents[i] as PendingComponent).item != component) continue; - + // TODO: Forcibly call register/unregister to ensure onAdd/onRemove semantics? - + _deferredComponents.splice(i, 1); break; } return; } - + component.unregister(); - + doResetComponents(); } - + public function lookupComponentByType(componentType:Class):IEntityComponent { for each(var component:IEntityComponent in _components) @@ -265,86 +263,86 @@ package com.pblabs.engine.entity if (component is componentType) return component; } - + return null; } - + public function lookupComponentsByType(componentType:Class):Array { var list:Array = new Array(); - + for each(var component:IEntityComponent in _components) { if (component is componentType) list.push(component); } - + return list; } - + public function lookupComponentByName(componentName:String):IEntityComponent { return _components[componentName]; } - + public function doesPropertyExist(property:PropertyReference):Boolean { return findProperty(property, false, _tempPropertyInfo, true) != null; } - + public function getProperty(property:PropertyReference, defaultVal:* = null):* { // Look up the property. var info:PropertyInfo = findProperty(property, false, _tempPropertyInfo); var result:* = null; - + // Get value if any. if (info) result = info.getValue(); else - result = defaultVal; - + result = defaultVal; + // Clean up to avoid dangling references. _tempPropertyInfo.clear(); - + return result; } - + public function setProperty(property:PropertyReference, value:*):void { // Look up and set. var info:PropertyInfo = findProperty(property, true, _tempPropertyInfo); if (info) info.setValue(value); - + // Clean up to avoid dangling references. _tempPropertyInfo.clear(); } - + private function doAddComponent(component:IEntityComponent, componentName:String):Boolean { if (componentName == "") { Logger.warn(this, "AddComponent", "A component name was not specified. This might cause problems later."); } - + if (component.owner) { Logger.error(this, "AddComponent", "The component " + componentName + " already has an owner. (" + name + ")"); return false; } - + if (_components[componentName]) { Logger.error(this, "AddComponent", "A component with name " + componentName + " already exists on this entity (" + name + ")."); return false; } - + component.owner = this; _components[componentName] = component; return true; } - + private function doRemoveComponent(component:IEntityComponent):Boolean { if (component.owner != this) @@ -352,17 +350,17 @@ package com.pblabs.engine.entity Logger.error(this, "AddComponent", "The component " + component.name + " is not owned by this entity. (" + name + ")"); return false; } - + if (!_components[component.name]) { Logger.error(this, "AddComponent", "The component " + component.name + " was not found on this entity. (" + name + ")"); return false; } - + delete _components[component.name]; return true; } - + /** * Call reset on all the registered components in this entity. */ @@ -375,27 +373,27 @@ package com.pblabs.engine.entity // Skip unregistered entities. if(!component.isRegistered) continue; - + // Reset it! - component.reset(); + component.reset(); } deferring = false; } - + private function findProperty(reference:PropertyReference, willSet:Boolean = false, providedPi:PropertyInfo = null, suppressErrors:Boolean = false):PropertyInfo { // TODO: we use appendChild but relookup the results, can we just use return value? - + // Early out if we got a null property reference. if (!reference || reference.property == null || reference.property == "") return null; - + Profiler.enter("Entity.findProperty"); - + // Must have a propertyInfo to operate with. if(!providedPi) providedPi = new PropertyInfo(); - + // Cached lookups apply only to components. if(reference.cachedLookup && reference.cachedLookup.length > 0) { @@ -408,11 +406,11 @@ package com.pblabs.engine.entity Profiler.exit("Entity.findProperty"); return null; } - + for(var i:int = 1; i 1) ? cl[cl.length-1] : null; Profiler.exit("Entity.findProperty"); return cachedPi; } - + // Split up the property reference. var propertyName:String = reference.property; var path:Array = propertyName.split("."); - + // Distinguish if it is a component reference (@), named object ref (#), or // an XML reference (!), and look up the first element in the path. var isTemplateXML:Boolean = false; @@ -447,12 +445,12 @@ package com.pblabs.engine.entity parentElem = lookupComponentByName(curLookup); if(!parentElem) { - if(!suppressErrors) - Logger.warn(this, "findProperty", "[#"+this.name+"] Could not resolve component named '" + curLookup + "' for property '" + reference.property + "'"); + if(!suppressErrors) + Logger.warn(this, "findProperty", "[#"+this.name+"] Could not resolve component named '" + curLookup + "' for property '" + reference.property + "'"); Profiler.exit("Entity.findProperty"); return null; } - + // Cache the split out string. path[0] = curLookup; reference.cachedLookup = path; @@ -463,20 +461,20 @@ package com.pblabs.engine.entity parentElem = PBE.nameManager.lookup(curLookup); if(!parentElem) { - if(!suppressErrors) - Logger.warn(this, "findProperty", "[#"+this.name+"] Could not resolve named object named '" + curLookup + "' for property '" + reference.property + "'"); + if(!suppressErrors) + Logger.warn(this, "findProperty", "[#"+this.name+"] Could not resolve named object named '" + curLookup + "' for property '" + reference.property + "'"); Profiler.exit("Entity.findProperty"); return null; } - + // Get the component on it. curIdx++; curLookup = path[1]; var comLookup:IEntityComponent = (parentElem as IEntity).lookupComponentByName(curLookup); if(!comLookup) { - if(!suppressErrors) - Logger.warn(this, "findProperty", "[#"+this.name+"] Could not find component '" + curLookup + "' on named entity '" + (parentElem as IEntity).name + "' for property '" + reference.property + "'"); + if(!suppressErrors) + Logger.warn(this, "findProperty", "[#"+this.name+"] Could not find component '" + curLookup + "' on named entity '" + (parentElem as IEntity).name + "' for property '" + reference.property + "'"); Profiler.exit("Entity.findProperty"); return null; } @@ -489,12 +487,12 @@ package com.pblabs.engine.entity parentElem = PBE.templateManager.getXML(curLookup, "template", "entity"); if(!parentElem) { - if(!suppressErrors) - Logger.warn(this, "findProperty", "[#"+this.name+"] Could not find XML named '" + curLookup + "' for property '" + reference.property + "'"); + if(!suppressErrors) + Logger.warn(this, "findProperty", "[#"+this.name+"] Could not find XML named '" + curLookup + "' for property '" + reference.property + "'"); Profiler.exit("Entity.findProperty"); return null; } - + // Try to find the specified component. curIdx++; var nextElem:* = null; @@ -506,13 +504,13 @@ package com.pblabs.engine.entity break; } } - + // Create it if appropriate. if(!nextElem && willSet) { // Create component tag. (parentElem as XML).appendChild(); - + // Look it up again. for each(cTag in parentElem.*) { @@ -523,36 +521,36 @@ package com.pblabs.engine.entity } } } - + // Error if we don't have it! if(!nextElem) { - if(!suppressErrors) - Logger.warn(this, "findProperty", "[#"+this.name+"] Could not find component '" + path[1] + "' in XML template '" + path[0].slice(1) + "' for property '" + reference.property + "'"); + if(!suppressErrors) + Logger.warn(this, "findProperty", "[#"+this.name+"] Could not find component '" + path[1] + "' in XML template '" + path[0].slice(1) + "' for property '" + reference.property + "'"); Profiler.exit("Entity.findProperty"); return null; } - + // Get ready to search the rest. parentElem = nextElem; - + // Indicate we are dealing with xml. isTemplateXML = true; } else { - if(!suppressErrors) - Logger.warn(this, "findProperty", "[#"+this.name+"] Got a property path that doesn't start with !, #, or @. Started with '" + startChar + "' for property '" + reference.property + "'"); + if(!suppressErrors) + Logger.warn(this, "findProperty", "[#"+this.name+"] Got a property path that doesn't start with !, #, or @. Started with '" + startChar + "' for property '" + reference.property + "'"); Profiler.exit("Entity.findProperty"); return null; } - + // Make sure we have a field to look up. if(curIdx < path.length) curLookup = path[curIdx++] as String; else curLookup = null; - + // Do the remainder of the look up. while(curIdx < path.length && parentElem) { @@ -569,13 +567,13 @@ package com.pblabs.engine.entity { parentElem = null; } - + // Several different possibilities that indicate we failed to advance. var gotEmpty:Boolean = false; if(parentElem == undefined) gotEmpty = true; if(parentElem == null) gotEmpty = true; if(parentElem is XMLList && parentElem.length() == 0) gotEmpty = true; - + // If we're going to set and it's XML, create the field. if(willSet && isTemplateXML && gotEmpty && oldParentElem) { @@ -583,19 +581,19 @@ package com.pblabs.engine.entity parentElem = oldParentElem.child(curLookup); gotEmpty = false; } - + if(gotEmpty) { - if(!suppressErrors) - Logger.warn(this, "findProperty", "[#"+this.name+"] Could not resolve property '" + curLookup + "' for property reference '" + reference.property + "'"); + if(!suppressErrors) + Logger.warn(this, "findProperty", "[#"+this.name+"] Could not resolve property '" + curLookup + "' for property reference '" + reference.property + "'"); Profiler.exit("Entity.findProperty"); return null; } - + // Advance to next element in the path. curLookup = path[curIdx++] as String; } - + // Did we end up with a match? if(parentElem) { @@ -605,13 +603,13 @@ package com.pblabs.engine.entity Profiler.exit("Entity.findProperty"); return pi; } - + Profiler.exit("Entity.findProperty"); return null; } - + private var _deferring:Boolean = true; - + protected var _components:Dictionary = new Dictionary(); protected var _tempPropertyInfo:PropertyInfo = new PropertyInfo(); protected var _deferredComponents:Array = new Array();