home *** CD-ROM | disk | FTP | other *** search
/ Game Programming - All in One (3rd Edition) / game_prog_all_in_one_3rd_ed.iso / sources / TankWar-Final / tank.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-09-17  |  2.9 KB  |  97 lines

  1. /////////////////////////////////////////////////////////////////////////
  2. // Game Programming All In One, Third Edition
  3. // Tank War Final - tank.c
  4. /////////////////////////////////////////////////////////////////////////
  5.  
  6. #include "tankwar.h"
  7.  
  8. //new function added in chapter 11
  9. void animatetanks()
  10. {
  11.     for (int num=0; num<2; num++)
  12.     {
  13.         //set the animation speed from average velocity
  14.         if (tanks[num]->speed > 0.0) {
  15.             tanks[num]->animdir = 1;
  16.             tanks[num]->framedelay = MAXSPEED - tanks[num]->speed;
  17.         }
  18.         else if (tanks[num]->speed < 0.0) {
  19.             tanks[num]->animdir = -1;
  20.             tanks[num]->framedelay = MAXSPEED - abs(tanks[num]->speed);
  21.         }
  22.         else
  23.             tanks[num]->animdir = 0;
  24.  
  25.         tanks[num]->updateAnimation();
  26.     }
  27. }
  28.  
  29. void drawtanks()
  30. {
  31.     int angle, x, y;
  32.  
  33.     for (int num=0; num<2; num++)
  34.     {
  35.         //rotate sprite, adjust for Allegro's 16.16 fixed trig
  36.         //(256 / 360 = 0.7), then divide by 2 radians
  37.         angle = tanks[num]->moveAngle / 0.7f / 2.0f;
  38.         tanks[num]->rotateframe(buffer, angle);
  39.  
  40.         //print info next to tank
  41. /*        x = tanks[num]->x;
  42.         y = tanks[num]->y;
  43.         textprintf_ex(buffer,font,x-40,y+40,makecol(255,255,255),-1,
  44.             "Pos %.1f,%.1f", scrollx[num], scrolly[num]);
  45.         textprintf_ex(buffer,font,x-40,y+50,makecol(255,255,255),-1,
  46.             "Vel %.1f,%.1f", tanks[num]->velx, tanks[num]->vely);
  47.         textprintf_ex(buffer,font,x-40, y+60,makecol(255,255,255),-1,
  48.             "moveAngle %d", tanks[num]->moveAngle);
  49. */
  50.  
  51.         //is the enemy tank visible in the window?
  52.         x = scrollx[!num] + SCROLLW/2 - 16;
  53.         y = scrolly[!num] + SCROLLH/2 - 16;
  54.         if (inside(x, y, 
  55.             scrollx[num], scrolly[num], 
  56.             scrollx[num] + SCROLLW, scrolly[num] + SCROLLH))
  57.         {
  58.             //draw enemy tank, adjust for scroll
  59.             x = startx[num]+x-scrollx[num];
  60.             y = starty[num]+y-scrolly[num];
  61.             angle = tanks[!num]->moveAngle / 0.7f / 2.0f;
  62.  
  63.             tanks[!num]->rotateframe(buffer, angle, x, y);
  64.         }
  65.     }
  66. }
  67.  
  68.  
  69. void movetanks()
  70. {
  71.     for (int num=0; num<2; num++)
  72.     {
  73.         scrollx[num] += tanks[num]->velx;
  74.         scrolly[num] += tanks[num]->vely;
  75.  
  76.         //keep tank inside bounds
  77.         if (scrollx[num] < 0) {
  78.             scrollx[num] = 0;
  79.             tanks[num]->velx = 0.0f;
  80.         }
  81.  
  82.         else if (scrollx[num] > mapwidth*mapblockwidth - SCROLLW) {
  83.             scrollx[num] = mapwidth*mapblockwidth - SCROLLW;
  84.             tanks[num]->velx = 0.0f;
  85.         }
  86.  
  87.         if (scrolly[num] < 0) {
  88.             scrolly[num] = 0;
  89.             tanks[num]->velx = 0.0;
  90.         }
  91.         else if (scrolly[num] > mapheight*mapblockheight - SCROLLH) {
  92.             scrolly[num] = mapheight*mapblockheight - SCROLLH;
  93.             tanks[num]->velx = 0.0f;
  94.         }
  95.     }
  96. }
  97.