Skip to content

Commit

Permalink
Merge branch 'master' of github.com:lpmaurice/Ember
Browse files Browse the repository at this point in the history
  • Loading branch information
tjdavies committed Apr 2, 2011
2 parents 5524b62 + 119cb23 commit 6f3de9d
Show file tree
Hide file tree
Showing 12 changed files with 197 additions and 17 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ bin-release/*
# OS X
Icon
Thumbs.db
.DS_Store
.DS_Store

#uncompiled libraries
libs-src/*
8 changes: 4 additions & 4 deletions src/com/tomseysdavies/ember/base/Entity.as
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
package com.tomseysdavies.ember.base{

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


/**
Expand All @@ -17,18 +17,18 @@ package com.tomseysdavies.ember.base{
*/
internal class Entity implements IEntity{

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

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

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

Expand Down
22 changes: 18 additions & 4 deletions src/com/tomseysdavies/ember/base/EntityManager.as
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

package com.tomseysdavies.ember.base {
import com.tomseysdavies.ember.core.IEntity;
import com.tomseysdavies.ember.core.IEntityManger;
import com.tomseysdavies.ember.core.IEntityManager;
import com.tomseysdavies.ember.core.IFamily;

import flash.utils.Dictionary;
Expand All @@ -18,7 +18,7 @@ package com.tomseysdavies.ember.base {
* manages the relations between components and entites and keeps entity families upto date.
* @author Tom Davies
*/
public class EntityManager implements IEntityManger {
public class EntityManager implements IEntityManager {

private var _components:Dictionary;
private var _families:Dictionary;
Expand All @@ -44,12 +44,22 @@ package com.tomseysdavies.ember.base {
if(key == null || key == ""){
_currentKey ++;
key = "id_" + _currentKey;
}
}else if(_components[Id]){
return null;
}
var entity:IEntity = new Entity(this,key);
_components[entity.id] = new Dictionary();
return entity;
}

/**
* @inheritDoc
*/
public function verifyExistenceOf(id:String):Boolean
{
return _components[id] != null;
}

/**
* @inheritDoc
*/
Expand All @@ -74,10 +84,14 @@ package com.tomseysdavies.ember.base {
/**
* @inheritDoc
*/
public function addComponent(entityId:String,component:Object):void{
public function addComponent(entityId:String,component:Object):Boolean{
var Component:Class = getClass(component);
if(!verifyExistenceOf(entityId)){
return false;
}
_components[entityId][Component] = component;
addEntityToFamilies(entityId,Component);
return true;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/com/tomseysdavies/ember/base/Family.as
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.tomseysdavies.ember.base
{
import com.tomseysdavies.ember.core.IEntity;
import com.tomseysdavies.ember.core.IEntityManger;
import com.tomseysdavies.ember.core.IEntityManager;
import com.tomseysdavies.ember.core.IFamily;

import org.osflash.signals.Signal;
Expand Down
6 changes: 3 additions & 3 deletions src/com/tomseysdavies/ember/base/Game.as
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

package com.tomseysdavies.ember.base{

import com.tomseysdavies.ember.core.IEntityManger;
import com.tomseysdavies.ember.core.IEntityManager;
import com.tomseysdavies.ember.core.IGame;
import com.tomseysdavies.ember.core.ISystemManager;

Expand All @@ -20,7 +20,7 @@ package com.tomseysdavies.ember.base{
public class Game implements IGame {

private var _contextView:DisplayObjectContainer;
private var _entityManager:IEntityManger;
private var _entityManager:IEntityManager;
private var _systemManager:ISystemManager;
private var _injector:Injector;

Expand Down Expand Up @@ -105,7 +105,7 @@ package com.tomseysdavies.ember.base{
/**
* The <code>IEntityManger</code> for this <code>IGame</code>
*/
protected function get entityManager():IEntityManger{
protected function get entityManager():IEntityManager{
return _entityManager || (_entityManager = new EntityManager());
}

Expand Down
2 changes: 1 addition & 1 deletion src/com/tomseysdavies/ember/core/IEntity.as
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ package com.tomseysdavies.ember.core{
* @param component to be added
*
*/
function addComponent(component:Object):void;
function addComponent(component:Object):Boolean;

/**
* retrieves a components instance based on class name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,25 @@

package com.tomseysdavies.ember.core{

public interface IEntityManger extends IDisposable {
public interface IEntityManager extends IDisposable {

/**
* creates a new entity with the id provided. If no id is provided a unique id will be auto generated
* Creates a new entity with the id provided. If no id is provided a unique id will be auto generated.
*
* If an id is provided but the entityManager already has an entity
* with the same id, no entity will be created.
*
* @return the new entity
*
*/
function createEntity(Id:String = null):IEntity;

/**
*
* @return Boolean, true if an entity with the provided id exists.
*/
function verifyExistenceOf(id:String):Boolean;

/**
* unregisters an entity
*
Expand All @@ -36,8 +45,10 @@ package com.tomseysdavies.ember.core{
*
* @param entity the component is to be registered with
* @param component to be registered
*
* @return Boolean, true if the component was added.
*/
function addComponent(entityId:String,component:Object):void;
function addComponent(entityId:String,component:Object):Boolean;

/**
*retrieves a component
Expand Down
15 changes: 15 additions & 0 deletions test/AllTests.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package
{
import asunit.framework.TestSuite;

import com.tomseysdavies.ember.base.EntityManagerTest;

public class AllTests extends TestSuite
{
public function AllTests()
{
super();
addTest(new EntityManagerTest());
}
}
}
10 changes: 10 additions & 0 deletions test/EmberTestRunner.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package {
import asunit.textui.TestRunner;

public class EmberTestRunner extends TestRunner {

public function EmberTestRunner() {
start(AllTests);
}
}
}
8 changes: 8 additions & 0 deletions test/com/tomseysdavies/ember/base/ComponentA.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.tomseysdavies.ember.base
{
public class ComponentA
{
public var x:int;
public var y:int;
}
}
7 changes: 7 additions & 0 deletions test/com/tomseysdavies/ember/base/ComponentB.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.tomseysdavies.ember.base
{
public class ComponentB
{
public var health:int;
}
}
112 changes: 112 additions & 0 deletions test/com/tomseysdavies/ember/base/EntityManagerTest.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package com.tomseysdavies.ember.base
{
import asunit.framework.TestCase;

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

public class EntityManagerTest extends TestCase
{
public function EntityManagerTest()
{
super();
}

override protected function setUp():void
{
_entityManager = new EntityManager();
}

override protected function tearDown():void
{
_entityManager.dispose();
_entityManager = null;
}

public function testVerifyExistenceOf():void
{
_entityManager.createEntity(SOME_ID);
assertTrue("Entity was proven to exist.", _entityManager.verifyExistenceOf(SOME_ID));
}

public function testVerifyExistenceOfFailure():void
{
_entityManager.createEntity(SOME_ID);
assertFalse("Entity was not proven to exist.", _entityManager.verifyExistenceOf("FAIL"));
}

public function testEntityWithoutIDCreated():void
{
var entity:IEntity = _entityManager.createEntity();
assertNotNull("Entity was created WITHOUT an id specified.", entity);
}

public function testEntityWithIDCreated():void
{
var entity:IEntity = _entityManager.createEntity("SOME_ID");
assertNotNull("Entity was created WITH an id specified.", entity);
}

public function testDuplicateIDDoesNotCreateEntity():void
{
_entityManager.createEntity(SOME_ID);
assertNull("Entity with duplicate ID won't be created.", _entityManager.createEntity(SOME_ID));
}

public function testEntityIsDeleted():void
{
_entityManager.createEntity(SOME_ID);
_entityManager.removeEntity(SOME_ID);
assertFalse("Entity was removed.", _entityManager.verifyExistenceOf(SOME_ID));
}

public function testComponentAdded():void
{
var result:Boolean;
_entityManager.createEntity(SOME_ID);
result = _entityManager.addComponent(SOME_ID, new ComponentA());
assertTrue("Component was added.", result);
}

public function testComponentNotAddedIfNoEntity():void
{
var result:Boolean = _entityManager.addComponent("FAIL", new ComponentA());
assertFalse("Component was not added.", result);
}

public function testComponentFound():void
{
var result:ComponentA;
_entityManager.createEntity(SOME_ID);
_entityManager.addComponent(SOME_ID, new ComponentA());
result = _entityManager.getComponent(SOME_ID, ComponentA);
assertNotNull("Component was found.", result);
}

public function testComponentRemoved():void
{
var result:ComponentA;
_entityManager.createEntity(SOME_ID);
_entityManager.addComponent(SOME_ID, new ComponentA());
_entityManager.removeComponent(SOME_ID, ComponentA);
result = _entityManager.getComponent(SOME_ID, ComponentA);
assertNull("Component was removed.", result);
}

public function testFamilyCreatedWithOneComponent():void
{
var result:IFamily = _entityManager.getEntityFamily(ComponentA);
assertTrue("Family was retrieved.", result is IFamily);
}

public function testFamilyCreatedWithManyComponents():void
{
var result:IFamily = _entityManager.getEntityFamily(ComponentA, ComponentB);
assertTrue("Family was retrieved.", result is IFamily);
}

//_________________PRIVATE
private static const SOME_ID:String = "SOME_ID";
private var _entityManager:EntityManager;
}
}

0 comments on commit 6f3de9d

Please sign in to comment.