Entity Component System – what the hell is that thing? It has entity, It has component, and system. But what the hell is that?
Cats are funny
I am developing game right now, it’s called Call Of The Death Mask, and about month ago I had a headache. Reason of my headache was the question “How I can make zombies fly?”
What to do if your game designer want to make all treasures chests in game to have teeth and attack player character? And if you ask him “Dude are you crazy?” he will replies to you with magic word “Mimic”
Mimics are fictional living creatures, that looks like ordinary things, but they can attack you.
Mimic by BenWootten on DeviantArt
At this point you probably have class called Chest and it can’t attack anyone, because it’s not belongs to enemy class hierarchy. Damn what to do?
Entity Component System gives good approach that works ideally for games. You create class called Entity, and construct every object in game by filling Enitity with Components. Component is just some property of Entity. For example in my game all zombies have:
- Position component (x, y, z – Vector)
- Sprite component (Zombie.png – image)
- Rotation angle component (x, y, z – Vector)
- Zombie AI component (to behave like zombie)
Ok, so how to make all treasure chests into mimics at some point of level. Treasure Chest will have those components in my engine:
- Position component (x, y, z – Vector)
- Sprite component (Chest.png – image)
- Rotation angle component (x, y, z – Vector)
- Item component (to give that item to player, when he will open it)
And now we just add Mimic AI component to Treasure Chest at runtime, and he will attack player at right moment. And if some magic spell can make Mimics into Treasure Chests, then we just need to remove Mimic AI component at game runtime for that Treasure Chest, or for all of them (for cool spells that applies to all game map)
What stands for System at this pattern? System handles components for every entity. For example if player character have weapon component, then UI System must get weapon component and render it on screen.
Weapon component must have necessary data for UI System. In my game every weapon have two components:
- On map sprite component (it shows if weapon dropped to the ground)
- Iron sight component (it shows when player get weapon)
Shotgun weapon component at map, and another in player’s character hands, one rendered by scene renderer, another by ui renderer.
Links about ECS pattern:
https://en.wikipedia.org/wiki/Entity_component_system
https://www.youtube.com/watch?v=NTWSeQtHZ9M