├── .gitignore └── EntitySystemJava ├── src └── com │ └── wikidot │ └── entitysystems │ └── rdbmswithcodeinsystems │ ├── Component.java │ ├── EntityManager.java │ ├── SubSystem.java │ └── examplecomponents │ └── Position.java └── tests └── com └── wikidot └── entitysystems └── rdbmswithcodeinsystems └── tests ├── AllTests.java ├── TestComponentAccess.java ├── TestKillEntity.java ├── TestPosition.java └── quicktest.java /.gitignore: -------------------------------------------------------------------------------- 1 | /.metadata/ 2 | /bin/ 3 | /*/bin/ 4 | EntitySystemJava/.classpath 5 | 6 | EntitySystemJava/.project 7 | -------------------------------------------------------------------------------- /EntitySystemJava/src/com/wikidot/entitysystems/rdbmswithcodeinsystems/Component.java: -------------------------------------------------------------------------------- 1 | package com.wikidot.entitysystems.rdbmswithcodeinsystems; 2 | 3 | /** 4 | * Standard design: c.f. http://entity-systems.wikidot.com/rdbms-with-code-in-systems 5 | * 6 | * Modified in java to use Generics: instead of having a "ComponentType" field, we use the class type 7 | * of each subclass instead. This is safer. 8 | */ 9 | 10 | public interface Component 11 | { 12 | 13 | } -------------------------------------------------------------------------------- /EntitySystemJava/src/com/wikidot/entitysystems/rdbmswithcodeinsystems/EntityManager.java: -------------------------------------------------------------------------------- 1 | package com.wikidot.entitysystems.rdbmswithcodeinsystems; 2 | import java.util.ArrayList; 3 | import java.util.HashMap; 4 | import java.util.HashSet; 5 | import java.util.LinkedList; 6 | import java.util.List; 7 | import java.util.Set; 8 | 9 | /** 10 | * Standard design: c.f. http://entity-systems.wikidot.com/rdbms-with-code-in-systems 11 | * 12 | * Modified in java to use Generics: instead of having a "ComponentType" enum, we use the class type 13 | * of each subclass instead. This is safer. 14 | */ 15 | 16 | public class EntityManager 17 | { 18 | int lowestUnassignedEntityID=1; 19 | List allEntities; 20 | HashMap, HashMap> componentStores; 21 | 22 | public EntityManager() 23 | { 24 | allEntities = new LinkedList(); 25 | componentStores = new HashMap, HashMap>(); 26 | } 27 | 28 | public T getComponent( int entity, Class componentType) 29 | { 30 | HashMap store = componentStores.get( componentType ); 31 | 32 | if( store == null) 33 | throw new IllegalArgumentException( "GET FAIL: there are no entities with a Component of class: "+componentType ); 34 | 35 | T result = componentType.cast(store.get(entity)); 36 | if( result == null ) 37 | throw new IllegalArgumentException( "GET FAIL: "+entity+" does not possess Component of class\n missing: "+componentType ); 38 | 39 | return result; 40 | } 41 | 42 | public List getAllComponentsOfType( Class componentType ) 43 | { 44 | HashMap store = componentStores.get( componentType ); 45 | 46 | if( store == null ) 47 | { 48 | return new LinkedList(); 49 | } 50 | else 51 | { 52 | List result = new ArrayList((java.util.Collection)store.values()); 53 | return result; 54 | } 55 | } 56 | 57 | public Set getAllEntitiesPossessingComponent( Class componentType ) 58 | { 59 | HashMap store = componentStores.get( componentType ); 60 | 61 | if( store == null) 62 | return new HashSet(); 63 | 64 | return store.keySet(); 65 | } 66 | 67 | public void addComponent( int entity, T component ) 68 | { 69 | HashMap store = componentStores.get( component.getClass() ); 70 | 71 | if( store == null ) 72 | { 73 | store = new HashMap(); 74 | componentStores.put(component.getClass(), store ); 75 | } 76 | 77 | ((HashMap)store).put(entity, component); 78 | } 79 | 80 | public int createEntity() 81 | { 82 | 83 | int newID = generateNewEntityID(); 84 | 85 | if( newID < 1 ) 86 | { 87 | /** 88 | Fatal error... 89 | */ 90 | return 0; 91 | } 92 | else 93 | { 94 | allEntities.add(newID); 95 | 96 | return newID; 97 | } 98 | } 99 | 100 | public void killEntity( Integer entity ) // Pass as an object to remove key instead of index 101 | { 102 | synchronized( this ) // prevent it generating two entities with same ID at once 103 | { 104 | allEntities.remove(entity); 105 | for( HashMap store : componentStores.values() ) 106 | { 107 | store.remove(entity); 108 | } 109 | } 110 | } 111 | 112 | public int generateNewEntityID() 113 | { 114 | synchronized( this ) // prevent it generating two entities with same ID at once 115 | { 116 | if( lowestUnassignedEntityID < Integer.MAX_VALUE ) 117 | { 118 | return lowestUnassignedEntityID++; 119 | } 120 | else 121 | { 122 | for( int i=1; i result = em.getAllComponentsOfType(Position.class); 63 | assertTrue(result.isEmpty()); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /EntitySystemJava/tests/com/wikidot/entitysystems/rdbmswithcodeinsystems/tests/TestPosition.java: -------------------------------------------------------------------------------- 1 | package com.wikidot.entitysystems.rdbmswithcodeinsystems.tests; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.Before; 6 | import org.junit.Test; 7 | 8 | import com.wikidot.entitysystems.rdbmswithcodeinsystems.EntityManager; 9 | import com.wikidot.entitysystems.rdbmswithcodeinsystems.examplecomponents.Position; 10 | 11 | public class TestPosition { 12 | private EntityManager em; 13 | 14 | @Before 15 | public void setUp() throws Exception { 16 | em = new EntityManager(); 17 | } 18 | 19 | /* 20 | * This is just quicktest. 21 | */ 22 | @Test 23 | public void test() { 24 | int entity1 = em.createEntity(); 25 | 26 | em.addComponent(entity1, new Position()); 27 | em.getComponent(entity1, Position.class).x = 5; 28 | em.getComponent(entity1, Position.class).y = 10; 29 | 30 | assertEquals("(5.0,10.0)", ""+em.getComponent(entity1, Position.class)); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /EntitySystemJava/tests/com/wikidot/entitysystems/rdbmswithcodeinsystems/tests/quicktest.java: -------------------------------------------------------------------------------- 1 | package com.wikidot.entitysystems.rdbmswithcodeinsystems.tests; 2 | 3 | import com.wikidot.entitysystems.rdbmswithcodeinsystems.EntityManager; 4 | import com.wikidot.entitysystems.rdbmswithcodeinsystems.examplecomponents.Position; 5 | 6 | /** 7 | * Very brief test to check that the ES implementation is compiling and running OK 8 | * 9 | * Run from the command line - no arguments - and see if you get Exceptions / Errors. 10 | */ 11 | public class quicktest 12 | { 13 | 14 | public static void main(String[] args) 15 | { 16 | EntityManager em = new EntityManager(); 17 | 18 | int entity1 = em.createEntity(); 19 | 20 | em.addComponent(entity1, new Position()); 21 | em.getComponent(entity1, Position.class).x = 5; 22 | em.getComponent(entity1, Position.class).y = 10; 23 | 24 | System.out.println( "Value of Position component is: "+em.getComponent(entity1, Position.class)); 25 | } 26 | } 27 | --------------------------------------------------------------------------------