home *** CD-ROM | disk | FTP | other *** search
/ Game Programming - All in One (3rd Edition) / game_prog_all_in_one_3rd_ed.iso / sources / chapter18 / tankwar_r8 / bullet.c < prev    next >
Encoding:
C/C++ Source or Header  |  2006-09-16  |  6.5 KB  |  233 lines

  1. /////////////////////////////////////////////////////////////////////////
  2. // Game Programming All In One, Third Edition
  3. // Tank War Enhancement 8 - bullet.c
  4. /////////////////////////////////////////////////////////////////////////
  5.  
  6. #include "tankwar.h"
  7.  
  8.  
  9. void updateexplosion(int num)
  10. {
  11.     int x, y;
  12.  
  13.     if (!explosions[num]->alive) return;
  14.  
  15.     //draw explosion (maxframe) times
  16.     if (explosions[num]->curframe++ < explosions[num]->maxframe)
  17.     {
  18.         x = explosions[num]->x;
  19.         y = explosions[num]->y;
  20.  
  21.         //draw explosion in enemy window
  22.         rotate_sprite(buffer, explode_bmp, 
  23.             x + rand()%10 - 20, y + rand()%10 - 20, 
  24.             itofix(rand()%255));
  25.  
  26.         //draw explosion in "my" window
  27.         x = scrollx[!num] + SCROLLW/2;
  28.         y = scrolly[!num] + SCROLLH/2;
  29.         if (inside(x, y, 
  30.             scrollx[num], scrolly[num], 
  31.             scrollx[num] + SCROLLW, scrolly[num] + SCROLLH))
  32.         {
  33.             //draw explosion in own window if enemy is visible
  34.             if (explosions[num]->alive)
  35.                 rotate_sprite(buffer, explode_bmp, 
  36.                     startx[num]+x-scrollx[num] + rand()%10 - 20, 
  37.                     starty[num]+y-scrolly[num] + rand()%10 - 20, 
  38.                     itofix(rand()%255));
  39.         }
  40.  
  41.     }
  42.     else
  43.     {
  44.         //play "end of explosion" sound
  45.         play_sample(sounds[HARP], VOLUME, PAN, PITCH, FALSE);
  46.  
  47.         explosions[num]->alive = 0;
  48.         explosions[num]->curframe = 0;
  49.     }
  50. }
  51.  
  52. void explode(int num, int x, int y)
  53. {
  54.     //initialize the explosion sprite
  55.     explosions[num]->alive = 1;
  56.     explosions[num]->x = x;
  57.     explosions[num]->y = y;
  58.     explosions[num]->curframe = 0;
  59.     explosions[num]->maxframe = 20;
  60.  
  61.     //play explosion sounds
  62.     play_sample(sounds[GOOPY], VOLUME, PAN, PITCH, FALSE);
  63.     play_sample(sounds[HIT1], VOLUME, PAN, PITCH, FALSE);
  64.     play_sample(sounds[HIT2], VOLUME, PAN, PITCH, FALSE);
  65. }
  66.  
  67. void drawbullet(int num)
  68. {
  69.     int n;
  70.     int x, y;
  71.  
  72.     x = bullets[num]->x;
  73.     y = bullets[num]->y;
  74.  
  75.     //is the bullet active?
  76.     if (!bullets[num]->alive) return;
  77.  
  78.     //draw bullet sprite
  79.     for (n=0; n<2; n++)
  80.     {
  81.         if (inside(x, y, scrollx[n], scrolly[n], 
  82.             scrollx[n] + SCROLLW - bullet_bmp->w, 
  83.             scrolly[n] + SCROLLH - bullet_bmp->h))
  84.             
  85.             //draw bullet, adjust for scroll
  86.             draw_sprite(buffer, bullet_bmp, startx[n] + x-scrollx[n], 
  87.                 starty[n] + y-scrolly[n]);
  88.     }
  89.  
  90.     //draw bullet on radar
  91.     putpixel(buffer, radarx + x/10, radary + y/12, WHITE);
  92.  
  93. }
  94.  
  95. void movebullet(int num)
  96. {
  97.     int x, y, tx, ty;
  98.  
  99.     x = bullets[num]->x;
  100.     y = bullets[num]->y;
  101.  
  102.     //is the bullet active?
  103.     if (!bullets[num]->alive) return;
  104.     
  105.     //move bullet
  106.     bullets[num]->x += bullets[num]->xspeed;
  107.     bullets[num]->y += bullets[num]->yspeed;
  108.     x = bullets[num]->x;
  109.     y = bullets[num]->y;
  110.  
  111.     //stay within the virtual screen
  112.     if (x < 0 || x > MAPW-6 || y < 0 || y > MAPH-6)
  113.     {
  114.  
  115.         //play the ammo sound
  116.         play_sample(sounds[AMMO], VOLUME, PAN, PITCH, FALSE);
  117.         
  118.         bullets[num]->alive = 0;
  119.         return;
  120.     }
  121.  
  122.     //look for a direct hit using basic collision
  123.     tx = scrollx[!num] + SCROLLW/2;
  124.     ty = scrolly[!num] + SCROLLH/2;
  125.     if (inside(x,y,tx-15,ty-15,tx+15,ty+15))
  126.     {
  127.         //kill the bullet
  128.         bullets[num]->alive = 0;
  129.             
  130.         //blow up the tank
  131.         x = scrollx[!num] + SCROLLW/2;
  132.         y = scrolly[!num] + SCROLLH/2;
  133.  
  134.         //draw explosion in enemy window
  135.         explode(num, tanks[!num]->x, tanks[!num]->y);
  136.         scores[num]++;
  137.  
  138.         //kill any "near miss" sounds
  139.         if (num)
  140.             stop_sample(sounds[SCREAM]);
  141.         else
  142.             stop_sample(sounds[OHHH]);
  143.     }
  144.  
  145.     else if (inside(x,y,tx-30,ty-30,tx+30,ty+30))
  146.     {
  147.         //it's a near miss!
  148.         if (num)
  149.             //player 1 screams
  150.             play_sample(sounds[SCREAM], VOLUME, PAN, PITCH, FALSE);
  151.         else
  152.             //player 2 ohhhs
  153.             play_sample(sounds[OHHH], VOLUME, PAN, PITCH, FALSE);
  154.  
  155.     }
  156. }
  157.  
  158. void fireweapon(int num)
  159. {
  160.     int x = scrollx[num] + SCROLLW/2;
  161.     int y = scrolly[num] + SCROLLH/2;
  162.  
  163.     //ready to fire again?
  164.     if (!bullets[num]->alive)
  165.     {
  166.         //play fire sound
  167.         play_sample(sounds[FIRE], VOLUME, PAN, PITCH, FALSE);
  168.  
  169.         bullets[num]->alive = 1;
  170.  
  171.         //fire bullet in direction tank is facing
  172.         switch (tanks[num]->dir)
  173.         {
  174.             //north
  175.             case 0:
  176.                 bullets[num]->x = x-2;
  177.                 bullets[num]->y = y-22;
  178.                 bullets[num]->xspeed = 0;
  179.                 bullets[num]->yspeed = -BULLETSPEED;
  180.                 break;
  181.             //NE
  182.             case 1:
  183.                 bullets[num]->x = x+18;
  184.                 bullets[num]->y = y-18;
  185.                 bullets[num]->xspeed = BULLETSPEED;
  186.                 bullets[num]->yspeed = -BULLETSPEED;         
  187.                 break;  
  188.             //east
  189.             case 2:
  190.                 bullets[num]->x = x+22;
  191.                 bullets[num]->y = y-2;
  192.                 bullets[num]->xspeed = BULLETSPEED;
  193.                 bullets[num]->yspeed = 0;
  194.                 break;
  195.             //SE
  196.             case 3:
  197.                 bullets[num]->x = x+18;
  198.                 bullets[num]->y = y+18;
  199.                 bullets[num]->xspeed = BULLETSPEED;
  200.                 bullets[num]->yspeed = BULLETSPEED;
  201.                 break;
  202.             //south
  203.             case 4:
  204.                 bullets[num]->x = x-2;
  205.                 bullets[num]->y = y+22;
  206.                 bullets[num]->xspeed = 0;
  207.                 bullets[num]->yspeed = BULLETSPEED;
  208.                 break;
  209.             //SW
  210.             case 5:
  211.                 bullets[num]->x = x-18;
  212.                 bullets[num]->y = y+18;
  213.                 bullets[num]->xspeed = -BULLETSPEED;
  214.                 bullets[num]->yspeed = BULLETSPEED;
  215.                 break;
  216.             //west
  217.             case 6:
  218.                 bullets[num]->x = x-22;
  219.                 bullets[num]->y = y-2;
  220.                 bullets[num]->xspeed = -BULLETSPEED;
  221.                 bullets[num]->yspeed = 0;
  222.                 break;
  223.             //NW
  224.             case 7:
  225.                 bullets[num]->x = x-18;
  226.                 bullets[num]->y = y-18;
  227.                 bullets[num]->xspeed = -BULLETSPEED;
  228.                 bullets[num]->yspeed = -BULLETSPEED;
  229.                 break;
  230.         }
  231.     }
  232. }
  233.