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

  1. // --------- badguy.cpp
  2.  
  3. #include <stdlib.h>
  4. #include "shootout.h"
  5. #include "options.h"
  6.  
  7. BadGuy::BadGuy(char *gfxfile) : BitPart(gfxfile)
  8. {
  9. }
  10.  
  11. void BadGuy::initialize_bitpart()
  12. {
  13.     BitPart::initialize_bitpart();
  14.     skill = -1;
  15.     hesitation = 0;
  16. }
  17.  
  18. short int BadGuy::drawbead()
  19. {
  20.     Sheriff *shp = ((Shootout*)director)->sheriff;
  21.     if (!shp->isdead() && abs(shp->zone()-zone()) < 2)
  22.         // --- the sheriff is in range
  23.         if (!((Shootout*)director)->isbadguy_shooting(this))
  24.             // --- no one else is drawing on the sheriff
  25.             return 1;
  26.     return 0;
  27. }
  28.  
  29. void BadGuy::update_position()
  30. {
  31.     if (skill == -1)    {
  32.         skill = options->aim->getnum();
  33.         hesitation = 5 - skill;
  34.     }
  35.     BitPart::update_position();
  36.     switch (mode)    {
  37.         case standing:
  38.             set_imageno(1);
  39.             setinterval(bpwalkinterval);
  40.             if (drawbead())
  41.                 // --- sheriff is in range
  42.                 mode = aiming;
  43.             else
  44.                 break;
  45.         case aiming:
  46.             if (hesitation-- == 0)    {
  47.                 mode = drawbead() ? shooting : exiting;
  48.                 hesitation = 5 - skill;
  49.             }
  50.             if (mode != shooting)
  51.                 break;
  52.         case shooting:
  53.             mode = exiting;
  54.             if (firegun())    {
  55.                 Sheriff *shp = ((Shootout*)director)->sheriff;
  56.                 shp->getshot();
  57.             }
  58.             break;
  59.         default:
  60.             break;
  61.     }
  62. }
  63.  
  64. short int BadGuy::firegun()
  65. {
  66.     setinterval(bpshootinterval);
  67.     set_imageno(8);
  68.     int rtn = skill ? (random((5-skill)+1) == 0) : 0;
  69.     play_sound_clip(rtn ? 1 : 1);    // : = ricochet later
  70.     return rtn;
  71. }
  72.