(this is a simplified, but complete, version of the “Entity System 1: Java/Android”)
NB: this post is part of the community effort to make working, usable, free, source-examples of actual Entity Systems, found at http://entity-systems.wikidot.com/
Wiki link: http://entity-systems.wikidot.com/rdbms-with-code-in-systems
Github source: https://github.com/adamgit/Entity-System–RDBMS-Inspired–Objective-C-
I’m posting my original design notes here – also copy/pasted to the wiki, but the wiki version will probably evolve over time. Comments on this post will be disabled – please comment direct on the wiki / wiki discussion pages.
NB: the source code is in GitHub, that’s where you should download it from (will include any fixes and updates over time)
Concepts
(integer) Entity
(enum) ComponentType
(interface OR empty base-class) Component
(interface OR empty base-class) System
(class with implementation) EntityManager
Data Classes
Entity
This is a non-negative integer, and uniquely identifies an entity within the ES. If you serialize your ES, this SHOULD be the same after serialization. New entities have to be assigned a new, unique, integer value when they are created. When an entity is destroyed, its integer value is freed, available for re-use.
ComponentType
This is needed because this ES is so simplistic it has no other way of identifying which Component you’re talking about when you’re looking at a single Entity.
(richer EntitySystems MAY not need this type)
Component
This exists purely to make method-signatures type-safe. We need to know that certain objects are valid instances of a “component” (so we use this superclass to indicate that), but we also need a method that’s guaranteed to work on all those objects (see below).
(richer EntitySystems make more use of this, and require it to be a class, but for this simple ES, you can use an interface/header file)
System
This exists purely to contain a known method-signature that your game-loop can call once per game-tick.
EntitySystem Implementation Classes
Each subclass of Component
Internally, the class has the following functions:
- (ComponentType) getComponentType();
EntityManager
This contains:
- The master collections (arrays, hashmaps, whatever) that contain all the data for all the entities
- Logic for creating, modifying, fetching, and deleting entities
- Logic for fetching and modifying components-from-entities
Internally, the class has the following variables:
- MAP: from “Entity + ComponentType” to “concrete Component subclass”
- LIST: all entities in existence (so it never duplicates entity-IDs!)
-
Internally, the class has the following functions:
- (Component) getComponent( Entity, ComponentType );
- (List) getAllComponentsOfType( ComponentType );
- (List) getAllEntitiesPossessingComponent( ComponentType );
- (void) addComponent( Entity, Component );
- (int) createEntity;
- (void) killEntity( Entity );