Slick Forums

Discuss the Slick 2D Library
It is currently Tue Oct 15, 2019 2:25 am

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Tilemaps in ECS
PostPosted: Fri Nov 01, 2013 6:36 pm 
Offline

Joined: Fri Nov 01, 2013 6:29 pm
Posts: 1
How are tilemaps supposed to work the Artemis way?

I need to draw few layers, then draw sprites, then draw other layers.
But in all tutorials to Artemis I always find extremly simple systems.

I can have multiple tilemaps, each have sprites on them.
Normally i would go like this:

Code:
foreach map
   map.drawlowerlayers

   foreach sprite in map.sprites
        sprite.draw

   map.drawhigherlayers


How am I supposed to do this in Artemis? Where Map is an entity with some MapComponent and monsters are entities with some SpriteComponent, how can i process first some components and then others and then again the first components in one system?


Top
 Profile  
 
 Post subject: Re: Tilemaps in ECS
PostPosted: Sun Nov 03, 2013 5:43 pm 
Offline

Joined: Sun Jun 03, 2012 9:21 pm
Posts: 49
Not sure I'm understanding the nature of your problem, so wave your arms if I'm completely off.

I haven't worked with tile-based maps before, but I'd try:
  • An instance of MapLayer for each layer, with some notion of Z-ordering.
  • each mob/sprite should have a BelongsToMapLayer component. BelongsToMapLayer only holds the Z value of the layer, or a reference to the MapLayer, whatever floats your boat.

create a new manager "MapLayerEntityManager"
  • override all EntityObserver methods to intercept and partition the sprite entities according to the BelongsToMapLayer value: I'd use a Bag for each Z value.
  • create a method with something like "getEntitiesForMapLayer(MapLayer)" which should return a Bag<Entity>.

in your entity system "MapLayerRenderer":
  • Aspect should match MapLayer and whatever else is needed.
  • Hold onto the MapLayerEntityManager in initilize()
  • override inserted() and deleted() to sort the MapLayer entites according the Z value.
  • override processEntites() and feed each MapLayer entity to process(), after every process(), call a separate processLayerEntities(MapLayer) to draw the sprites (get the entities from the manager).

_________________
agrotera: bytecode-weaving antiboilerplate lib for artemis. | artemis-odb: bugfixing and performance optimized fork.


Top
 Profile  
 
 Post subject: Re: Tilemaps in ECS
PostPosted: Wed Dec 18, 2013 12:08 pm 
Offline

Joined: Sat Mar 03, 2012 3:51 pm
Posts: 3
Sorry for hijacking this thread, but it deals more or less with the problems I'm having with my game. I also read the sticky post about not asking questions regarding game design, but I see a few posts here about that, so I'll try :)

I'm using a tilemap in my game like the OP, although I only have one layer. My problem is updating only the section of screen you're currently seeing. How do you go about this with atemis-odb? To be clear, I have a tilemap of, say, 500x100 tiles, but my camera only covers 32x24 at any given time. How do I make the systems update only those? Preferably, I'd like not to fill a collection first and then pass that collection to every system, as I see this as not performant, but maybe that's is my only option with an ES approach?

Thank you in advance!


Top
 Profile  
 
 Post subject: Re: Tilemaps in ECS
PostPosted: Wed Dec 18, 2013 8:20 pm 
Offline

Joined: Sun Jun 03, 2012 9:21 pm
Posts: 49
Is each tile represented by an Entity? If so, I'd add a Cullable component with a boolean field "culled" with a matching CullingSystem (maybe built around sweep and prune. and have any related systems check the field before processing the entity.

Quote:
Preferably, I'd like not to fill a collection first and then pass that collection to every system, as I see this as not performant, but maybe that's is my only option with an ES approach?


A collection of what? EntitySystems already hold on to all entities matching the Aspect and are updated whenever an entity is created?deleteted or had components added/removed.

_________________
agrotera: bytecode-weaving antiboilerplate lib for artemis. | artemis-odb: bugfixing and performance optimized fork.


Top
 Profile  
 
 Post subject: Re: Tilemaps in ECS
PostPosted: Wed Dec 18, 2013 9:43 pm 
Offline

Joined: Sat Mar 03, 2012 3:51 pm
Posts: 3
Well, I don't know if I should represent each tile with an Entity. My game can have up to a few million of those, that's why I'm asking for directions :).

Your idea will still "check" all the entities in all the systems (so you're still iterating over all entities on every system). Also, I don't really need any algorithm to check the tiles that need to be rendered, I can easily do that with the camera bounds.
Quote:
A collection of what? EntitySystems already hold on to all entities matching the Aspect and are updated whenever an entity is created?deleteted or had components added/removed.

Maybe I explained myself poorly. What I mean is that the only solution I see to this problem is, before starting to process any system, I first get the list of entities, I add those to a collection, and then I pass that collection to the systems to process. I'd like to do that without having to create a collection first if possible.

Thanks for your responses :)


Top
 Profile  
 
 Post subject: Re: Tilemaps in ECS
PostPosted: Fri Dec 20, 2013 11:52 pm 
Offline

Joined: Sun Jun 03, 2012 9:21 pm
Posts: 49
Why don't you want to create a collection though? If you reuse the same instance, it doesn't incur a higher cost to pass a collection than any other object. But it sounds somewhat headachey since you'd need to make sure that all entities that are cull-aware have matching aspects - or you'd need to allocate a Bag/collection for each unique aspect.

Without being familiar with your code, I'd consider having a culling system that populates entity ids into a BitSet; interested systems would then check if the entity.getId() is present in the bitset before processing (extend EntitySystem and do the check inside EntitySystem#processEntities). If it turns out to be a bottleneck (though I doubt it), consider tracking whenever an entity goes from being culled/not culled - and manually manage the Entities held by affected systems (by calling EntitySystem#enabled and EntitySytem#disabled, from your culling system).

_________________
agrotera: bytecode-weaving antiboilerplate lib for artemis. | artemis-odb: bugfixing and performance optimized fork.


Top
 Profile  
 
 Post subject: Re: Tilemaps in ECS
PostPosted: Fri Dec 20, 2013 11:58 pm 
Offline

Joined: Sat Mar 03, 2012 3:51 pm
Posts: 3
There is no code yet (well, there IS some code, but nothing ES yet, that's why I'm asking all this questions before implementing anything :D)

I'll try your suggestions, thank you very much for your time :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 14 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group