1
\$\begingroup\$

I currently have a turret that can fire projectiles. I can control the delay between shots (fireDelay), and I can control the time needed to reload the magazine of the gun(reloadDelay). I do the simple solution of, when the user tries to fire, see how long has passed since he last fired, and if it has been long enough, it fires again. Same for reloadDelay.

But now I wanted to make it so clicking fire once would fire 3 bullets, with delay between, akin to the Burst Fire Mode on an M-16, like FPS genres.

A simplified overview of what happens right now:

  1. User clicks mouse.
  2. The program enters the fire function().
  3. Fires a bullet if sufficient time has elapsed, and enough ammo remains.
  4. Exits the function, and only enters again when you try and fire again.

So how do you make it fire again in the future without step 1 happening. I have a messaging system built into the engine, so my current solution would be for the gun to fire a bullet, and add 2 messages to the message list with the fire delay attached, and the message manager would send the messages to the gun to fire when the message times had expired. I didn't like this though, because I thought the gun should not be sending itself messages in the future to fire, and that's not what the messaging system was meant for initially, although it does work. I'm just wondering if there is a better solution to implement this?

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Maybe inside the core game loop, check in your turret if remainBulletsToFire > 0 per 0.33 seconds. Fire a bullet and remainBulletsToFire--; if needed. If remainBulletsToFire == 0, remove the turret from checking inside the core game loop. Is this approach viable? \$\endgroup\$
    – S.C.
    Jul 23 '14 at 3:42
1
\$\begingroup\$

The solution can be pretty simple - just add a field or flag and dont release until n bullets of your burst have been fired. in weapon:

int burst = 0;

when hadling event:

 void onFireButtonPress() {
    //previously simple fire();
    burst = burst > 0 ? BULLETS_PER_BURST : burst; //or some other behavoiur like burst += 3 etc.
}

in game loop:

if(wepon.burst > 0)
 weapon.fire();

in fire() method:

if(fired)//all conditions met (time, magazine etc.)
  burst--;
\$\endgroup\$
1
  • \$\begingroup\$ Ok, this works, but my game has 10-15 ships, each with 5-10 turrets. So I guess I can create a turret component, which gets added to a list of turrets, which is then looped over constantly to check if they should fire. \$\endgroup\$
    – user46877
    Jul 25 '14 at 19:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.