home *** CD-ROM | disk | FTP | other *** search
/ Game Programming - All in One (3rd Edition) / game_prog_all_in_one_3rd_ed.iso / sources / chapter12 / tankwar_r5 / bullet.c < prev    next >
Encoding:
C/C++ Source or Header  |  2004-04-05  |  5.0 KB  |  183 lines

  1. /////////////////////////////////////////////////////////////////////////
  2. // Game Programming All In One, Second Edition
  3. // Source Code Copyright (C)2004 by Jonathan S. Harbour
  4. // Tank War Enhancement 5 - bullet.c
  5. /////////////////////////////////////////////////////////////////////////
  6.  
  7. #include "tankwar.h"
  8.  
  9.  
  10. void explode(int num, int x, int y)
  11. {
  12.     int n;
  13.  
  14.     //load explode image
  15.     if (explode_bmp == NULL)
  16.     {
  17.         explode_bmp = load_bitmap("explode.bmp", NULL);
  18.     }
  19.     
  20.     //draw the explosion bitmap several times
  21.     for (n = 0; n < 5; n++)
  22.     {
  23.         rotate_sprite(screen, explode_bmp, 
  24.             x + rand()%10 - 20, y + rand()%10 - 20, 
  25.             itofix(rand()%255));
  26.  
  27.         rest(30);
  28.     }
  29. }
  30.  
  31. void drawbullet(int num)
  32. {
  33.     int n;
  34.     int x, y;
  35.  
  36.     x = bullets[num]->x;
  37.     y = bullets[num]->y;
  38.  
  39.     //is the bullet active?
  40.     if (!bullets[num]->alive) return;
  41.  
  42.     //draw bullet sprite
  43.     for (n=0; n<2; n++)
  44.     {
  45.         if (inside(x, y, scrollx[n], scrolly[n], 
  46.             scrollx[n] + SCROLLW - bullet_bmp->w, 
  47.             scrolly[n] + SCROLLH - bullet_bmp->h))
  48.             
  49.             //draw bullet, adjust for scroll
  50.             draw_sprite(buffer, bullet_bmp, startx[n] + x-scrollx[n], 
  51.                 starty[n] + y-scrolly[n]);
  52.     }
  53.  
  54.     //draw bullet on radar
  55.     putpixel(buffer, radarx + x/10, radary + y/12, WHITE);
  56.  
  57. }
  58.  
  59. void movebullet(int num)
  60. {
  61.     int x, y, tx, ty;
  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.     //move bullet
  70.     bullets[num]->x += bullets[num]->xspeed;
  71.     bullets[num]->y += bullets[num]->yspeed;
  72.     x = bullets[num]->x;
  73.     y = bullets[num]->y;
  74.  
  75.     //stay within the virtual screen
  76.     if (x < 0 || x > MAPW-6 || y < 0 || y > MAPH-6)
  77.     {
  78.         bullets[num]->alive = 0;
  79.         return;
  80.     }
  81.  
  82.     //look for a direct hit using basic collision
  83.     tx = scrollx[!num] + SCROLLW/2;
  84.     ty = scrolly[!num] + SCROLLH/2;
  85.  
  86.     //if (collided(bullets[num], tanks[!num]))
  87.     if (inside(x,y,tx-15,ty-15,tx+15,ty+15))
  88.     {
  89.         //kill the bullet
  90.         bullets[num]->alive = 0;
  91.             
  92.         //blow up the tank
  93.             x = scrollx[!num] + SCROLLW/2;
  94.             y = scrolly[!num] + SCROLLH/2;
  95.  
  96.             if (inside(x, y, 
  97.                 scrollx[num], scrolly[num], 
  98.                 scrollx[num] + SCROLLW, scrolly[num] + SCROLLH))
  99.             {
  100.                 //draw explosion in my window
  101.                 explode(num, startx[num]+x-scrollx[num], 
  102.                     starty[num]+y-scrolly[num]);
  103.             }
  104.  
  105.         //draw explosion in enemy window
  106.         explode(num, tanks[!num]->x, tanks[!num]->y);
  107.         scores[num]++;
  108.     }
  109. }
  110.  
  111. void fireweapon(int num)
  112. {
  113.     int x = scrollx[num] + SCROLLW/2;
  114.     int y = scrolly[num] + SCROLLH/2;
  115.  
  116.     //ready to fire again?
  117.     if (!bullets[num]->alive)
  118.     {
  119.         bullets[num]->alive = 1;
  120.  
  121.         //fire bullet in direction tank is facing
  122.         switch (tanks[num]->dir)
  123.         {
  124.             //north
  125.             case 0:
  126.                 bullets[num]->x = x-2;
  127.                 bullets[num]->y = y-22;
  128.                 bullets[num]->xspeed = 0;
  129.                 bullets[num]->yspeed = -BULLETSPEED;
  130.                 break;
  131.             //NE
  132.             case 1:
  133.                 bullets[num]->x = x+18;
  134.                 bullets[num]->y = y-18;
  135.                 bullets[num]->xspeed = BULLETSPEED;
  136.                 bullets[num]->yspeed = -BULLETSPEED;         
  137.                 break;  
  138.             //east
  139.             case 2:
  140.                 bullets[num]->x = x+22;
  141.                 bullets[num]->y = y-2;
  142.                 bullets[num]->xspeed = BULLETSPEED;
  143.                 bullets[num]->yspeed = 0;
  144.                 break;
  145.             //SE
  146.             case 3:
  147.                 bullets[num]->x = x+18;
  148.                 bullets[num]->y = y+18;
  149.                 bullets[num]->xspeed = BULLETSPEED;
  150.                 bullets[num]->yspeed = BULLETSPEED;
  151.                 break;
  152.             //south
  153.             case 4:
  154.                 bullets[num]->x = x-2;
  155.                 bullets[num]->y = y+22;
  156.                 bullets[num]->xspeed = 0;
  157.                 bullets[num]->yspeed = BULLETSPEED;
  158.                 break;
  159.             //SW
  160.             case 5:
  161.                 bullets[num]->x = x-18;
  162.                 bullets[num]->y = y+18;
  163.                 bullets[num]->xspeed = -BULLETSPEED;
  164.                 bullets[num]->yspeed = BULLETSPEED;
  165.                 break;
  166.             //west
  167.             case 6:
  168.                 bullets[num]->x = x-22;
  169.                 bullets[num]->y = y-2;
  170.                 bullets[num]->xspeed = -BULLETSPEED;
  171.                 bullets[num]->yspeed = 0;
  172.                 break;
  173.             //NW
  174.             case 7:
  175.                 bullets[num]->x = x-18;
  176.                 bullets[num]->y = y-18;
  177.                 bullets[num]->xspeed = -BULLETSPEED;
  178.                 bullets[num]->yspeed = -BULLETSPEED;
  179.                 break;
  180.         }
  181.     }
  182. }
  183.