Categories
entity systems games design programming

Entity System 1: Objective-C

(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:

  1. (ComponentType) getComponentType();
EntityManager

This contains:

  1. The master collections (arrays, hashmaps, whatever) that contain all the data for all the entities
  2. Logic for creating, modifying, fetching, and deleting entities
  3. Logic for fetching and modifying components-from-entities

Internally, the class has the following variables:

  1. MAP: from “Entity + ComponentType” to “concrete Component subclass”
  2. LIST: all entities in existence (so it never duplicates entity-IDs!)
  3. Internally, the class has the following functions:

    1. (Component) getComponent( Entity, ComponentType );
    2. (List) getAllComponentsOfType( ComponentType );
    3. (List) getAllEntitiesPossessingComponent( ComponentType );
    4. (void) addComponent( Entity, Component );
    5. (int) createEntity;
    6. (void) killEntity( Entity );