Skip to content

Commit

Permalink
added the ability to name an entity and add removeAll() entities func…
Browse files Browse the repository at this point in the history
…tion to the entitiy manager
  • Loading branch information
tjdavies committed Dec 2, 2010
1 parent 902f5d3 commit 07182ad
Show file tree
Hide file tree
Showing 11 changed files with 250 additions and 144 deletions.
47 changes: 47 additions & 0 deletions .actionScriptProperties
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<actionScriptProperties analytics="false" mainApplicationPath="Ember.as" projectUUID="c028dfdf-c7fe-4f84-a4d2-da9b71d7d03b" version="6">
<compiler additionalCompilerArguments="-locale en_US" autoRSLOrdering="true" copyDependentFiles="false" fteInMXComponents="false" generateAccessible="false" htmlExpressInstall="true" htmlGenerate="false" htmlHistoryManagement="false" htmlPlayerVersionCheck="true" includeNetmonSwc="false" outputFolderPath="bin" sourceFolderPath="src" strict="true" targetPlayerVersion="0.0.0" useApolloConfig="false" useDebugRSLSwfs="true" verifyDigests="true" warn="true">
<compilerSourcePath/>
<libraryPath defaultLinkType="0">
<libraryPathEntry kind="4" path="">
<excludedEntries>
<libraryPathEntry kind="1" linkType="1" path="${PROJECT_FRAMEWORKS}/locale/{locale}"/>
<libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/flex.swc" useDefaultLinkType="false"/>
<libraryPathEntry index="4" kind="3" linkType="4" path="${PROJECT_FRAMEWORKS}/libs/sparkskins.swc" useDefaultLinkType="true">
<crossDomainRsls>
<crossDomainRslEntry autoExtract="true" policyFileUrl="http://fpdownload.adobe.com/pub/swz/crossdomain.xml" rslUrl="http://fpdownload.adobe.com/pub/swz/flex/4.1.0.16076/sparkskins_4.1.0.16076.swz"/>
<crossDomainRslEntry autoExtract="true" policyFileUrl="" rslUrl="sparkskins_4.1.0.16076.swz"/>
</crossDomainRsls>
</libraryPathEntry>
<libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/datavisualization.swc" useDefaultLinkType="false"/>
<libraryPathEntry index="2" kind="3" linkType="4" path="${PROJECT_FRAMEWORKS}/libs/framework.swc" useDefaultLinkType="true">
<crossDomainRsls>
<crossDomainRslEntry autoExtract="true" policyFileUrl="http://fpdownload.adobe.com/pub/swz/crossdomain.xml" rslUrl="http://fpdownload.adobe.com/pub/swz/flex/4.1.0.16076/framework_4.1.0.16076.swz"/>
<crossDomainRslEntry autoExtract="true" policyFileUrl="" rslUrl="framework_4.1.0.16076.swz"/>
</crossDomainRsls>
</libraryPathEntry>
<libraryPathEntry index="1" kind="3" linkType="4" path="${PROJECT_FRAMEWORKS}/libs/textLayout.swc" useDefaultLinkType="true">
<crossDomainRsls>
<crossDomainRslEntry autoExtract="true" policyFileUrl="http://fpdownload.adobe.com/pub/swz/crossdomain.xml" rslUrl="http://fpdownload.adobe.com/pub/swz/tlf/1.1.0.604/textLayout_1.1.0.604.swz"/>
<crossDomainRslEntry autoExtract="true" policyFileUrl="" rslUrl="textLayout_1.1.0.604.swz"/>
</crossDomainRsls>
</libraryPathEntry>
<libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/utilities.swc" useDefaultLinkType="false"/>
<libraryPathEntry index="4" kind="3" linkType="4" path="${PROJECT_FRAMEWORKS}/libs/spark.swc" useDefaultLinkType="true">
<crossDomainRsls>
<crossDomainRslEntry autoExtract="true" policyFileUrl="http://fpdownload.adobe.com/pub/swz/crossdomain.xml" rslUrl="http://fpdownload.adobe.com/pub/swz/flex/4.1.0.16076/spark_4.1.0.16076.swz"/>
<crossDomainRslEntry autoExtract="true" policyFileUrl="" rslUrl="spark_4.1.0.16076.swz"/>
</crossDomainRsls>
</libraryPathEntry>
</excludedEntries>
</libraryPathEntry>
<libraryPathEntry kind="1" linkType="1" path="libs"/>
</libraryPath>
<sourceAttachmentPath/>
</compiler>
<applications>
<application path="Ember.as"/>
</applications>
<modules/>
<buildCSSFiles/>
</actionScriptProperties>
6 changes: 6 additions & 0 deletions .flexLibProperties
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<flexLibProperties includeAllClasses="true" version="3">
<includeClasses/>
<includeResources/>
<namespaceManifests/>
</flexLibProperties>
18 changes: 18 additions & 0 deletions .project
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Ember</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>com.adobe.flexbuilder.project.flexbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>com.adobe.flexbuilder.project.flexlibnature</nature>
<nature>com.adobe.flexbuilder.project.actionscriptnature</nature>
</natures>
</projectDescription>
3 changes: 3 additions & 0 deletions .settings/org.eclipse.core.resources.prefs
@@ -0,0 +1,3 @@
#Wed Dec 01 08:58:14 GMT 2010
eclipse.preferences.version=1
encoding/<project>=utf-8
Binary file added libs/SwiftSuspenders-v1.5.1.swc
Binary file not shown.
124 changes: 64 additions & 60 deletions src/com/tomseysdavies/ember/base/Entity.as
@@ -1,61 +1,65 @@
/*
* Copyright (c) 2010 Tom Davies
*
* Permission is hereby granted to use, modify, and distribute this file
* in accordance with the terms of the license agreement accompanying it.
*/

