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

  1. /////////////////////////////////////////////////////////////////////////
  2. // Game Programming All In One, Third Edition
  3. // Tank War Enhancement 6 - 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.     //*****
  49.     //animate tank when moving
  50.     if (speed > 0) 
  51.     {
  52.         tanks[num]->animdir = 1;
  53.         tanks[num]->framedelay = MAXSPEED - speed;
  54.     }
  55.     else if (speed < 0)
  56.     {
  57.         tanks[num]->animdir = -1;
  58.         tanks[num]->framedelay = MAXSPEED - abs(speed);
  59.     }
  60.     else
  61.         tanks[num]->animdir = 0;
  62.  
  63.  
  64.     //update tank position
  65.     switch(dir)
  66.     {
  67.         case 0:
  68.             scrolly[num] -= speed;
  69.             break;
  70.         case 1:
  71.             scrolly[num] -= speed;
  72.             scrollx[num] += speed;
  73.             break;
  74.         case 2:
  75.             scrollx[num] += speed;
  76.             break;
  77.         case 3:
  78.             scrollx[num] += speed;
  79.             scrolly[num] += speed;
  80.             break;
  81.         case 4:
  82.             scrolly[num] += speed;
  83.             break;
  84.         case 5:
  85.             scrolly[num] += speed;
  86.             scrollx[num] -= speed;
  87.             break;
  88.         case 6:
  89.             scrollx[num] -= speed;
  90.             break;
  91.         case 7:
  92.             scrollx[num] -= speed;
  93.             scrolly[num] -= speed;
  94.             break;
  95.     }
  96.  
  97.     //keep tank inside bounds
  98. //*****
  99.     if (scrollx[num] < 0)
  100.     {
  101.         scrollx[num] = 0;
  102.         tanks[num]->xspeed = 0;
  103.     }
  104.     else if (scrollx[num] > scroll->w - SCROLLW)
  105.     {
  106.         scrollx[num] = scroll->w - SCROLLW;
  107.         tanks[num]->xspeed = 0;
  108.     }
  109.     if (scrolly[num] < 0)
  110.     {
  111.         scrolly[num] = 0;
  112.         tanks[num]->xspeed = 0;
  113.     }
  114.     else if (scrolly[num] > scroll->h - SCROLLH)
  115.     {
  116.         scrolly[num] = scroll->h - SCROLLH;
  117.         tanks[num]->xspeed = 0;
  118.     }
  119.  
  120. }
  121.