home *** CD-ROM | disk | FTP | other *** search
/ Media Share 13 / mediashare_13.zip / mediashare_13 / ZIPPED / PROGRAM / WTJ9403.ZIP / BARNHART / SO_CREEP.CPP < prev    next >
C/C++ Source or Header  |  1993-11-21  |  3KB  |  130 lines

  1.  
  2. // so_creep.cpp - Implements Creepers - thos little bugs running along 
  3. //                 platforms giving our hero a hard time
  4.  
  5. // Andy Barnhart
  6. // This program is provided as a sample to accompany an article on graphics
  7. //    animation using C++
  8. // This seems to work for me and I hope it works for you. No other warranty
  9. //    is expressed or implied.
  10.  
  11. #include <so_all.hpp>
  12.  
  13. #define CR_UP 0
  14. #define CR_DN 1
  15. #define CR_DEAD 2
  16.  
  17. Creeper::Creeper( char *res, Wall *wall, int ibx, int iex) : ScreenItem( NULL)
  18.     {
  19.     int i;
  20.     char acIc[32];
  21.     int len;
  22.  
  23.     strcpy( acIc, res);
  24.     len = strlen( acIc);
  25.     strcat( acIc, "0");
  26.  
  27.     for(i = 0; i < 3; i++)
  28.         {
  29.         acIc[len]++;
  30.         frames[i] = LoadBitmap( hInst, acIc);
  31.         }
  32.     SelectBM( frames[0]);
  33.     state = CR_UP;
  34.     live = TRUE;
  35.     if( ibx == -1)
  36.         {
  37.         bx = wall->x + width/2;
  38.         ex = (bx + (wall->width * wall->mems)) - width;
  39.         }
  40.     else
  41.         {
  42.         bx = ibx;
  43.         ex = iex;
  44.         }
  45.     Move( bx, wall->y);
  46.     OnTop( wall->y);
  47.     SetTarget( ex, y + height/2);
  48.     }
  49.  
  50. Creeper::~Creeper(void)
  51.     {
  52.     int i;
  53.     for(i = 0; i < 3; i++)
  54.         {
  55.         if (frames[i]) DeleteObject( frames[i]);
  56.         }
  57.     hbmImage = NULL;
  58.     }
  59.  
  60. BOOL Creeper::Update()
  61.     {
  62.     ScreenObj *SONxt;
  63.     RECT rc;
  64.  
  65.     if( !live) return( FALSE);
  66.     if( state == CR_UP)
  67.         state = CR_DN;
  68.     else
  69.         state = CR_UP;
  70.     SelectBM( frames[state]);
  71.     SetRect( &rc);
  72.     for( SONxt = Background->Next(); SONxt != NULL; SONxt = SONxt->Next())
  73.         {
  74.         if( (SONxt != this) && (SONxt->shown) && (SONxt->InRect( &rc)))
  75.             {
  76.             int hit;
  77.  
  78.             SONxt->Hit( TYPE_HAZARD);
  79.             hit = SONxt->Type();
  80.             if( (hit == TYPE_MISSILE) || (hit == TYPE_HAZARD) )
  81.                 {
  82.                 Hit( hit);
  83.                 return( FALSE);
  84.                 }
  85.             }
  86.         }
  87.     if (! ScreenItem::Update())
  88.         {
  89.         if(x > bx)
  90.             SetTarget( bx, y + height/2);
  91.         else
  92.             SetTarget( ex, y + height/2);
  93.         }
  94.     SetRect( &rc);
  95.     for( SONxt = Background->Next(); SONxt != NULL; SONxt = SONxt->Next())
  96.         {
  97.         if( (SONxt != this) && (SONxt->shown) && (SONxt->InRect( &rc)))
  98.             {
  99.             int hit;
  100.  
  101.             SONxt->Hit( TYPE_HAZARD);
  102.             hit = SONxt->Type();
  103.             if( (hit == TYPE_MISSILE) || (hit == TYPE_HAZARD) )
  104.                 {
  105.                 Hit(hit);
  106.                 return( FALSE);
  107.                 }
  108.             }
  109.         }
  110.     return( TRUE);
  111.     }
  112.  
  113. int Creeper::Type()
  114.     {
  115.     if( live) return( TYPE_HAZARD);
  116.     return(NULL);
  117.     }
  118.  
  119. BOOL Creeper::Hit( int type)
  120.     {
  121.     if( (type == TYPE_MISSILE) || (type == TYPE_HAZARD) )
  122.         {
  123.         state = CR_DEAD;
  124.         live = FALSE;
  125.         SelectBM( frames[state]);
  126.         Paint();
  127.         return( TRUE);
  128.         }
  129.     return( FALSE);
  130.     }