Entities are the most important game elements in every game. They move, fight, trade, spawn, die, kill, look good, give quests, grant buffs and everything else you can imagine! I think you can see that a good implementation of those is essential, especially to avoid conflicts. I will show you two general implementations and will list the advantages and disadvantages. I will support everything with examples from Doodle Jump, a very famous platformer on smartphones.

 

Extention:

The first approach is very basic and seems logical. There is an Entity class, which is the basis for all Entities. It could look like:

class Entity {
  float x;
  float y;
  float z;
  void update () { ... }
  void render () { ... }
}

 

This Entity class includes the position of the entity and two methods, which are called each update from the game loop. This is very basic, so we will dive further into this in form of an example.

Doodle Jump has 4 types of entities: The player, enemies, power ups and platforms. All these entities are classes which extend Entity. Let´s have a look a the Enemy class (It is, of course, pseudo code):

class Enemy extends Entity {
  float health;
  void update () {
    move();
    if (health <= 0) die();
  }
}

 

This class adds the normal enemy functionality to the game, so you can build different enemies. It is mostly used for small games, because of it´s easy implementation.

However, this method has some disadvantages:

  • You can run into conflicts: What can you do, if you want to make an enemy, which can´t move, but moving is inherited from Enemy?
  • You will have many classes and therefore much code to maintain.

 

Component based Entities:

The second approach is conflict proove and also quite logical. Instead of having different Entity classes you have one class which has components. A component is a part of an entity, which does exactly one thing. Entities can have, of course, multiple components. Let´s have a look into the Entity class:

class Entity {
  float x;
  float y;
  float z;

  Component[] components;

  void update () {
    foreach (component in components) component.update();
  }

  void render () {
    foreach (component in components) component.render();
  }
}

 

As you can see, components are updated by the Entity every update run and their render method is also called by the Entity. You can simply add and remove components from entites. It should be clear, how the component class looks. It simply has an update and a render method and subclasses (such as Moveable) could also have variables like velocity.

In our Doodle Jump example an enemy would get the components Moveable, Enemy and Killable. Moveable gives the enemy the ability to move, Enemy declares it as an enemy to allow other entities to check for enemies and Killable gives the enemy the ability to die and also a health variable, which can be modified (e.g. by the player´s projectiles on collision).

You should prefer to use the component based method to avoid nasty conflicts. It is also better for defining entities in files and not with classes, but this is a topic for another blog entry.

About these ads