Unlimited WordPress themes, graphics, videos & courses! Unlimited asset downloads! From \$16.50/m

# Avoiding the Blob Antipattern: A Pragmatic Approach to Entity Composition

Organising your game code into component-based entities, rather than relying only on class inheritance, is a popular approach in game development. In this tutorial, we'll look at why you might do this, and set up a simple game engine using this technique.

## Introduction

In this tutorial I'm going to explore component-based game entities, look at why you might want to use them, and suggest a pragmatic approach to dip your toe in the water.

As it's a story about code organisation and architecture, I'll start by dropping in the usual "get out of jail" disclaimer: this is just one way of doing things, it's not "the one way" or maybe even the best way, but it might work for you.  Personally, I like to find out about as many approaches as possible and then work out what suits me.

## Final Result Preview

Throughout this two-part tutorial, we'll create this Asteroids game. (The full source code is available on GitHub.) In this first part, we'll focus on the core concepts and general game engine.

## What Problem Are We Solving?

In a game like Asteroids, we might have a few basic types of on-screen "thing": bullets, asteroids, player ship and enemy ship. We might want to represent these basic types as four separate classes, each containing all the code we need to draw, animate, move and control that object.

While this will work, it might be better to follow the Don't Repeat Yourself (DRY) principle and try to reuse some of the code between each class -- after all, the code for moving and drawing a bullet is going to be very similar to, if not exactly the same as, the code to move and draw an asteroid or a ship.

So we can refactor our rendering and movement functions into a base class that everything extends from. But Ship and EnemyShip also need to be able to shoot. At this point we could add the shoot function to the base class, creating a "Giant Blob" class that can do basically everything, and just make sure asteroids and bullets never call their shoot function.  This base class would soon get very large, swelling in size each time entities need to be able to do new things. This isn't necessarily wrong, but I find smaller, more specialised classes to be easier to maintain.

Alternatively, we can go down the root of deep inheritance and have something like EnemyShip extends Ship extends ShootingEntity extends Entity. Again this approach isn't wrong, and will also work quite well, but as you add more types of Entities, you will find yourself constantly having to readjust the inheritance hierarchy to handle all the possible scenarios, and you can box yourself into a corner where a new type of Entity needs to have the functionality of two different base classes, requiring multiple inheritance (which most programming languages don't offer).

I have used the deep hierarchy approach many times myself, but I actually prefer the Giant Blob approach, as at least then all entities have a common interface and new entities can be added more easily (so what if all your trees have A* pathfinding?!)

There is, however, a third way...

## Composition Over Inheritance

If we think of the Asteroids problem in terms of things that objects might need to do, we might get a list like this:

• move()
• shoot()
• takeDamage()
• die()
• render()

Instead of working out a complicated inheritance hierarchy for which objects can do which things, let's model the problem in terms of components that can perform these actions.

For instance, we could create a Health class, with the methods takeDamage(), heal() and die(). Then any object that needs to be able to take damage and die can "compose" an instance of the Health class -- where "compose" basically means "keep a reference to its own instance of this class".

We could create another class called View to look after the rendering functionality, one called Body to handle movement and one called Weapon to handle shooting.

Most Entity systems are based on the principle described above, but differ in how you access functionality contained in a component.

### Mirroring the API

For example, one approach is to mirror the API of each component in the Entity, so an entity that can take damage would have a takeDamage() function that itself just calls the takeDamage() function of its Health component.

You then have to create an interface called something like IHealth for your entity to implement, so that other objects can access the takeDamage() function. This is how a Java OOP guide might advise you to do it.

### getComponent()

Another approach is to simply store each component in a key-value lookup, so that every Entity has a function called something like getComponent("componentName") which returns a reference to the particular component. You then need to cast the reference you get back to the type of component you want -- something like:

This is basically how Unity's entity/behaviour system works. It's very flexible, because you can keep adding new types of component without changing your base class, or creating new subclasses or interfaces. It might also be useful when you want to use configuration files to create entities without recompiling your code, but I'll leave that to someone else to figure out.

### Public Components

The approach I favour is to let all entities have a public property for each major type of component, and leave fields null if the entity doesn't have that functionality. When you want to call a particular method, you just "reach in" to the entity to get the component with that functionality -- for example, call enemy.health.takeDamage(5) to attack an enemy.

