mardi 30 avril 2019

How do I prevent adding an object in multiple vectors?

There are some objects that are Drawable and some that are Movable.
All drawable objects are movable.
I store all the drawable objects in a vector called drawables and movable objects in a vector called movables.
I also have vectors ships and bullets which contain objects of type Ship and Bullet respectively. Ship and Bullet both are Movable

The thing is, that each time I create a Ship I have to add it in all the vectors i.e.

drawables.push_back(ship);
movables.push_back(ship);
ships.push_back(ship);

I have created seperate drawables and movables vectors since I have a draw() function which calls the draw() method of all objects in the drawables vector. Similarly, I have a move() function which calls the move() method of all objects in the movables vector.

My question is, how do I change the structure to prevent adding the same thing in different vectors. I also need to remove objects from all the vectors once it's purpose is done.
For example, once the bullet hits someone or moves out of the screen, then I'll have to remove it from the vectors drawables, movables and bullets after searching it in all three vectors.
It seems like I'm not using the correct approach for storing these objects. Please suggest an alternative.

This seems more like a software engineering question than a coding question. Please migrate the question to other forum if necessary.

Thank you.

Aucun commentaire:

Enregistrer un commentaire