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

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