If you try to call health.takeDamage() on an entity that doesn't have a Health component, it will compile, but you'll get a runtime error letting you know you've done something silly. In practice this rarely happens, as it's pretty obvious which types of entity will have which components (for example, of course a tree doesn't have a weapon!).

Some strict OOP advocates might argue that my approach breaks some OOP principles, but I find it works really well, and there's a really good precedent from the history of Adobe Flash.

In ActionScript 2, the MovieClip class had methods for drawing vector graphics: for example, you could call myMovieClip.lineTo() to draw a line. In ActionScript 3, these drawing methods were moved to the Graphics class, and each MovieClip gets a Graphics component, which you access by calling, for example, myMovieClip.graphics.lineTo() in the same way I described for enemy.health.takeDamage(). If it's good enough for the ActionScript language designers, it's good enough for me.

## My System (Simplified)

Below I'm going to detail a very simplified version of the system I use across all my games. In terms of how simplified, it's something like 300 lines of code for this, compared to 6,000 for my full engine. But we can actually do quite a lot with just these 300 lines!

I've left in just enough functionality to create a working game, while keeping the code as short as possible so it's easier to follow. The code is going to be in ActionScript 3, but a similar structure is possible across most languages. There are a few public variables that could be properties (i.e. put behind get and set accessor functions), but as this is quite verbose in ActionScript, I've left them as public variables for ease of reading.

### The IEntity Interface

Let's start by defining an interface that all entities will implement:

All entities can perform three actions: you can update them, render them and destroy them.

They each have "slots" for five components:

• A body, handling position and size.
• physics, handling movement.
• health, handling getting hurt.
• A weapon, handling attacking.
• And finally a view, allowing you to render the entity.

All of these components are optional and can be left null, but in practice most entities will have at least a couple of components.

A piece of static scenery that the player can't interact with (maybe a tree, for example), would need just a body and a view. It wouldn't need physics as it doesn't move, it wouldn't need health as you can't attack it, and it certainly wouldn't need a weapon. The player's ship in Asteroids, on the other hand, would need all five components, as it can move, shoot and get hurt.

By configuring these five basic components, you can create most simple objects you might need. Sometimes they won't be enough, however, and at that point we can either extend the basic components, or create new additional ones -- both of which we'll discuss later.

Next we have two Signals: entityCreated and destroyed.

Signals are an open source alternative to ActionScript's native events, created by Robert Penner. They're really nice to use as they allow you to pass data between the dispatcher and the listener without having to create lots of custom Event classes. For more information on how to use them, check out the documentation.

The entityCreated Signal allows an entity to tell the game that there is another new entity that needs to be added -- a classic example being when a gun creates a bullet. The destroyed Signal lets the game (and any other listening objects) know that this entity has been destroyed.

Finally, the entity has two other optional dependencies: targets, which is a list of entities that it might want to attack, and group, which is a list of entities that it belongs to. For example, a player ship might have a list of targets, which would be all the enemies in the game, and might belong to a group which also contains any other players and friendly units.

### The Entity Class

Now let's look at the Entity class that implements this interface.

It looks long, but most of it is just those verbose getter and setter functions (boo!). The important part to look at is the first four functions: the constructor, where we create our Signals; destroy(), where we dispatch the destroyed Signal and remove the entity from its group list; update(), where we update any components that need to act every game loop -- although in this simple example this is only the physics component -- and finally render(), where we tell the view to do its thing.

You'll notice that we don't automatically instantiate the components here in the Entity class -- this is because, as I explained earlier, each component is optional.

### The Individual Components

Now let's look at the components one by one. First, the body component:

All our components need a reference to their owner entity, which we pass to the constructor. The body then has four simple fields: an x and y position, an angle of rotation, and a radius to store its size. (In this simple example, all entities are circular!)

This component also has a single method: testCollision(), which uses Pythagoras to calculate the distance between two entities, and compares this to their combined radiuses. (More info here.)

Next let's look at the Physics component:

Looking at the update() function, you can see that the velocityX and velocityY values are added onto the entity's position, which moves it, and the velocity is multiplied by drag, which has the effect of gradually slowing the object down. The thrust() function allows a quick way to accelerate the entity in the direction it is facing.

Next let's look at the Health component:

The Health component has a function called hit(), allowing the entity to be hurt. When this happens, the hits value is reduced, and any listening objects are notified by dispatching the hurt Signal. If hits are less than zero, the entity is dead and we dispatch the died Signal.

Let's see what's inside the Weapon component:

Not much here! That's because this is really just a base class for the actual weapons -- as you'll see in the Gun example later. There's a fire() method that subclasses should override, but here it just reduces the value of ammo.

The final component to examine is View:

This component is very specific to Flash. The main event here is the render() function, which updates a Flash sprite with the body's position and rotation values, and the alpha and scale values it stores itself. If you wanted to use a different rendering system such as copyPixels blitting or Stage3D (or indeed a system relevant to a different choice of platform), you would adapt this class.

### The Game Class

Now we know what an Entity and all its components look like. Before we start using this engine to make an example game, let's look at the final piece of the engine -- the Game class that controls the whole system:

There's a lot of implementation detail here, but let's just pick out the highlights.

Every frame, the Game class loops through all the entities, and calls their update and render methods. In the addEntity function, we add the new entity to the entities list, listen to its Signals, and if it has a view, add its sprite to the stage.

When onEntityDestroyed is triggered, we remove the entity from the list and remove its sprite from the stage. In the stopGame function, which you only call if you want to end the game, we remove all entities' sprites from the stage and clear the entities list by setting its length to zero.

## Next Time...

Wow, we made it! That's the whole game engine! From this starting point, we could make many simple 2D arcade games without much additional code. In the next tutorial, we'll use this engine to make an Asteroids-style space shoot-'em-up.

Did you find this post useful?