1
\$\begingroup\$

How can I make my enemies shoot bullets in intervals?

I already have the enemy shooting bullets, however my problem is that it shoots the next bullet instantly, making it look like a line instead of bullets with gaps between them.

I tried to use gluttimerfunc but it didn't work since it just delayed the trigger of the bullet but once the trigger (bool statement) was set to true, it would shoot the max bullets on screen instantly.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ You'll need to give him a cooldown, like a int where you store the time, how long he has to wait. Every update() you fire and set it ti like 500, if its zero, and if its not zero, then just decrement it. (To be more precise you could use a double and decrement it by your delta and check <=.0) \$\endgroup\$ – tkausl Dec 8 '14 at 19:21
  • \$\begingroup\$ FRICKING STACK EXCHANGE! It pops un-answered and un-checkmarked answers to the top and makes them appear new. Then I spend time answering them even though the OP says Got it working already. Dude!! Answer your own freaking question then so others can be helped by it, or delete it, or don't use SE! \$\endgroup\$ – badweasel Dec 16 '14 at 18:01
1
\$\begingroup\$

You need to store some c (c++) properties for the enemy to support this. You have to ask yourself:

Do you want each enemy to shoot in sync? Or off sync?

Do you want shooting to be at a set interval or the timing to be somewhat random?

Do you want each enemy to have shooting triggered by some other event so that shooting can be turned on or off? To give the enemies the appearance of intelligence?

If you want them to shoot in sync you can have a single struct for the timing with members like this:

typedef struct _FireRate {
    float rate;
    CFTimeInterval lastFire;
    int state;
} FireRate;

Then preset your fire rate somewhere in initialization. In your game loop when updating things do something like this:

fireNow = 0;
if (fireNow.lastBlink+ fireNow.rate < timeThisRound) {
    fireNow.state = 1;
    fireNow.lastBlink = timeThisRound;
    // if you want the shooting to appear random
    fireNow.rate = rateMinimum + RANDOM_0_TO_1() * rateVariance;
}

Then when deciding to fire a shot or not just check if fireNow.state==1.

If you want each individual enemy to have it's own way of doing things add some shooting properties to it. Surely you have properties somewhere for the enemies like where it is on the screen? I would create a big struct with lots of properties for the enemies like position, rotation, and if it's moving and what direction. And for shooting add to that a shooting trigger and something that hold the last time it fired a shot and how long until the next shot is fired. If you want the shooting frequency to appear somewhat random change the rate each time the shot is fired.

It's amazing how REAL your game will appear once you treat enemies like particles that can shoot bullets which are also treated like particles, and give them each their own properties and triggers for actions.

I'm going to add this: Make it an engine. Since you're in c you can also have a fireRate struct inside you're enemy struct. And you can stick the fire rate update in a method where you pass a pointer to the fire rate property of each enemy. Then when you loop through enemies do updateFireRate(&enemy[x].fireNow). In the method you'll use -> instead of the . dot to access members. After calling the update check enemy[x].fireNow.state to decide if it's time to fire or not.

\$\endgroup\$

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