home *** CD-ROM | disk | FTP | other *** search
/ C++ Games Programming / CPPGAMES.ISO / thx / demos / skyscrap / build / heli.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-13  |  3.6 KB  |  172 lines

  1. #include <stdlib.h>
  2. #include "heli.h"
  3. #include "gfx.h"
  4. #include "sfx.h"
  5. #include "global.h"
  6. #include "messages.h"
  7.  
  8. void Helicopter::Setup( Director* director )
  9. {
  10.   MaxNumShots=10-1;
  11.   set_director( director );
  12.   for ( int i=0; i<MaxNumShots; i++ )
  13.     rocket[i].set_director( director );
  14. }
  15.  
  16. void Helicopter::initialize()
  17. {
  18.   load_gfxlib( "ss.gfx" );
  19.   load_sfxlib( "ss.sfx" );
  20.   // check for kill
  21.   request_message_cue( HelicopterHit, (callback)&Helicopter::OnHit );
  22. }
  23.  
  24. void Helicopter::GetNewLocation(void)
  25. {
  26.   if ( my_director() )
  27.   {
  28.     loc.y=1-get_image_height( HELICOPTERa );
  29.     randomize();
  30.     loc.x=rand()%320;
  31.   }
  32. }
  33.  
  34. void Helicopter::Reset(void)
  35. {
  36.   for ( int i=0; i<MaxNumShots; i++ )
  37.     rocket[i].Reset();
  38.   HelicopterRect.w=width=get_image_width( HELICOPTERa );
  39.   HelicopterRect.h=height=get_image_height( HELICOPTERa );
  40.   ExplosionWidth=get_image_width( EXPLOSION )/2; 
  41.   ExplosionHeight=get_image_height( EXPLOSION )/2; 
  42.   GetNewLocation();
  43.   alive=YES;
  44.   for ( i=0; i<MaxNumShots; i++ )
  45.     rocket[i].Deactivate();
  46. }
  47.  
  48. void Helicopter::Move(void)
  49. {
  50.   if ( !alive )
  51.     return;
  52.  
  53.   // movement step
  54.   const int MoveLength=4;
  55.   // 0=left, 1=right
  56.   static int dir=1;
  57.   static int hmax=160-(width/2);
  58.   // move ship left and right
  59.   if ( dir==1 )
  60.   {
  61.     if ( loc.x>=hmax )
  62.       dir=0, loc.x-=MoveLength;
  63.     else
  64.       loc.x+=MoveLength;
  65.   }
  66.   else
  67.   {
  68.     if ( loc.x==0 || loc.x-MoveLength<0 )
  69.       dir=1, loc.x+=MoveLength;
  70.     else
  71.       loc.x-=MoveLength;
  72.   }
  73.   // 0=up, 1=down
  74.   static int vdir=1;
  75.   // move ship up and down
  76.   if ( vdir==1 )
  77.   {
  78.     if ( loc.y>200 )
  79.     {
  80.       static GoneUp=FALSE;
  81.       randomize();
  82.       vdir=rand()%2;
  83.       if ( vdir==0 && GoneUp==FALSE )
  84.         loc.y-=MoveLength, GoneUp=TRUE;
  85.       else
  86.         GetNewLocation(), GoneUp=FALSE;
  87.     }
  88.     else
  89.       loc.y+=MoveLength;
  90.   }
  91.   else
  92.   {
  93.     if ( loc.y<=100 )
  94.       vdir=1, loc.y+=MoveLength;
  95.     else
  96.       loc.y-=MoveLength;
  97.   }
  98.   HelicopterRect.l=loc.x, HelicopterRect.r=loc.x+width;
  99.   HelicopterRect.t=loc.y, HelicopterRect.b=loc.y+height;
  100.   // check to see if stealth is in our line of fire
  101.   if ( HorizontalProximityCheck( HelicopterRect, StealthRect ) )
  102.     FireRocket();
  103. }
  104.  
  105. void Helicopter::OnDraw(void)
  106. {
  107.   static int ImageNum;
  108.  
  109.   if ( !alive )
  110.   {
  111.     // display and sound explosion
  112.     int x=(HelicopterRect.l+(width/2))-ExplosionWidth;
  113.     int y=(HelicopterRect.t+(height/2))-ExplosionHeight;
  114.     show_clipped_image( x, y, EXPLOSION );
  115.     return;
  116.   }
  117.  
  118.   ImageNum=1-ImageNum;
  119.   show_clipped_image( loc.x, loc.y, ImageNum ? HELICOPTERa:HELICOPTERb );
  120.   for ( int i=0; i<MaxNumShots; i++ )
  121.   {
  122.     if ( rocket[i].IsActive() )
  123.     {
  124.       if ( rocket[i].Move()==FALSE )
  125.         rocket[i].Deactivate();
  126.       else
  127.         rocket[i].OnDraw();
  128.     }
  129.   }
  130. }
  131.  
  132. void Helicopter::FireRocket(void)
  133. {
  134.   static int FirePause;
  135.   FirePause++;
  136.   if ( FirePause<5 )
  137.     return;
  138.   FirePause=0;
  139.  
  140.   for ( int i=0; i<MaxNumShots; i++ )
  141.   {
  142.     if ( !rocket[i].IsActive() )
  143.     {
  144.       play_sound_clip( SND_ROCKET );
  145.       rocket[i].Activate( loc.x+16, loc.y+32 );
  146.       break;
  147.     }
  148.   }
  149. }
  150.  
  151. static int HelisLeft=2;
  152. void Helicopter::OnHit(int)
  153. {
  154.   HelisLeft--;
  155.   if ( HelisLeft==0 )
  156.   {
  157.     // do the kill thang
  158.     for ( int i=0; i<MaxNumShots; i++ )
  159.       rocket[i].Deactivate();
  160.     alive=NO;
  161.     HelisLeft=2;
  162.     MsgInt=250;
  163.     post_message( ScoreUpdate, 0 );
  164.     post_message( HelicopterDestroyed, 0 );
  165.   }
  166. }
  167.  
  168. void Helicopter::SoundDestroy(void)
  169. {
  170.   play_sound_clip( SND_EXPLODE );
  171. }
  172.