home *** CD-ROM | disk | FTP | other *** search
/ C++ Games Programming / CPPGAMES.ISO / thx / demos / shootout / build / bitpart.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-06  |  4.6 KB  |  242 lines

  1. // ------- bitpart.cpp
  2.  
  3. #include <stdlib.h>
  4. #include "shootout.h"
  5.  
  6. BitPart::BitPart(char *gfxfile) : Player(gfxfile, "sounds.sfx")
  7. {
  8. }
  9.  
  10. BitPart::~BitPart()
  11. {
  12. }
  13.  
  14. void BitPart::initialize_bitpart()
  15. {
  16.     steps = 0;
  17.     door = 0;
  18.     location = nowhere;
  19.     mode = inactive;
  20.     noactivity = 0;
  21.     clipper.x1 = clipper.x2 = clipper.y1 = clipper.y2 = 0;
  22.     clipper.occupied = 0;
  23.     exitright = 1;
  24.     enterright = 1;
  25.     disappear();
  26. }
  27.  
  28. void BitPart::make_entrance()
  29. {
  30.     int sct = stepct/stepincr;
  31.     if (++steps < sct)    {
  32.         if (enterright)
  33.             setx(getx()+stepincr);
  34.         else 
  35.             setx(getx()-stepincr);
  36.     }
  37.     else if (steps >= sct && steps < sct + forwardsteps)    {
  38.         if (clipper.y1 == 0)
  39.             unclip();
  40.         setinterval(bpwalkinterval);
  41.         sety(gety()+1);
  42.         set_imageno((steps & 1) + 2);
  43.     }
  44.     else if (steps == sct + forwardsteps)    {
  45.         noactivity = 0;
  46.         mode = standing;
  47.         setinterval(1);
  48.         steps = 0;
  49.         if (door != 0)
  50.             door->close();
  51.         if (iscitizen() && on_the_street(location) &&
  52.                 ((Shootout*)director)->sheriff->isdead())
  53.             set_imageno(8);
  54.         else 
  55.             set_imageno(1);
  56.     }
  57. }
  58.  
  59. void BitPart::hit_the_mark()
  60. {
  61.     short int sheriffdead = ((Shootout*)director)->sheriff->isdead();
  62.     short int badguysdead = ((Shootout*)director)->badguys->getnum() == 3;
  63.     short int citizensdead = ((Shootout*)director)->citizens->getnum();
  64.     if (sheriffdead || badguysdead)    {
  65.         // -- either the sheriff is dead or all the badguys are dead
  66.         if (iscitizen() && !on_the_street(location))
  67.             // --- send citizens to the street
  68.             noactivity = waitcount;
  69.         else if (!iscitizen())
  70.             // --- badguys leave town
  71.             noactivity = waitcount;
  72.         else    {
  73.             if (sheriffdead)    {
  74.                 if (get_imageno() != 8)
  75.                     set_imageno(8);
  76.             }
  77.             else if (!citizensdead)
  78.                 // ---- all bad guys are dead and no citizens. applaud
  79.                 mode = applauding;
  80.         }
  81.     }
  82.     else    {
  83.         if (get_imageno() == 8)
  84.             set_imageno(1);
  85.         ++noactivity;
  86.     }
  87.     if (noactivity == waitcount)    {
  88.         mode = exiting;
  89.         setinterval(bpwalkinterval);
  90.         noactivity = 0;
  91.     }
  92. }
  93. void BitPart::exit_stage()
  94. {
  95.     int sct = stepct/stepincr;
  96.     if (steps == 0)    {
  97.         clip(clipper.x1, clipper.y1, clipper.x2, clipper.y2);
  98.         if (door != 0)
  99.             door->open();
  100.     }
  101.     if (steps < forwardsteps)    {
  102.         sety(gety()-1);
  103.         set_imageno((steps & 1) + 4);
  104.     }
  105.     else if (steps == forwardsteps)    {
  106.         exitright = door ? 1 : random(2);
  107.         setinterval(1);
  108.         set_imageno(9-exitright*3);
  109.     }
  110.     else if (steps > forwardsteps && steps < sct+forwardsteps)    {
  111.         if (exitright)
  112.             setx(getx()-stepincr);
  113.         else
  114.             setx(getx()+stepincr);
  115.     }
  116.     else if (steps == sct+forwardsteps)    {
  117.         mode = inactive;
  118.         if (door != 0)
  119.             door->close();
  120.         disappear();
  121.         ((Shootout*)director)->exitstage(this);
  122.         setinterval(1);
  123.         return;
  124.     }
  125.     steps++;
  126. }
  127.  
  128. void BitPart::swan_song()
  129. {
  130.     set_imageno(7);
  131.     if (getx() > 270)
  132.         setx(270);
  133.     mode = dying;
  134. }
  135.  
  136. void BitPart::fall()
  137. {
  138.     int y = gety();
  139.     if (y < sidewalk)
  140.         sety(y+10);
  141.     else    {
  142.         mode = dead;
  143.         play_sound_clip(3);
  144.         ((Shootout*)director)->exitstage(this);
  145.     }
  146. }
  147.  
  148. void BitPart::getshot()
  149. {
  150.     setinterval(1);
  151.     unclip();
  152.     mode = die;
  153.     if (door)
  154.         door->close();
  155. }
  156.  
  157. void BitPart::applaud()
  158. {
  159.     if (((Shootout*)director)->citizens->getnum())    {
  160.         // -- stop clapping and exit if the sheriff kills a citizen
  161.         setinterval(1);    // run, don't walk
  162.         mode = exiting;
  163.     }
  164.     else    {
  165.         if (((Shootout*)director)->sheriff->isdead())
  166.             mode = standing;
  167.         else    {
  168.             // ----- applause seqence
  169.             if (get_imageno() != 10)
  170.                 set_imageno(10);
  171.             else
  172.                 set_imageno(11);
  173.             if (!sound_clip_is_playing())
  174.                 play_sound_clip(5);
  175.         }
  176.     }
  177. }
  178.  
  179. void BitPart::update_position()
  180. {
  181.     switch (mode)    {
  182.         case entrance:
  183.             make_entrance();
  184.             break;
  185.         case standing:
  186.             hit_the_mark();
  187.             break;
  188.         case exiting:
  189.             exit_stage();
  190.             break;
  191.         case die:
  192.             swan_song();
  193.         case dying:
  194.             fall();
  195.             break;
  196.         case applauding:
  197.             applaud();
  198.             break;
  199.         default:
  200.             break;
  201.     }
  202. }
  203.  
  204. void BitPart::enter(Portal cl, Door *dr, Location loc)
  205. {
  206.     if (loc == alley2 && clipper.building == jail)
  207.         enterright = 0;
  208.     else if (loc == alley3 && clipper.building == saloon)
  209.         enterright = 0;
  210.     else 
  211.         enterright = 1;
  212.     door = dr;
  213.     if (door)
  214.         door->open();
  215.     location = loc;
  216.     clipper = cl;
  217.     mode = entrance;
  218.     noactivity = 0;
  219.     if (enterright)
  220.         setx(clipper.x1-stepct);
  221.     else
  222.         setx(clipper.x2);
  223.     sety(clipper.y1 ? clipper.y1-13 : 104);
  224.     set_imageno(1);
  225.     appear();
  226.     steps = 0;
  227. }
  228.  
  229. short int BitPart::zone()
  230. {
  231.     short int zn = 0;
  232.     if (mode != inactive && mode != dead && mode != dying)    {
  233.         zn = location - 5;
  234.         if (zn < 1)
  235.             zn = 1;
  236.         else if (zn > 5)
  237.             zn = 5;
  238.     }
  239.     return zn;
  240. }
  241.  
  242.