Pages: [1]
  Print  
Author Topic: Enitity question  (Read 1438 times)
Warmist
Guardian
**
Posts: 110



View Profile
« on: June 15, 2010, 10:28:07 PM »

I have this approach that everything on map is an entity. However this raises the question how do you store them? Mostly I used "All entity list" and then each turn iterated over it, and drawn all that are in view (later used sub maps with respective lists). One time i used each tile approach - every tile has a pointer to vector of entities, memory saving approach was to delete the vector if it's empty. Now I think is there a better way? What you use? Which of these two is better?
Logged
Jotaf
Global Moderator
Master
*****
Posts: 1183


View Profile
« Reply #1 on: June 16, 2010, 12:21:21 AM »

If you're gonna have a huge density of entities, you're probably better off considering a sparse mapping structure for your entities (a map/dictionary). I don't have a lot of entities per map currently, so for me a simple list works just fine Smiley

So, maps/dictionaries. Each key-value pair has a key that identifies the tile (a tuple/pair of its coordinates), and the value is a pointer to the entity. Since you brought up the issue of per-tile vectors, you want multiple values with the same key (many entities at the same tile), a multi-map/dictionary. Dictionaries are easy in python, though to turn one into a multi-dictionary you need a tiny class (see here, it's pretty cool!). In C++, if I'm not mistaken, the STL already has a multi-map class, so you're set Smiley

This way you can iterate all entities if you want to (to update them or something), or just find the ones in specific tiles. Using STL's multi-map it should be relatively painless to move one entity from a tile to another by just changing its key. Using this python multi-map, you have to remove it from the list at one key and append it to the list at another key.
« Last Edit: June 16, 2010, 12:27:50 AM by Jotaf » Logged
Warmist
Guardian
**
Posts: 110



View Profile
« Reply #2 on: June 16, 2010, 01:19:01 AM »

Great ideas. Just one question though: is it possible to have it sorted. Because per tile vector(or better list) can bet autosorted (by just adding to end or start) by drawing priority that really helps to make drawing code simple later on.
Logged
Jotaf
Global Moderator
Master
*****
Posts: 1183


View Profile
« Reply #3 on: June 16, 2010, 02:18:38 AM »

Well, STL's multi-map is supposed to maintain a consistent ordering of the elements, although I'm not sure if you can manipulate that order easily (do you need to? as long as the drawing order is consistent across frames I see no problem in having that order be arbitrary). Each value of that python multi-dictionary is a simple list, so the order of elements in it is consistent and can be changed easily. Smiley
Logged
Pages: [1]
  Print  
 
Jump to: