Extending the Simple CPP Game Engine

Engine Architecture

Simple CPP Game is implemented with an Entity Component System that handles complex behavior of the actors in the game world. Rather than traditional inheritance, where game objects have an “is-a” relationship to classes that provide game behavior, Entity Component Systems define game objects that have a “has-a” relationship to components that interact with Systems that provide game behavior. This creates a much more modular and scalable framework for game behavior (that is supported even by languages that disallow multiple inheritance) without sacrificing the performance and ecosystem of a popular language like C++. In essence, Entity Component Systems bring the best parts of Duck Typing to the part of the program that needs it without sacrificing the value of static typing elsewhere in the codebase.

Adding New Behavior

The Entity Component System is managed by the EntityEngine class. The game creates and maintains a single instance of this class throughout the lifetime of the game. Behavior can be added to the game by defining new Components and Systems that control the desired new behavior and adding them to the engine.

Entities and Components

The primary objects in an Entity Component System are the entities and components.

Entities are little more than “bags” of components - groupings of characteristics that are together treated as a single entity (hence the name). When extending the functionality of an Entity Component System, new entity types are not defined; instead, new components are created that entities can own.

Components serve as components (hence, the name) of entities. They mark which systems should act on the entity and store any state data related to the systems they are built for. For example, the RenderComponent stores sprite data and marks an entity as belonging to the RenderSystem. Custom components can be anything from color information to ball spin to paddle speed. Once a custom Component has been defined, simply add it to the entities it applies to and define a system to act on it. Speaking of systems…

Entity Systems

Entity Systems are implementations of the EntitySystem class. Designing an Entity System requires two major components:

1. Define the Entities the System Acts on

Each Entity System defines some behavior that components that match its criteria and “quack like a duck” will exhibit. In Simple Cpp Game’s implementation, an EntityFamily is used to filter the entities in the engine to those that are appropriate for a given system. A custom system should pass this family to the appropriate constructor of EntitySystem or whatever subclass thereof it inherits from (such as IteratingSystem). The simplist implementation of EntityFamily (and the only one implemented in Simple Cpp Game at this time) is the ComponentFamily, which allows the system to specify a set of Components each entity it will process must have.

2. Define the Behavior of those Entities

Each Entity System defines one class of behavior for its constituent entities. In the base game, VelocitySystem defines how position updates from velocity, PlayerInputSystem defines how the player paddle moves given input, etc. Each custom system should implement a similarly narrow scope of functionality - this is what gives the Entity Component System its flexibility and modularity. Custom systems might add score counters, terrain, or the next new feature that no Pong clone has ever seen before.

The most common way to implement an Entity System mis to inherit from IteratingSystem. This allows the developer to just provide a ComponentFamily and an implementation of the process() method that processes one entity at a time.

Conclusion

Component Entity Systems are an incredible piece of technology that bring the modularity of dynamically typed languages to the statically typed environments needed for performance in game development, allowing for modular systems, efficient code development, and performant end solutions. With the steps here, you’ll be extending the Simple Cpp Game engine to be writing custom functionality in no time!