asteroids! shots, arrays, entity system :o
i have a clone of asteroids without the asteroids and the bullets atm.
i am wondering what the best was to update everything in the game was?
i was thinking aout having an aray of CEntitys and then when someone shoots i have my CShot class that derives from CEntity sort of thing?
what do you think?
Sounds good to me. Then every frame you would only have to loop through the entity list and let each one render and update themselves.
You could have a std::vector of the base class pointers.
Look here for vector methods.
#include <vector>...std::vector<*CEntity> entityList;
Look here for vector methods.
I've also been working on an asteroids clone for a while now and instead of using a vector to manage the memory, I used a std::list. This is because you'll be deleting elements from the unordered memory and don't really need random access. Deleting elements from the middle of a vector is a very slow process.
So, I came up with something like this:
So, I came up with something like this:
#include <list>std::list<CEntity*> entityList;// Helper functions to manage all entitiesvoid UpdateEntities();void DrawEntities();void AddAsteroid(int x, int y, ... );void AddBullet(int x, int y, ... );//...
Trick for quickly deleting an element in the middle of a vector:
*iterator = vector.back();
vector.pop_back();
This works assuming the order of elements doesn't matter. I'm pretty sure it doesn't in this case.
This pretty much becomes: *pointer = *--vector.data_end; It would also normally invoke the destructor, but pointers don't have one. It's so cheap, it's practically free.
*iterator = vector.back();
vector.pop_back();
This works assuming the order of elements doesn't matter. I'm pretty sure it doesn't in this case.
This pretty much becomes: *pointer = *--vector.data_end; It would also normally invoke the destructor, but pointers don't have one. It's so cheap, it's practically free.
Quote:Original post by smart_idiot
Trick for quickly deleting an element in the middle of a vector:
*iterator = vector.back();
vector.pop_back();
This works assuming the order of elements doesn't matter. I'm pretty sure it doesn't in this case.
This pretty much becomes: *pointer = *(--vector.data_end-1); It would also normally invoke the destructor, but pointers don't have one. It's so cheap, it's practically free.
I'm not completely clear on how this deletes something from the middle of the vector.
*edit: stupid upgrade eats tags.
*edit2: Never mind, figured it out. Presumably, iterator already points to the element to be deleted?
CM
Quote:Original post by Conner McCloud
Presumably, iterator already points to the element to be deleted?
That's the assumption. Also, you've quoted my bug where I goofed up combining the two lines. Teh noes!
how do i set a custom size for my vector or list
like i only want 100 shots
then i have an int shotnumber and every time i want to create a new shot i so ShotsVector[shotnumber].Init( x, y, angle); shotnumber++; if shots over 100 shots = 1;
(EDIT) sorry i did not see all those other posts hadnt refreshed page in ages
oh and david, http://www.bringyou.to/games/ that asteroid game uses an array of structs and i tried that before but with classes and it didnt work.
like i only want 100 shots
then i have an int shotnumber and every time i want to create a new shot i so ShotsVector[shotnumber].Init( x, y, angle); shotnumber++; if shots over 100 shots = 1;
(EDIT) sorry i did not see all those other posts hadnt refreshed page in ages
oh and david, http://www.bringyou.to/games/ that asteroid game uses an array of structs and i tried that before but with classes and it didnt work.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement