简体   繁体   中英

Choosing the best stucture for my list of players

I am in trouble choosing the most pertinent structure for my data, here are the explanations:

I am actually working on a game project for school, a Bomberman like game in c++. I am designing the Map object, who contain Bombs, Boxes, Players and Walls. The map is 2D, I have already 2 containers of type:

std::unordered_map<int, std::unordered_map<int, AEntity*> > *UMap;

One contain Walls, the other contain destructible objects (Bombs, Boxes). I have already discussed this choice here -> Choice of unsorted_map . It's mainly for fast access time and because there can only be one element per map's box.

Now as the title suggest I'am in trouble choosing a data container for my players, because there can be multiple players on a single map's box, unordered_maps can't be used.

In a first time I was going to use a std::list<AEntity*> sorted with std::sorted , AEntity containing the entity information (coords), but while coding my

playerOn(const int x, const int y);

function I found it was a poor choice. I can't retrieve fast enough which player(s) is on the given box using dichotomies, and if there is no player of this box it's a waste of time.

How should I store my (AEntity)Players in order to be able to retrieve them fast

(On of the itchy thing is that there can be more than 500 player at the single time, on big map, that's why I am looking for optimization)

I am running out of brain juice. Thanks for your future answers.

Edit about my probleme

It's mainly because I want to know if there is another solution to go trough my whole std::list of player to find if there is someone on box(x, y). Looks slow and not optimized, but i can't figure another way to do this. (I can change container type if needed)

First of all, before trying any optimization , you should profile your code to detect where is the bottleneck. You will be surprised. Once that said :

1) Your unordered_maps seem like a lot of over-engineering. The answers provided in your previous post are true but I think it is useless in your case. You absolutely do not care of a O(log(n)) cost when n = 500. Just make a vector of Entities* . It is much enough.

2) You seem to be falling in the god object anti-design pattern. A bomberman game is an excellent project for studying OOP, as it teaches you to avoid the God Object design pattern. Make sure each class does its own business and no class handles all the logic (I suspect your class Map or Game has too much power).

a) Create a vector of players, a vector of bombs, a vector of walls, a vector of fires, etc.

b) Map should be a 2-dimensionnal array storing list of pointers to the entities that are present on each Map's box. (Each Map[i][j] holds pointers to all the elements that are on the box of index (i,j)).

c) Bombs should create fire elements.

d) Your central routine should be something like :

while(gameContinued)  
{  
    for each entity in the game  
    {  
        entity.Update(map);  
    }  
    game.Render();  
}     

Bomb update consists in making the bomb's texture rendering dynamic and create fires if delay is over-due (and update the Map with the fires pointers accordingly). Fire update consists in nothing except vanishing if delay is over-due (and update the Map). Player update consists in moving through keyboard event handling (and updating their current internal x and y values ), creating bomb if needed, dying if for their position (x,y), there is a fire in the Map[i][j] list of entities.

3) In most of my students implementation of bomberman, the only issue they can get is with textures and deep copy of objects. Make sure you have a Texture Loader (using Singleton Pattern) to avoid loading multiple times the same textures, and you should be fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM