Side-scrolling shooter question
Over this past summer me and a cousin of mine have been colaborating to make a side-scrolling shooter similar to Contra using DirectX. We have a large portion of it working, the character runs, jumps, and fires nicely and we have some collision detection. Currently I am working on some enemy AI. A rifleman's AI in particular. I have it so that whenever the rifleman is on the same screen as the player he is firing at the player. I made it so that the program determines a net slope between the 2 characters to determine where the bullet should fly. Currently I have one bullet firing on the screen at a time in the players direction no matter where he is. I want to get it to fire in three-round bursts and am having trouble doing it. Here are some samples of how the code is working.
First, the program calls the AI and determines what speed the bullet should move and direction depending on where the player is located. Here is an example.
// looking West
if(g_chary > g_riflemany - 9.0f && g_chary < g_riflemany + 10.0f)
{
// current frame and direction
g_riflemanFrame = 2;
g_riflemanLooking = 2;
// initial firing point and speed
g_riflemanFirexi = g_riflemanx + 10.0f;
g_riflemanFireyi = g_riflemany + 8.0f;
// determine speed and where the bullet is to go
g_xSpeed = (g_riflemanx - g_charx);
g_ySpeed = (g_riflemany - g_chary);
g_netSlope = (absValue(g_ySpeed) + absValue(g_xSpeed));
// set the initial bullets firing speed
g_riflemanFireSpeedxi = -((g_xSpeed / g_netSlope) * g_netBulletSpeed);
g_riflemanFireSpeedyi = -((g_ySpeed / g_netSlope) * g_netBulletSpeed);
}
The second thing that happens is once the program is in the AI, if the rifleman is on the same screen as the player the program determines which bullet it is on in this case it would be the first then initializes the variables for the corresponding bullet.
// trigger enemy fire by determining if the rifleman is on the same screen as the player
if(!playerBulletCollision(g_riflemanx, g_riflemany))
{
// first bullet fired
if(!g_riflemanFire1)
{
g_riflemanFire1 = true;
// initialize variables for current bullet
g_riflemanFire1x = g_riflemanFirexi;
g_riflemanFire1y = g_riflemanFireyi;
g_riflemanFireSpeedx1 = g_riflemanFireSpeedxi;
g_riflemanFireSpeedy1 = g_riflemanFireSpeedyi;
}
// second bullet fired
if(!g_riflemanFire2)
{
g_riflemanFire2 = true;
// initialize variables for current bullet
g_riflemanFire2x = g_riflemanFirexi;
g_riflemanFire2y = g_riflemanFireyi;
g_riflemanFireSpeedx2 = g_riflemanFireSpeedxi;
g_riflemanFireSpeedy2 = g_riflemanFireSpeedyi;
}
// third bullet fired
if(!g_riflemanFire3)
{
g_riflemanFire3 = true;
// initialize variables for current bullet
g_riflemanFire3x = g_riflemanFirexi;
g_riflemanFire3y = g_riflemanFireyi;
g_riflemanFireSpeedx3 = g_riflemanFireSpeedxi;
g_riflemanFireSpeedy3 = g_riflemanFireSpeedyi;
}
}
The program loop moves based on the correct variables, and finally the render function for the bullet is called.
Now, the problem I am having is I am not sure how to space out the 3 round bursts of bullets. I believe what is currently happening is that it instantaneously fires all 3 bullets at almost the exact same time the way it is coded. For the players firing code it only fires when you push the S key which kind of forces the bullets to be spaced. In this situation I need something like a short timer in between each bullet time to get there to be a short distance between. This I believe would go in the code example I gave right before this. I however am having trouble getting this to work. I'm not even completely sure that the bullets are all firing at the same time, it could be perhaps that it never reaches the second bullet, but I can't figure out why. The thing I want to know right now though is how would I put a short distance in each bullet so you can actually see all 3 bullets moving on the screen at once. Like I said earlier I am having trouble with a timer. I hope all this makes sense, I'm trying to explain it as well as I can and the program itself is way too large to post the whole thing, plus you need the DXSDK to run it anyway. So, I'm just looking for ideas. Thanks!
First, the program calls the AI and determines what speed the bullet should move and direction depending on where the player is located. Here is an example.
// looking West
if(g_chary > g_riflemany - 9.0f && g_chary < g_riflemany + 10.0f)
{
// current frame and direction
g_riflemanFrame = 2;
g_riflemanLooking = 2;
// initial firing point and speed
g_riflemanFirexi = g_riflemanx + 10.0f;
g_riflemanFireyi = g_riflemany + 8.0f;
// determine speed and where the bullet is to go
g_xSpeed = (g_riflemanx - g_charx);
g_ySpeed = (g_riflemany - g_chary);
g_netSlope = (absValue(g_ySpeed) + absValue(g_xSpeed));
// set the initial bullets firing speed
g_riflemanFireSpeedxi = -((g_xSpeed / g_netSlope) * g_netBulletSpeed);
g_riflemanFireSpeedyi = -((g_ySpeed / g_netSlope) * g_netBulletSpeed);
}
The second thing that happens is once the program is in the AI, if the rifleman is on the same screen as the player the program determines which bullet it is on in this case it would be the first then initializes the variables for the corresponding bullet.
// trigger enemy fire by determining if the rifleman is on the same screen as the player
if(!playerBulletCollision(g_riflemanx, g_riflemany))
{
// first bullet fired
if(!g_riflemanFire1)
{
g_riflemanFire1 = true;
// initialize variables for current bullet
g_riflemanFire1x = g_riflemanFirexi;
g_riflemanFire1y = g_riflemanFireyi;
g_riflemanFireSpeedx1 = g_riflemanFireSpeedxi;
g_riflemanFireSpeedy1 = g_riflemanFireSpeedyi;
}
// second bullet fired
if(!g_riflemanFire2)
{
g_riflemanFire2 = true;
// initialize variables for current bullet
g_riflemanFire2x = g_riflemanFirexi;
g_riflemanFire2y = g_riflemanFireyi;
g_riflemanFireSpeedx2 = g_riflemanFireSpeedxi;
g_riflemanFireSpeedy2 = g_riflemanFireSpeedyi;
}
// third bullet fired
if(!g_riflemanFire3)
{
g_riflemanFire3 = true;
// initialize variables for current bullet
g_riflemanFire3x = g_riflemanFirexi;
g_riflemanFire3y = g_riflemanFireyi;
g_riflemanFireSpeedx3 = g_riflemanFireSpeedxi;
g_riflemanFireSpeedy3 = g_riflemanFireSpeedyi;
}
}
The program loop moves based on the correct variables, and finally the render function for the bullet is called.
Now, the problem I am having is I am not sure how to space out the 3 round bursts of bullets. I believe what is currently happening is that it instantaneously fires all 3 bullets at almost the exact same time the way it is coded. For the players firing code it only fires when you push the S key which kind of forces the bullets to be spaced. In this situation I need something like a short timer in between each bullet time to get there to be a short distance between. This I believe would go in the code example I gave right before this. I however am having trouble getting this to work. I'm not even completely sure that the bullets are all firing at the same time, it could be perhaps that it never reaches the second bullet, but I can't figure out why. The thing I want to know right now though is how would I put a short distance in each bullet so you can actually see all 3 bullets moving on the screen at once. Like I said earlier I am having trouble with a timer. I hope all this makes sense, I'm trying to explain it as well as I can and the program itself is way too large to post the whole thing, plus you need the DXSDK to run it anyway. So, I'm just looking for ideas. Thanks!
