I had been using objected-oriented programming since learning my first language. At some point, I always ran into the same issue where my inheratance trees quickly turn into a flaming, convoluted mess. I eventually found that Entity Component System removes that problem by assigning components to entities that are all impacted by the same system. I also frequently use C++ 17 features, particuarly unique_ptr and auto keyword.
Here's a little primer on Entity Component Systems:
Entities are ONLY a simple unsigned datatype like std::uint32_t. A component is a simple struct that only contains a collection of variables. A system refrences components and applies a universal set of rules upon it. In the example of the game above, the blue box is an entity that has a transform component (x,y,z) and is set as dynamic. In a system called gravity, it should check two things:
- It has a transform (otherwise it wouldn't be able to refrence the Y position of the entity).
- The entity is dynamic (because a static entity cannot be affected by gravity).
If these two conditions are fulfilled, then there should be a gravitational field going dornward.
Each entity is added to a registry which keeps a record of all components attached to an entity.
Systems check every entity with a collection of components and apply a universal set of rules upon it.
Components are a set of variables. An entity can have multiple of these of once. Each entity has their own inputs for the component;
Prefabs create an entity that already has a specific group of components already.
Managers contain a set of systems and describe how they interact with each other.
The code is written using Allman indentation (and the occasional K&R for small statements). The code uses PascalCase.
Type | Convention |
---|---|
Global Variable | _VariableName |
System | SSystemClass |
Manager | MManagerClass |
Component | CComponentStruct |
Prefab | PF_PrefabStruct |
Helper | HHelperStruct |