Projectiles... arrays?

Started by
1 comment, last by dirtypope 22 years, 5 months ago
hello, i am ready to add some projectiles to my game, and i was wondering how the best way to do this would be... would my best bet be somthing along theese lines? Psuedo code --Mouse is Clicked-- //check for assigned projectiles... // d = max ammount of live projectiles allowed for(int d=0; d<1000; d++ { if (gun_proj[d] == 0)//using 0 as null indicating that projectile is not active. gun_proj[0] = 1; // make it active } then after i do that, i will have to set that individule projectiles heading to another array... does that sound about right, or am i going about this all wrong? Edited by - dirtypope on November 5, 2001 11:38:30 PM
Theres no such thing as stupid people... only people with under clocked brains. -dirty
Advertisement
I would personally use a linked list for projectiles. Forexample in the ''RocketLauncher'' class I would have a linked list of ''Rocket'' objects. Everytime the player clicks the mouse button you add a ''Rocket'' object to the list, and when the rocket explodes you remove it from the list. Then all you have to do every frame, is to traverse the list and update each rocket and render it.

I''m yet to master writing my own template class for linked lists. But you can always use the ''vector'' or ''list'' template classes.

I don''t really see anything wrong with your method either. The Pro''s are: you don''t have to keep creating and destorying objects which could mean more efficiency. The Con''s are: You don''t exactly know how many projectiles are gonna be alive at one time. If you make your array too big then you are wasting memory.

Hope that helps
I agree with the above post. A linked list is the best way to go about it.

This topic is closed to new replies.

Advertisement