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

  1. /////////////////////////////////////////////////////////////////////////
  2. // Game Programming All In One, Third Edition
  3. // Tank War Enhancement 7 - bullet.c
  4. /////////////////////////////////////////////////////////////////////////
  5.  
  6. #include "tankwar.h"
  7.  
  8. //new function added in chapter 11
  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.     else
  42.     {
  43.         explosions[num]->alive = 0;
  44.         explosions[num]->curframe = 0;
  45.     }
  46. }
  47.  
  48. void explode(int num, int x, int y)
  49. {
  50.     //initialize the explosion sprite
  51.     explosions[num]->alive = 1;
  52.     explosions[num]->x = x;
  53.     explosions[num]->y = y;
  54.     explosions[num]->curframe = 0;
  55.     explosions[num]->maxframe = 20;
  56. }
  57.  
  58. void drawbullet(int num)
  59. {
  60.     int n;
  61.     int x, y;
  62.  
  63.     x = bullets[num]->x;
  64.     y = bullets[num]->y;
  65.  
  66.     //is the bullet active?
  67.     if (!bullets[num]->alive) return;
  68.  
  69.     //draw bullet sprite
  70.     for (n=0; n<2; n++)
  71.     {
  72.         if (inside(x, y, scrollx[n], scrolly[n], 
  73.             scrollx[n] + SCROLLW - bullet_bmp->w, 
  74.             scrolly[n] + SCROLLH - bullet_bmp->h))
  75.             
  76.             //draw bullet, adjust for scroll
  77.             draw_sprite(buffer, bullet_bmp, startx[n] + x-scrollx[n], 
  78.                 starty[n] + y-scrolly[n]);
  79.     }
  80.  
  81.     //draw bullet on radar
  82.     putpixel(buffer, radarx + x/10, radary + y/12, WHITE);
  83.  
  84. }
  85.  
  86. void movebullet(int num)
  87. {
  88.     int x, y, tx, ty;
  89.  
  90.     x = bullets[num]->x;
  91.     y = bullets[num]->y;
  92.  
  93.     //is the bullet active?
  94.     if (!bullets[num]->alive) return;
  95.     
  96.     //move bullet
  97.     bullets[num]->x += bullets[num]->xspeed;
  98.     bullets[num]->y += bullets[num]->yspeed;
  99.     x = bullets[num]->x;
  100.     y = bullets[num]->y;
  101.  
  102.     //stay within the virtual screen
  103.     if (x < 0 || x > MAPW-6 || y < 0 || y > MAPH-6)
  104.     {
  105.         bullets[num]->alive = 0;
  106.         return;
  107.     }
  108.  
  109.     //look for a direct hit using basic collision
  110.     tx = scrollx[!num] + SCROLLW/2;
  111.     ty = scrolly[!num] + SCROLLH/2;
  112.     if (inside(x,y,tx-15,ty-15,tx+15,ty+15))
  113.     {
  114.         //kill the bullet
  115.         bullets[num]->alive = 0;
  116.             
  117.         //blow up the tank
  118.         x = scrollx[!num] + SCROLLW/2;
  119.         y = scrolly[!num] + SCROLLH/2;
  120.  
  121.         //draw explosion in enemy window
  122.         explode(num, tanks[!num]->x, tanks[!num]->y);
  123.         scores[num]++;
  124.     }
  125. }
  126.  
  127. void fireweapon(int num)
  128. {
  129.     int x = scrollx[num] + SCROLLW/2;
  130.     int y = scrolly[num] + SCROLLH/2;
  131.  
  132.     //ready to fire again?
  133.     if (!bullets[num]->alive)
  134.     {
  135.         bullets[num]->alive = 1;
  136.  
  137.         //fire bullet in direction tank is facing
  138.         switch (tanks[num]->dir)
  139.         {
  140.             //north
  141.             case 0:
  142.                 bullets[num]->x = x-2;
  143.                 bullets[num]->y = y-22;
  144.                 bullets[num]->xspeed = 0;
  145.                 bullets[num]->yspeed = -BULLETSPEED;
  146.                 break;
  147.             //NE
  148.             case 1:
  149.                 bullets[num]->x = x+18;
  150.                 bullets[num]->y = y-18;
  151.                 bullets[num]->xspeed = BULLETSPEED;
  152.                 bullets[num]->yspeed = -BULLETSPEED;         
  153.                 break;  
  154.             //east
  155.             case 2:
  156.                 bullets[num]->x = x+22;
  157.                 bullets[num]->y = y-2;
  158.                 bullets[num]->xspeed = BULLETSPEED;
  159.                 bullets[num]->yspeed = 0;
  160.                 break;
  161.             //SE
  162.             case 3:
  163.                 bullets[num]->x = x+18;
  164.                 bullets[num]->y = y+18;
  165.                 bullets[num]->xspeed = BULLETSPEED;
  166.                 bullets[num]->yspeed = BULLETSPEED;
  167.                 break;
  168.             //south
  169.             case 4:
  170.                 bullets[num]->x = x-2;
  171.                 bullets[num]->y = y+22;
  172.                 bullets[num]->xspeed = 0;
  173.                 bullets[num]->yspeed = BULLETSPEED;
  174.                 break;
  175.             //SW
  176.             case 5:
  177.                 bullets[num]->x = x-18;
  178.                 bullets[num]->y = y+18;
  179.                 bullets[num]->xspeed = -BULLETSPEED;
  180.                 bullets[num]->yspeed = BULLETSPEED;
  181.                 break;
  182.             //west
  183.             case 6:
  184.                 bullets[num]->x = x-22;
  185.                 bullets[num]->y = y-2;
  186.                 bullets[num]->xspeed = -BULLETSPEED;
  187.                 bullets[num]->yspeed = 0;
  188.                 break;
  189.             //NW
  190.             case 7:
  191.                 bullets[num]->x = x-18;
  192.                 bullets[num]->y = y-18;
  193.                 bullets[num]->xspeed = -BULLETSPEED;
  194.                 bullets[num]->yspeed = -BULLETSPEED;
  195.                 break;
  196.         }
  197.     }
  198. }
  199.