home *** CD-ROM | disk | FTP | other *** search
- /////////////////////////////////////////////////////////////////////////
- // Game Programming All In One, Third Edition
- // Tank War Final - tank.c
- /////////////////////////////////////////////////////////////////////////
-
- #include "tankwar.h"
-
- //new function added in chapter 11
- void animatetanks()
- {
- for (int num=0; num<2; num++)
- {
- //set the animation speed from average velocity
- if (tanks[num]->speed > 0.0) {
- tanks[num]->animdir = 1;
- tanks[num]->framedelay = MAXSPEED - tanks[num]->speed;
- }
- else if (tanks[num]->speed < 0.0) {
- tanks[num]->animdir = -1;
- tanks[num]->framedelay = MAXSPEED - abs(tanks[num]->speed);
- }
- else
- tanks[num]->animdir = 0;
-
- tanks[num]->updateAnimation();
- }
- }
-
- void drawtanks()
- {
- int angle, x, y;
-
- for (int num=0; num<2; num++)
- {
- //rotate sprite, adjust for Allegro's 16.16 fixed trig
- //(256 / 360 = 0.7), then divide by 2 radians
- angle = tanks[num]->moveAngle / 0.7f / 2.0f;
- tanks[num]->rotateframe(buffer, angle);
-
- //print info next to tank
- /* x = tanks[num]->x;
- y = tanks[num]->y;
- textprintf_ex(buffer,font,x-40,y+40,makecol(255,255,255),-1,
- "Pos %.1f,%.1f", scrollx[num], scrolly[num]);
- textprintf_ex(buffer,font,x-40,y+50,makecol(255,255,255),-1,
- "Vel %.1f,%.1f", tanks[num]->velx, tanks[num]->vely);
- textprintf_ex(buffer,font,x-40, y+60,makecol(255,255,255),-1,
- "moveAngle %d", tanks[num]->moveAngle);
- */
-
- //is the enemy tank visible in the window?
- x = scrollx[!num] + SCROLLW/2 - 16;
- y = scrolly[!num] + SCROLLH/2 - 16;
- if (inside(x, y,
- scrollx[num], scrolly[num],
- scrollx[num] + SCROLLW, scrolly[num] + SCROLLH))
- {
- //draw enemy tank, adjust for scroll
- x = startx[num]+x-scrollx[num];
- y = starty[num]+y-scrolly[num];
- angle = tanks[!num]->moveAngle / 0.7f / 2.0f;
-
- tanks[!num]->rotateframe(buffer, angle, x, y);
- }
- }
- }
-
-
- void movetanks()
- {
- for (int num=0; num<2; num++)
- {
- scrollx[num] += tanks[num]->velx;
- scrolly[num] += tanks[num]->vely;
-
- //keep tank inside bounds
- if (scrollx[num] < 0) {
- scrollx[num] = 0;
- tanks[num]->velx = 0.0f;
- }
-
- else if (scrollx[num] > mapwidth*mapblockwidth - SCROLLW) {
- scrollx[num] = mapwidth*mapblockwidth - SCROLLW;
- tanks[num]->velx = 0.0f;
- }
-
- if (scrolly[num] < 0) {
- scrolly[num] = 0;
- tanks[num]->velx = 0.0;
- }
- else if (scrolly[num] > mapheight*mapblockheight - SCROLLH) {
- scrolly[num] = mapheight*mapblockheight - SCROLLH;
- tanks[num]->velx = 0.0f;
- }
- }
- }
-