Tuesday, December 17, 2013

Entity Component System

After long time of no post, i decided (read: this is a school assignment ), to post something about this new phenomenon, that is trying to take over the game industry. It's called Entity Component System. In my eyes, it deserves all the hype around it. After playing around with it, i don't think i can ever go back to OOP approach, in game developing. So, let me tell you a bit about it, then i tell you what i did

Entity Component System

Entity Component System, ECS, is THE new way to create games. In ECS you separate data and functionality. Instead of using deep inheritance that object oriented programming, OOP, offers, you just composite new entities to your world. Entity is nothing but an ID and container for components. ECS allows you to create truly re-usable code.
With ECS, you just add functionality to entity. This means, that difference between player entity and AI entity is just an AIComponent. If entity has AI component, then the system will drive it towards it's goals, if its player component, then it uses input to drive the entity.


Entity in this system is just an ID to refer from code and container to hold components. The functionality of the entity is composed with components. In game perspective, everything is entities. Items, players, enemies, world, you name it.


Components are data holders, data templates. Position component in 2D game, would have two integers, x and y. That's it. Component is data holder for all the data that is needed to produce the functionality of the component. In case of position component, that would be the location, a point in space. It's very important to understand, that components don't contain any information about implementation. It just keeps the data of this component for entity that it's attached to.


Systems build up the game engine. These provide the wanted functionality. Systems modify the data inside the components of entities. System can be some major part of the engine. There could be 2DRenderSystem, that looks for entities that has 2DSprite-component and position-component attached or there could be MovementSystem, that looks for entities that has position- and movement-components. Systems are only interested in those entities that has all the needed components for prosessing. 
System is the part of your code, that does the work. That's what makes things happen.

My testings

I have done some testings with this kind of system and it indeed looks good. I like the idea, that i can with one component, add the collision detection for certain item or that by setting Health-component to entity i can make it mortal. I don't have to write classes for all the different mobs and players nor i don't have to come up with some clever inheritance system.

world.RegisterRenderSystem(new TiledMapRenderSystem());
world.RegisterSystem(new BasicControllingSystem());
world.RegisterSystem(new TiledMapCollisionSystem());
world.RegisterSystem(new MovementSystem());
world.RegisterSystem(new ColliderUpdateSystem());
world.RegisterRenderSystem(new CollisionBoxRenderSystem());
world.RegisterSystem(new Camera2DSystem());  
world.RegisterRenderSystem(new Render2DSystem());

Entity mapEntity = world.CreateEntity();
mapEntity.AddComponent(new TiledMapComponent("Content/rpgtest.tmx"));

Entity player = world.CreateEntity();
player.AddComponent(new PositionComponent(0, 0));
player.AddComponent(new MovementComponent());
player.AddComponent(new SpriteComponent("Blue-Monster"));
player.AddComponent(new BasicControllerComponent());
player.AddComponent(new Camera2D(true, "", new Vector2(320, 320), new Rectangle(0, 0, 1600, 1600)));
player.AddComponent(new ColliderComponent());

That's example code from my test environment. It loads map, renders map and player entity. Allows movement, sprite, camera following, colliding with map..

Some links to check out!