#### Archived

This topic is now archived and is closed to further replies.

# Multiple blasts from the ship?

This topic is 6229 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey guys. I am doing a horizontal shooting game ( similar to Gradius I guess) and have a couple of frames for my laser coming out of the ship. What I want to know is how to let the player shoot multiple blasts, not just have to wait until one goes off the screen. (Using C++ by the way) I don't need the code for it, just wanna know the algorithm ( or the conceptual way to do it, code would be appreciated tho, hehe)used, thanks! Edited by - randomDecay on October 30, 2001 1:08:32 AM

##### Share on other sites
A linked list of laser blasts might work, not sure what kind of performance it would yield though. An array of lasers would definitely work because I''ve done it in a game a made a while ago. But the number of lasers is then limited to how large you make the array. And I''m sure there are other ways, but I''m no expert so I don''t think of them right away. Hope I helped!

--Buzzy

##### Share on other sites
Make the lasers autonomous entities that can create, move and destroy themselves (essentially objects as per Object-Oriented Methodology). Then whenever the player fires a laser, the laser takes care of itself while the player goes ahead and can fire more lasers.

##### Share on other sites
I follow you on the array, but could you give me a tiny example of how to do it? thanks

##### Share on other sites
Have a struct or object to represent an individual laser shot, containing, for example, a position, a velocity, and a flag indicating whether its currently on screen (being fired). Create an array of these structs/objects. When the player wants to fire, search the array for a shot that isn''t currently on screen (if there aren''t any unused ones the player can''t fire just yet). Initialize the shot''s position and velocity, and set the flag to indicate that it''s currently in use. When a shot goes off screen or hits something or whatever, set the flag back to indicate that it''s no longer in use.

##### Share on other sites
Not sure if you should listen to any advice i''ve got cos i''m only a few weeks work ahead of you but i''m using a modified array. I looked at using a linked list but since the number of *bullets* would be small <100 the array should be more effective.

For every lazer blast i create a sprite which has a x,y,width,height,LP to the surface, active flag etc and is added to a contact list (also stored in an array).

when the fire button is pressed i search through the array for a non-active bullet and then i initialise it, activate it and add it to the contact list.

Every turn i test all the active bullets for lazer going off the screen or hitting something, when that happens i remove it from the contact list and set it''s active variable to NULL. I guess the bad thing is that the storage is set at compile time but then again there will never be enough bullets to worry about that.

There are optimisation in there to speed the process up but you shouldn''t really need them or you should be able to work them out for yourself.

By the way i found that using lots of c-style pointers made the whole thing easier to implement , might be just me though.

hope this helps,
zipless

##### Share on other sites
In a lot of games, your laser power goes down as you fire, and when you run out of power, you can't fire until it's recharged sufficiently. This prevents you from firing far too many lasers.

If the lasers will be destroyed after travelling a finite distance (they reach the end of the screen, say), then you can calculate how many can be on the screen at a time given their lifetime and the firing rate.

Given a rate of 10 shots per second, and a lifetime of three seconds, there can be a maximum of 30 shots visible. Therefore, your array need only have room for 30 lasers.

But, you should know that linked lists aren't the spawn of Satan. If you maintain a linked list of free nodes, then an expensive malloc() operation isn't needed until you want to allocate some new nodes. To add a node to the list requires only 4 assignment operations:
  temp = first_free->next; first_free->next = first_laser; first_laser = first_free; first_free = temp;

For each blast, you must also assign the position, velocity, some flags, a sprite pointer, which might take, say, six assignments. So the linked list accounts for 40% of laser-allocation time. Even that isn't a problem, since you aren't creating a new laser on each pixel, each scanline, or even each frame.

Oh, and the contact list/array. This doesn't need to be updated when you add a new laser. Instead, put &first_laser, &first_enemy, &first_hamster (and whatever) in the contact list. Then, when you want to walk the contact_list, follow each kind of contacter to the start of their respective lists.

'Nuff said. I'll enjoy watching you live, demon.

Edit: Formatting.

Edited by - Mayrel on October 30, 2001 7:02:52 AM

Edited by - Mayrel on October 30, 2001 7:03:46 AM

1. 1
Rutin
42
2. 2
3. 3
4. 4
5. 5

• 9
• 27
• 20
• 14
• 14
• ### Forum Statistics

• Total Topics
633385
• Total Posts
3011605
• ### Who's Online (See full list)

There are no registered users currently online

×