# Tilemaps in a Entity System Framework?

I have been reading up on Entity System Frameworks specifically Artemis. I am trying to decide if it is right for me. I strictly work on tile based 2d pixel art games, and I don’t think they will ever be that resource intensive. I have always used standard OOP with a lot of inheritance in the past.

My understanding of the Entity System Framework right now (I am not sure if I grasp it completely yet) is:

• Entities are nothing but ID's
• Components are nothing but dumb data added to an entities component pool
• Systems are update functions that are attached to the world to handle every entity that matches the Systems component signature

If my understanding is correct then I am having quite a bit of trouble conceptualizing adding tilemaps and AI Behavioral Trees into this framework. I will ask about AI in the future.

Should a tilemap be built into this framework? Or should it be kept separate to make it easy to generate with a tilemap editor?

If the tilemap should be built into this framework, is each tile a different entity? And the tilemap a system? Or is the tilemap itself a single entity with inheritance built off of it?

If the tilemap is sperate, what would be the best way of collision detecting entities against the external tilemap?

I understand that multiple options that I have listed might be correct, but if anybody has done this in the past they might be able to shed some light on my confusion. Maybe there is another alternative that I have not thought of?

Thank you.

• A completely random note, X/Y coordinates works as IDs as well. Not that I'm suggesting you implement it in your entity system, but rather, you can attach effects to your tiles in the same manner as you would in an entity system. – William Mariager Jul 20 '12 at 6:53
• Hey, just passing on Byte56's suggestion: components don't really need to be dumb data holders. Sometimes it would be useful if they could actually operate that data; vectors, for example. – jcora Jul 20 '12 at 11:22
• I really like this suggestion as well. I can add tilemaps into the entity framework without having to separate the tilemap helpers from the data, or having to break up the tilemap into multiple components / entities. – Spencer Marr Jul 20 '12 at 16:31
• Tilemap is just another component, and collision system and renderer system handles collisions with tilemap and rendering of tilemap – Kikaimaru Jul 20 '12 at 20:16

## 2 Answers

I implemented an entity component framework (similar to Artemis) after I'd already been in development for a while, but I don't think I would have done things differently if starting from a blank slate.

I have my world totally separate from the entity framework. It just didn't make sense to me to convert the world into some sort of entity or collection of entities. My world is 3D with cubes, but I believe the same applies for tiles. Entities make up everything else in the world, but the terrain is separate. However, when cubes are removed, they do spawn "material" entities.

Collision detection isn't too hard to tie in. Your world would likely have isSolidAt(x,y) type methods that your collision system will utilize. Honesty, I've said this once already today in a different answer, do whatever makes the most sense to you. You're not breaking any rules by making the tile map separate or making it an entity. Whatever you can wrap your head around is what's best. For me, that was keeping the world separate and making everything else entities.

• Say my map is an entity. How would another entity go about accesing the map's isSolidAt method? – Gerardo Marset Oct 4 '13 at 0:26
• Storing the map as an entity probably isn't a great idea for just this situation. I suppose you've have to communicate directly with it in this situation. The system in charge of the map entity would have access to the map entity, and be able to retrieve information from that entity when it's requested. – MichaelHouse Oct 4 '13 at 1:51

I added tilemap as a separate component to entity (with all tiles inside) and there is also separate renderer for tile map, so I can render it in 3d or in 2d. To create all tiles as entities is possible but to expensive.

something like that:


Entity e("map");
e.addComponent(new Tilemap("1.xml"));
e.addComponent(new TilemapRenderer2d(graphics));

tile map is something like that
class Tilemap
{
array of tiles;
getTileAtPosition();
isWalkable ; etc;
}

class ITilemapRenderer
{}

class TilemapRenderer2d
{
draw()
{
foreach (tile in owner()->component()->tiles())
{
if (tile == ground) ground_sprite->draw() ....
}
}



benefit in separating drawing from logic is base idea of MVC pattern

• The entire tilemap is contained within a single component? Is it attached to some sort of game environment entity / master entity? I can see a pretty large benefit gained by separating the render-er from the tilemap component. – Spencer Marr Jul 20 '12 at 16:26
• Thank you for updating your answer! I love code snippets. – Spencer Marr Jul 20 '12 at 20:24