Hello guys,
I came here looking for an advise on what data structure to implement on my game. I’m working on a shooter game. I am concerned with the way I’m designing this program. Briefly explaining, there will be player, player bullet, enemy, enemy bullet, and item objects. The update method in the game loop will be calling methods to update these game objects (I named the base class GameObject), detect 2 types of collisions (One is player with enemy, enemy bullet. Another one is player bullet with enemy), check if it should add an enemy on the screen, and so on.
I have a class called “ObjectManager” that holds these GameObjects. Originally, I was thinking of holding all of them in one Hashtable. As I was starting to write the code for collision detection on player’s bullets, I realized putting all GameObjects together would just produce unnecessary amounts of loop. To make this operation more efficient, I thought of dividing each GameObject in the corresponding type of collection. I just don’t know if I should go with ArrayList or Hashtable.
The way I handle when to throw enemies on the screen is by incrementing a counter variable every frame to check when. I prepare a ArrayList of enemy objects which holds an int to mark when to appear during game. The update method in game loop calls this method to check when to register enemies by comparing the counter with this int value each enemy contains. It would not new and just add this enemy object to the collection in ObjectManager, then remove from this ArrayList.
Here are some points I want to consider upon deciding…
Loop through to get the object and call update method every frame.
Get objects during collision detection.
Remove if the GameObjects except player go out of screen. (done within update method in each GameObject)
Remove if an enemy object dies.
Add when the player shoots. (let’s say about 250’ish bullets at most?)
Add when the enemy shoots. (I would also say about 250-300 maximum)
I want to choose based on performance and efficiency. According to my test allocating both ArrayList/Hashtable with String, ArrayList seems to be faster… though I thought this hashing algorithm was supposed to be faster. Also, like Hashtable, it would make my life easier if I could use keys to fetch them and keep the size as the number of objects in it.(non-contiguous memory)
I hope my description is not so confusing. I would appreciate if anyone could throw some opinions.
Thanks