Enitity-Component-System architecture

Motivation

Entity–component–system (ECS) is an architectural pattern that is mostly used in game development. ECS follows the composition over inheritance principle that allows greater flexibility in defining entities where every object in a game's scene is an entity (e.g. enemies, bullets, vehicles, etc.). Every entity consists of one or more components which add behavior or functionality. Therefore, the behavior of an entity can be changed at runtime by adding or removing components. This eliminates the ambiguity problems of deep and wide inheritance hierarchies that are difficult to understand, maintain and extend. Common ECS approaches are highly compatible and often combined with data-oriented design techniques.


Architecture

Architecture contains three different classes: Entity, System and Component.

Entity: The entity is a general purpose object. Typically Entity has several components attached to it. If several components are attached to Entity, it will result to some object type. See Entity diagrams below for examples.

Component: the raw data for one aspect of the object, and how it interacts with the world. Labels the Entity as possessing this particular aspect. Implementations typically use structs, classes, or associative arrays.

System: Each System runs continuously (as though each System had its own private thread) and performs global actions on every Entity that possesses a Component of the same aspect as that System.

EntityComponentSystem1nn1


Example: AnimalSystem

Class Diagram:

Animal System is a kind of system, which has Animal Behaviour -components. There are several kind of AnimalBehaviours like: Flight, Pack and Walk behaviours.

ComponentAnimalBehaviourFlightPeckWalkSystemAnimalSystem1n

Entity diagram of Bear:

Bear is an Entity only having Walking component.

bearname = "Bear"Walk1

Entity diagram of Penguin:

Penguin is an Entity having Walking and Pecking components.

penguinname = "Penguin"PeckWalk11

Entity diagram of Raven:

Raven is an Entity having Flighting, Walking and Pecking components.

ravenname = "Raven"PeckFlightWalk111


Code example

You can find some kind of reference implementation of ECS in repository: ´https://repo.kamit.fi/mikkorom/ecs.git´

git clone https://repo.kamit.fi/mikkorom/ecs.git