Выбрать главу

Anyways… after making sure the object is far off the left side of the screen, we check to see if it was moving left (dir=0). If it's not moving left, we don't care if it's off the left side of the screen!

Finally, we check to see if the object was hit. If the object is off the left of the screen, it's travelling left and it wasn't hit, it's too late for the player to hit it. So we increase the value of miss. This lowers morale and increases the number of missed targets. We set the objects hit value to TRUE so the computer thinks it's been hit. This forces the object to self destruct (allowing us to give the object a new texture, directions, spin, etc).

  // If We Are To Far Left, Direction Is Left And The Object Was Not Hit

  if ((object[loop].x<(object[loop].distance-15.0f)/2.0f) && (object[loop].dir==0) && !object[loop].hit) {

   miss+=1; // Increase miss (Missed Object)

   object[loop].hit=TRUE; // Set hit To True To Manually Blow Up The Object

  }

The following code does the exact same thing as the code above, but instead of checking to see if we've gone off the left side of the screen, we check to see if it's gone off the right side of the screen. We also check to make sure the object is moving right and not some other direction. If the object is off the screen, we increase the value of miss and self destruct the object by telling our program it's been hit.

  // If We Are To Far Right, Direction Is Left And The Object Was Not Hit

  if ((object[loop].x>-(object[loop].distance-15.0f)/2.0f) && (object[loop].dir==1) && !object[loop].hit) {

   miss+=1; // Increase miss (Missed Object)

   object[loop].hit=TRUE; // Set hit To True To Manually Blow Up The Object

  }

The falling code is pretty straight forward. We check to see if the object has just about hit the ground. We don't want it to fall through the ground which is at –3.0f. Instead, we check to see if the object is below –2.0f. We then check to make sure the object is indeed falling (dir=3) and that the object has not yet been hit. If the object is below –2.0f on the y axis, we increase miss and set the objects hit variable to TRUE (causing it to self destruct as it hits the ground… nice effect).

  // If We Are To Far Down, Direction Is Down And The Object Was Not Hit

  if ((object[loop].y<-2.0f) && (object[loop].dir==3) && !object[loop].hit) {

   miss+=1; // Increase miss (Missed Object)

   object[loop].hit=TRUE; // Set hit To True To Manually Blow Up The Object

  }

Unlike the previous code, the going up code is a little different. We don't want the object to go through the clouds! We check to see if the objects y variable is greater than 4.5f (close to the clouds). We also make sure the object is travelling up (dir=2). If the objects y value is greater than 4.5f, instead of destroying the object, we change it's direction. That way the object will quickly pop out of the ground (remember, it goes up faster than it comes down) and once it gets to high we change its direction so it starts to fall toward the ground.

There is no need to destroy the object, or increase the miss variable. If you miss the object as it's flying into the sky, there's always a chance to hit it as it falls. The falling code will handle the final destruction of the object.

  if ((object[loop].y>4.5f) && (object[loop].dir==2)) // If We Are To Far Up And The Direction Is Up

   object[loop].dir=3; // Change The Direction To Down

 }

}

Next we have the object drawing code. I wanted a quick and easy way to draw the game objects, along with the crosshair with as little code as possible. Object takes 3 parameters. First we have the width. The width controls how wide the object will be when it's drawn. Then we have the height. The height controls how tall the object will be when it's drawn. Finally, we have the texid. The texid selects the texture we want to use. If we wanted to draw a bucket, which is texture 1, we would pass a value of 1 for the texid. Pretty simple!

A quick breakdown. We select the texture, and then draw a quad. We use standard texture coordinates so the entire textue is mapped to the face of the quad. The quad is drawn in a counter-clockwise direction (required for culling to work).

void Object(float width,float height,GLuint texid) // Draw Object Using Requested Width, Height And Texture

{

 glBindTexture(GL_TEXTURE_2D, textures[texid].texID); // Select The Correct Texture

 glBegin(GL_QUADS); // Start Drawing A Quad

  glTexCoord2f(0.0f,0.0f); glVertex3f(-width,-height,0.0f); // Bottom Left

  glTexCoord2f(1.0f,0.0f); glVertex3f( width,-height,0.0f); // Bottom Right

  glTexCoord2f(1.0f,1.0f); glVertex3f( width, height,0.0f); // Top Right

  glTexCoord2f(0.0f,1.0f); glVertex3f(-width, height,0.0f); // Top Left

 glEnd(); // Done Drawing Quad

}

The explosion code takes one parameter. num is the object identifier. In order to create the explosion we need to grab a portion of the explosion texture similar to the way we grab each letter from the font texture. The two lines below calculate the column (ex) and row (ey) from a single number (frame).

The first line below grabs the current frame and divides it by 4. The division by 4 is to slow down the animation. %4 keeps the value in the 0-3 range. If the value is higher than 3 it would wrap around and become 0. If the value is 5 it would become 1. A value of 9 would be 0,1,2,3,0,1,2,3,0. We divide the final result by 4.0f because texture coordinates are in the 0.0f to 1.0f range. Our explosion texture has 4 explosion images from left to right and 4 up and down.

Hopefully you're not completely confused. So if our number before division can only be 0,1,2 or 3 our number after we divide it by 4.0f can only be 0.0f, 0.25f (1/4), 0.50f (2/4) or 0.75f (3/4). This gives us our left to right texture coordinate (ex).

Next we calculate the row (ey). We grab the current object frame and divide it by 4 to slow the animation down a little. We then divide by 4 again to eliminate an entire row. Finally we divide by 4 one last time to get our vertical texture coordinate.

A quick example. If our current frame was 16. ey=((16/4)/4)/4 or 4/4/4 or 0.25f. One row down. If our current frame was 60. ey=((60/4)/4)/4 or 15/4/4 or 3/4 or 0.75f. The reason 15/4 isn't 3.75 is because we are working with integers up until we do the final division. With that in mind, the value of ey can only be one of 4 values… 0.0f, 0.25f, 0.50f or 0.75f. Assuming we stay inside our texture (prevent frame from going over a value of 63).

Hope that made sense… it's simple, but intimidating math.

void Explosion(int num) // Draws An Animated Explosion For Object "num"

{

 float ex = (float)((object[num].frame/4)%4)/4.0f; // Calculate Explosion X Frame (0.0f – 0.75f)

 float ey = (float)((object[num].frame/4)/4)/4.0f; // Calculate Explosion Y Frame (0.0f – 0.75f)

Now that we have the texture coordinates, all that's left to do is draw our textured quad. The vertex coordinates are fixed at –1.0f and 1.0f. You will notice we subract ey from 1.0f. If we didn't, the animation would be drawn in the reverse order… The explosion would get bigger, rather than fade out. The effect wont look right!