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.