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 / tank.c < prev    next >
Encoding:
C/C++ Source or Header  |  2006-09-15  |  3.1 KB  |  121 lines

  1. /////////////////////////////////////////////////////////////////////////
  2. // Game Programming All In One, Third Edition
  3. // Tank War Enhancement 7 - tank.c
  4. /////////////////////////////////////////////////////////////////////////
  5.  
  6. #include "tankwar.h"
  7.  
  8. //new function added in chapter 11
  9. void animatetank(int num)
  10. {
  11.     if (++tanks[num]->framecount > tanks[num]->framedelay)
  12.     {
  13.         tanks[num]->framecount = 0;
  14.         tanks[num]->curframe += tanks[num]->animdir;
  15.         if (tanks[num]->curframe > tanks[num]->maxframe)
  16.             tanks[num]->curframe = 0;
  17.         else if (tanks[num]->curframe < 0)
  18.             tanks[num]->curframe = tanks[num]->maxframe;
  19.     }
  20. }
  21.  
  22. void drawtank(int num)
  23. {
  24.     int dir = tanks[num]->dir;
  25.     int x = tanks[num]->x-15;
  26.     int y = tanks[num]->y-15;
  27.     draw_sprite(buffer, tank_bmp[num][tanks[num]->curframe][dir], x, y);
  28.  
  29.     //what about the enemy tank?
  30.     x = scrollx[!num] + SCROLLW/2;
  31.     y = scrolly[!num] + SCROLLH/2;
  32.     if (inside(x, y, 
  33.         scrollx[num], scrolly[num], 
  34.         scrollx[num] + SCROLLW, scrolly[num] + SCROLLH))
  35.     {
  36.         //draw enemy tank, adjust for scroll
  37.         draw_sprite(buffer, tank_bmp[!num][tanks[!num]->curframe][tanks[!num]->dir], 
  38.             startx[num]+x-scrollx[num]-15, starty[num]+y-scrolly[num]-15);
  39.  
  40.     }
  41. }
  42.  
  43. void movetank(int num)
  44. {
  45.     int dir = tanks[num]->dir;
  46.     int speed = tanks[num]->xspeed;
  47.  
  48.     //animate tank when moving
  49.     if (speed > 0) 
  50.     {
  51.         tanks[num]->animdir = 1;
  52.         tanks[num]->framedelay = MAXSPEED - speed;
  53.     }
  54.     else if (speed < 0)
  55.     {
  56.         tanks[num]->animdir = -1;
  57.         tanks[num]->framedelay = MAXSPEED - abs(speed);
  58.     }
  59.     else
  60.         tanks[num]->animdir = 0;
  61.  
  62.  
  63.     //update tank position
  64.     switch(dir)
  65.     {
  66.         case 0:
  67.             scrolly[num] -= speed;
  68.             break;
  69.         case 1:
  70.             scrolly[num] -= speed;
  71.             scrollx[num] += speed;
  72.             break;
  73.         case 2:
  74.             scrollx[num] += speed;
  75.             break;
  76.         case 3:
  77.             scrollx[num] += speed;
  78.             scrolly[num] += speed;
  79.             break;
  80.         case 4:
  81.             scrolly[num] += speed;
  82.             break;
  83.         case 5:
  84.             scrolly[num] += speed;
  85.             scrollx[num] -= speed;
  86.             break;
  87.         case 6:
  88.             scrollx[num] -= speed;
  89.             break;
  90.         case 7:
  91.             scrollx[num] -= speed;
  92.             scrolly[num] -= speed;
  93.             break;
  94.     }
  95.  
  96.     //keep tank inside bounds
  97.     if (scrollx[num] < 0)
  98.     {
  99.         scrollx[num] = 0;
  100.         tanks[num]->xspeed = 0;
  101.     }
  102.  
  103.     else if (scrollx[num] > mapwidth*mapblockwidth - SCROLLW)
  104.     {
  105.         scrollx[num] = mapwidth*mapblockwidth - SCROLLW;
  106.         tanks[num]->xspeed = 0;
  107.     }
  108.  
  109.     if (scrolly[num] < 0)
  110.     {
  111.         scrolly[num] = 0;
  112.         tanks[num]->xspeed = 0;
  113.     }
  114.     else if (scrolly[num] > mapheight*mapblockheight - SCROLLH)
  115.     {
  116.         scrolly[num] = mapheight*mapblockheight - SCROLLH;
  117.         tanks[num]->xspeed = 0;
  118.     }
  119.  
  120. }
  121.