home *** CD-ROM | disk | FTP | other *** search
- /////////////////////////////////////////////////////////////////////////
- // Game Programming All In One, Third Edition
- // Tank War Final - bullet.c
- /////////////////////////////////////////////////////////////////////////
-
- #include "tankwar.h"
-
-
-
- void updateexplosion(int window, int num)
- {
- if (explosions[window][num]->alive)
- {
- //draw explosion in enemy window (target)
- explosions[window][num]->updateAnimation();
- if (explosions[window][num]->curframe == explosions[window][num]->totalframes-1)
- explosions[window][num]->alive = 0;
- explosions[window][num]->drawframe(buffer);
-
- }
-
- //play "end of explosion" sound
- play_sample(sounds[HARP], VOLUME, PAN, PITCH, FALSE);
- }
-
- void explode(int num, int x, int y)
- {
- if (!explosions[num][0]->alive)
- {
- explosions[num][0]->curframe = 0;
- explosions[num][0]->framecount = 0;
- explosions[num][0]->x = x - explosions[num][0]->width/2 + 16;
- explosions[num][0]->y = y - explosions[num][0]->height/2 + 16;
- explosions[num][0]->alive = 1;
- }
-
- if (!explosions[num][1]->alive)
- {
-
- //draw explosion in my window
- int x = scrollx[!num] + SCROLLW/2;
- int y = scrolly[!num] + SCROLLH/2;
-
- //is enemy tank visible in my window?
- if (inside(x, y,
- scrollx[num], scrolly[num],
- scrollx[num] + SCROLLW, scrolly[num] + SCROLLH))
- {
- //draw explosion in own window if enemy is visible
- explosions[num][1]->curframe = 0;
- explosions[num][1]->framecount = 0;
- explosions[num][1]->x = startx[num]+x-scrollx[num] - 48;
- explosions[num][1]->y = starty[num]+y-scrolly[num] - 48;
- explosions[num][1]->alive = 1;
- }
- }
- }
-
- void drawbullets()
- {
- int x,y;
-
- //is the bullet active?
- for (int num=0; num<2; num++)
- {
- if (bullets[num]->alive)
- {
- x = bullets[num]->x;
- y = bullets[num]->y;
-
- //draw bullet sprite in window if visible
- if (inside(x, y, scrollx[num], scrolly[num],
- scrollx[num] + SCROLLW,
- scrolly[num] + SCROLLH))
- {
- //draw bullet, adjust for scroll
- x = startx[num] - scrollx[num] + x;
- y = starty[num] - scrolly[num] + y;
-
- //rotate sprite, adjust for Allegro's 16.16 fixed trig
- int angle = bullets[num]->moveAngle + 90;
- rotate_sprite(buffer, bullets[num]->image, x, y,
- itofix(angle / 0.7f / 2.0f));
- }
- }
- }
- }
-
- void movebullets()
- {
- int tx, ty;
-
- for (int num=0; num<2; num++)
- {
-
- //is the bullet active?
- if (!bullets[num]->alive) return;
-
- //move bullet
- //bullets[num]->updatePosition();
- bullets[num]->x += bullets[num]->velx;
- bullets[num]->y += bullets[num]->vely;
- int x = bullets[num]->x;
- int y = bullets[num]->y;
-
- //stay within the virtual screen
- if (x < 0 || x > mapwidth*mapblockwidth-6 || y < 0 || y > mapheight*mapblockheight-6)
- {
- //reload
- bullets[num]->alive = 0;
- play_sample(sounds[AMMO], VOLUME, PAN, PITCH, FALSE);
- return;
- }
-
- //look for a direct hit using basic collision
- tx = scrollx[!num] + SCROLLW/2;
- ty = scrolly[!num] + SCROLLH/2;
-
- //start at center of scroll window, subtract 16 from
- //each side to encompass the tank
- if (inside(x,y,tx-16,ty-16,tx+16,ty+16))
- {
- //kill the bullet
- bullets[num]->alive = 0;
-
- //draw explosion in enemy window
- tanks[!num]->velx=0;
- tanks[!num]->vely=0;
- explode(num, tanks[!num]->x, tanks[!num]->y);
- health[!num] -= 2;
- if (health[!num] < 1) {
- gamestate = STATE_VICTORY;
- rest(200);
- play_midi(music_victory, 1);
- clear_keybuf();
- }
-
- //play explosion sounds
- play_sample(sounds[GOOPY], VOLUME, PAN, PITCH, FALSE);
- play_sample(sounds[HIT1], VOLUME, PAN, PITCH, FALSE);
- play_sample(sounds[HIT2], VOLUME, PAN, PITCH, FALSE);
-
- //kill any "near miss" sounds
- if (num)
- stop_sample(sounds[SCREAM]);
- else
- stop_sample(sounds[OHHH]);
- }
-
- else if (inside(x,y,tx-30,ty-30,tx+30,ty+30))
- {
- //it's a near miss!
- if (num)
- //player 1 screams
- play_sample(sounds[SCREAM], VOLUME, PAN, PITCH, FALSE);
- else
- //player 2 ohhhs
- play_sample(sounds[OHHH], VOLUME, PAN, PITCH, FALSE);
-
- }
- }
- }
-
-
- void fireweapon(int num)
- {
- //ready to fire again?
- if (!bullets[num]->alive)
- {
- //fire the bullet
- bullets[num]->alive = 1;
- bullets[num]->x = scrollx[num] + SCROLLW/2;
- bullets[num]->y = scrolly[num] + SCROLLH/2;
-
- //adjust velocity based on angle
- int angle = tanks[num]->moveAngle - 90;
- if (angle < 0) angle = 360 + angle;
- bullets[num]->moveAngle = angle;
- bullets[num]->velx = calcAngleMoveX(angle) * BULLETSPEED;
- bullets[num]->vely = calcAngleMoveY(angle) * BULLETSPEED;
-
- play_sample(sounds[FIRE], VOLUME, PAN, PITCH, FALSE);
- }
- }
-
-
-
-