home *** CD-ROM | disk | FTP | other *** search
/ Game Programming - All in One (3rd Edition) / game_prog_all_in_one_3rd_ed.iso / sources / TankWar-Final / bullet.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-09-17  |  5.6 KB  |  188 lines

  1. /////////////////////////////////////////////////////////////////////////
  2. // Game Programming All In One, Third Edition
  3. // Tank War Final - bullet.c
  4. /////////////////////////////////////////////////////////////////////////
  5.  
  6. #include "tankwar.h"
  7.  
  8.  
  9.  
  10. void updateexplosion(int window, int num)
  11. {
  12.     if (explosions[window][num]->alive) 
  13.     {
  14.         //draw explosion in enemy window (target)
  15.         explosions[window][num]->updateAnimation();
  16.         if (explosions[window][num]->curframe == explosions[window][num]->totalframes-1) 
  17.             explosions[window][num]->alive = 0;
  18.         explosions[window][num]->drawframe(buffer);
  19.  
  20.     }
  21.  
  22.     //play "end of explosion" sound
  23.     play_sample(sounds[HARP], VOLUME, PAN, PITCH, FALSE);
  24. }
  25.  
  26. void explode(int num, int x, int y)
  27. {
  28.     if (!explosions[num][0]->alive)
  29.     {
  30.         explosions[num][0]->curframe = 0;
  31.         explosions[num][0]->framecount = 0;
  32.         explosions[num][0]->x = x - explosions[num][0]->width/2 + 16;
  33.         explosions[num][0]->y = y - explosions[num][0]->height/2 + 16;
  34.         explosions[num][0]->alive = 1;
  35.     }
  36.  
  37.     if (!explosions[num][1]->alive)
  38.     {
  39.  
  40.         //draw explosion in my window
  41.         int x = scrollx[!num] + SCROLLW/2;
  42.         int y = scrolly[!num] + SCROLLH/2;
  43.  
  44.         //is enemy tank visible in my window?
  45.         if (inside(x, y, 
  46.             scrollx[num], scrolly[num], 
  47.             scrollx[num] + SCROLLW, scrolly[num] + SCROLLH))
  48.         {
  49.             //draw explosion in own window if enemy is visible
  50.             explosions[num][1]->curframe = 0;
  51.             explosions[num][1]->framecount = 0;
  52.             explosions[num][1]->x = startx[num]+x-scrollx[num] - 48;
  53.             explosions[num][1]->y = starty[num]+y-scrolly[num] - 48;
  54.             explosions[num][1]->alive = 1;
  55.         }
  56.     }
  57. }
  58.  
  59. void drawbullets()
  60. {
  61.     int x,y;
  62.  
  63.     //is the bullet active?
  64.     for (int num=0; num<2; num++) 
  65.     {
  66.         if (bullets[num]->alive)
  67.         {
  68.             x = bullets[num]->x;
  69.             y = bullets[num]->y;
  70.  
  71.             //draw bullet sprite in window if visible
  72.             if (inside(x, y, scrollx[num], scrolly[num], 
  73.                 scrollx[num] + SCROLLW, 
  74.                 scrolly[num] + SCROLLH))
  75.             {
  76.                 //draw bullet, adjust for scroll
  77.                 x = startx[num] - scrollx[num] + x;
  78.                 y = starty[num] - scrolly[num] + y;
  79.  
  80.                 //rotate sprite, adjust for Allegro's 16.16 fixed trig
  81.                 int angle = bullets[num]->moveAngle + 90;
  82.                 rotate_sprite(buffer, bullets[num]->image, x, y, 
  83.                     itofix(angle / 0.7f / 2.0f));
  84.             }
  85.         }
  86.     }
  87. }
  88.  
  89. void movebullets()
  90. {
  91.     int tx, ty;
  92.  
  93.     for (int num=0; num<2; num++) 
  94.     {
  95.  
  96.         //is the bullet active?
  97.         if (!bullets[num]->alive) return;
  98.         
  99.         //move bullet
  100.         //bullets[num]->updatePosition();
  101.         bullets[num]->x += bullets[num]->velx;
  102.         bullets[num]->y += bullets[num]->vely;
  103.         int x = bullets[num]->x;
  104.         int y = bullets[num]->y;
  105.  
  106.         //stay within the virtual screen
  107.         if (x < 0 || x > mapwidth*mapblockwidth-6 || y < 0 || y > mapheight*mapblockheight-6)
  108.         {
  109.             //reload
  110.             bullets[num]->alive = 0;
  111.             play_sample(sounds[AMMO], VOLUME, PAN, PITCH, FALSE);
  112.             return;
  113.         }
  114.  
  115.         //look for a direct hit using basic collision
  116.         tx = scrollx[!num] + SCROLLW/2;
  117.         ty = scrolly[!num] + SCROLLH/2;
  118.  
  119.         //start at center of scroll window, subtract 16 from  
  120.         //each side to encompass the tank
  121.         if (inside(x,y,tx-16,ty-16,tx+16,ty+16))
  122.         {
  123.             //kill the bullet
  124.             bullets[num]->alive = 0;
  125.  
  126.             //draw explosion in enemy window
  127.             tanks[!num]->velx=0;
  128.             tanks[!num]->vely=0;
  129.             explode(num, tanks[!num]->x, tanks[!num]->y);
  130.             health[!num] -= 2;
  131.             if (health[!num] < 1) {
  132.                 gamestate = STATE_VICTORY;
  133.                 rest(200);
  134.                 play_midi(music_victory, 1);
  135.                 clear_keybuf();
  136.             }
  137.  
  138.             //play explosion sounds
  139.             play_sample(sounds[GOOPY], VOLUME, PAN, PITCH, FALSE);
  140.             play_sample(sounds[HIT1], VOLUME, PAN, PITCH, FALSE);
  141.             play_sample(sounds[HIT2], VOLUME, PAN, PITCH, FALSE);
  142.  
  143.             //kill any "near miss" sounds
  144.             if (num)
  145.                 stop_sample(sounds[SCREAM]);
  146.             else
  147.                 stop_sample(sounds[OHHH]);
  148.         }
  149.  
  150.         else if (inside(x,y,tx-30,ty-30,tx+30,ty+30))
  151.         {
  152.             //it's a near miss!
  153.             if (num)
  154.                 //player 1 screams
  155.                 play_sample(sounds[SCREAM], VOLUME, PAN, PITCH, FALSE);
  156.             else
  157.                 //player 2 ohhhs
  158.                 play_sample(sounds[OHHH], VOLUME, PAN, PITCH, FALSE);
  159.  
  160.         }
  161.     }
  162. }
  163.  
  164.  
  165. void fireweapon(int num)
  166. {
  167.     //ready to fire again?
  168.     if (!bullets[num]->alive)
  169.     {
  170.         //fire the bullet
  171.         bullets[num]->alive = 1;
  172.         bullets[num]->x = scrollx[num] + SCROLLW/2;
  173.         bullets[num]->y = scrolly[num] + SCROLLH/2;
  174.  
  175.         //adjust velocity based on angle
  176.         int angle = tanks[num]->moveAngle - 90;
  177.         if (angle < 0) angle = 360 + angle;
  178.         bullets[num]->moveAngle = angle;
  179.         bullets[num]->velx = calcAngleMoveX(angle) * BULLETSPEED;
  180.         bullets[num]->vely = calcAngleMoveY(angle) * BULLETSPEED;
  181.         
  182.         play_sample(sounds[FIRE], VOLUME, PAN, PITCH, FALSE);
  183.     }
  184. }
  185.  
  186.  
  187.  
  188.