package com.tomseysdavies.ember.base{

import com.tomseysdavies.ember.core.IEntity;
import com.tomseysdavies.ember.core.IEntityManger;


/**
* this is just a unique key used to retrieve components. It also has some helper function for convenience.
* @author Tom Davies
*/
public class Entity implements IEntity{

private var _entityManger:IEntityManger;

public function Entity(){
}

/**
* @inheritDoc
*/
public function addComponent(component:Object):void {
return _entityManger.addComponent(this,component);
}

/**
* @inheritDoc
*/
public function getComponent(Component:Class):* {
return _entityManger.getComponent(this,Component);
}

/**
* @inheritDoc
*/
public function removeComponent(Component:Class):void {
_entityManger.removeComponent(this,Component);
}

/**
* @inheritDoc
*/
public function destroy():void{
_entityManger.removeEntity(this);
}

/**
* @inheritDoc
*/
public function set manager(value:IEntityManger):void{
_entityManger = value;
}

}
/*
* Copyright (c) 2010 Tom Davies
*
* Permission is hereby granted to use, modify, and distribute this file
* in accordance with the terms of the license agreement accompanying it.
*/

package com.tomseysdavies.ember.base{

import com.tomseysdavies.ember.core.IEntity;
import com.tomseysdavies.ember.core.IEntityManger;


/**
* this is just class that holds helper functions for convenience.
* @author Tom Davies
*/
public class Entity implements IEntity{

private var _entityManger:IEntityManger;
private var _id:String;

public function Entity(entityManger:IEntityManger,id:String){
_entityManger = entityManger;
_id = id;
}

/**
* @inheritDoc
*/
public function addComponent(component:Object):void {
return _entityManger.addComponent(_id,component);
}

/**
* @inheritDoc
*/
public function getComponent(Component:Class):* {
return _entityManger.getComponent(_id,Component);
}

/**
* @inheritDoc
*/
public function removeComponent(Component:Class):void {
_entityManger.removeComponent(_id,Component);
}

/**
* @inheritDoc
*/
public function destroy():void{
_entityManger.removeEntity(_id);
}

/**
* @inheritDoc
*/
public function get id():String{
return _id;
}


}
}
84 changes: 52 additions & 32 deletions src/com/tomseysdavies/ember/base/EntityManager.as
Expand Up @@ -22,11 +22,13 @@ package com.tomseysdavies.ember.base {
private var _components:Dictionary;
private var _families:Object;
private var _componentFamilyMap:Dictionary;
private var _currentKey:int;

public function EntityManager() {
_components = new Dictionary();
_families = new Object();
_componentFamilyMap = new Dictionary();
_currentKey = 0;
}

//---------------------------------------------------------------------
Expand All @@ -36,45 +38,59 @@ package com.tomseysdavies.ember.base {
/**
* @inheritDoc
*/
public function createEntity():IEntity{
var entity:IEntity = new Entity();
entity.manager = this;
_components[entity] = new Dictionary();
public function createEntity(Id:String = null):IEntity{
var key:String = Id;
if(key == null){
_currentKey ++;
key = "id_" + _currentKey;
}
var entity:IEntity = new Entity(this,key);
_components[entity.id] = new Dictionary();
return entity;
}

/**
* @inheritDoc
*/
public function removeEntity(entity:IEntity):void {
for each(var component:Object in _components[entity]){
removeEntityFromFamilies(entity,getClass(component));
public function removeEntity(entityId:String):void {
for each(var component:Object in _components[entityId]){
removeEntityFromFamilies(entityId,getClass(component));
}
delete _components[entity];
delete _components[entityId];
}

/**
* @inheritDoc
*/
public function addComponent(entity:IEntity,compoment:Object):void{
public function removeAll():void {
for(var entityId:String in _components){
removeEntity(entityId);
}
_currentKey = 0;
}

/**
* @inheritDoc
*/
public function addComponent(entityId:String,compoment:Object):void{
var Compoment:Class = getClass(compoment);
_components[entity][Compoment] = compoment;
addEntityToFamilies(entity,Compoment);
_components[entityId][Compoment] = compoment;
addEntityToFamilies(entityId,Compoment);
}

/**
* @inheritDoc
*/
public function getComponent(entity:IEntity,Component:Class):*{
return _components[entity][Component];
public function getComponent(entityId:String,Component:Class):*{
return _components[entityId][Component];
}

/**
* @inheritDoc
*/
public function removeComponent(entity:IEntity,Component:Class):void{
removeEntityFromFamilies(entity,Component);
delete _components[entity][Component];
public function removeComponent(entityId:String,Component:Class):void{
removeEntityFromFamilies(entityId,Component);
delete _components[entityId][Component];
}

/**
Expand All @@ -90,22 +106,24 @@ package com.tomseysdavies.ember.base {
*/
public function destroy():void {
_components = null;
_families = null;
_componentFamilyMap = null;
_currentKey = null;
}

//---------------------------------------------------------------------
// Internal
//---------------------------------------------------------------------



/**
* gets all Entities with specifed Components
*/
private function getAllComposingX(Components:Array):Vector.<IEntity>{
var entityList:Vector.<IEntity> = new Vector.<IEntity>;
for(var e:Object in _components){
var enity:IEntity = e as IEntity;
if(hasAllComponents(enity,Components)){
entityList.push(enity);
for(var entityId:String in _components){
//var enity:IEntity = e as IEntity;
if(hasAllComponents(entityId,Components)){
entityList.push(new Entity(this,entityId));
}
}
return entityList;
Expand All @@ -114,9 +132,9 @@ package com.tomseysdavies.ember.base {
/**
* checks if a entity has a set of Components
*/
private function hasAllComponents(entity:IEntity,Components:Array):Boolean{
private function hasAllComponents(entityId:String,Components:Array):Boolean{
for each(var Component:Class in Components){
if(!_components[entity][Component]){
if(!_components[entityId][Component]){
return false;
}
}
Expand All @@ -126,26 +144,28 @@ package com.tomseysdavies.ember.base {
/**
* updates families when a component is added to an entity
*/
private function addEntityToFamilies(entity:IEntity,Component:Class):void{
private function addEntityToFamilies(entityId:String,Component:Class):void{
var families:Vector.<Array> = getFamiliesWithComponent(Component);
for each(var Components:Array in getFamiliesWithComponent(Component)){
if(hasAllComponents(entity,Components)){
if(hasAllComponents(entityId,Components)){
var family:Vector.<IEntity> = getFamily(Components);
family.push(entity);
family.push(new Entity(this,entityId));
}
}
}

/**
* updates families when a component is removed from an entity
*/
private function removeEntityFromFamilies(entity:IEntity,Component:Class):void{
private function removeEntityFromFamilies(entityId:String,Component:Class):void{
var families:Vector.<Array> = getFamiliesWithComponent(Component);
for each(var Components:Array in getFamiliesWithComponent(Component)){
var family:Vector.<IEntity> = getFamily(Components);
var index:int = family.indexOf(entity);
if(index > -1){
family.splice(family.indexOf(entity),1)
for(var i:int=0; i<family.length; i++){
var entity:IEntity = family[i] as IEntity;
if(entity.id == entityId){
family.splice(i,1)
}
}
}
}
Expand Down Expand Up @@ -181,7 +201,7 @@ package com.tomseysdavies.ember.base {
return getAllComposingX(Components);
}


}

}
3 changes: 2 additions & 1 deletion src/com/tomseysdavies/ember/base/SystemManager.as
Expand Up @@ -38,10 +38,11 @@ package com.tomseysdavies.ember.base{
/**
* @inheritDoc
*/
public function addSystem(System:Class):void{
public function addSystem(System:Class):*{
var system:ISystem = _injector.instantiate(System)
_systems[System] = system;
system.onRegister();
return system;
}

/**
Expand Down

0 comments on commit 07182ad

Please sign in to comment.