home *** CD-ROM | disk | FTP | other *** search
/ Cutting-Edge 3D Game Programming with C++ / CE3DC++.ISO / BOOK / CHAP12 / MONSTER.CPP < prev    next >
C/C++ Source or Header  |  1996-01-24  |  4KB  |  153 lines

  1. //
  2. // File name: Monster.CPP
  3. //
  4. // Description: The monster implementation
  5. //
  6. // Author: John De Goes
  7. //
  8. // Project: Cutting Edge 3D Game Programming
  9. //
  10.  
  11. #include <StdIO.H>
  12.  
  13. #include "Monster.HPP"
  14. #include "PalShade.HPP"
  15. #include "3DClass.HPP"
  16. #include "LineType.HPP"
  17.  
  18. Monster::Monster ( float Sx, float Sy, float Sz, char *IniFile,
  19.                    int Mode )
  20.    {
  21.    FILE *InFile;
  22.    long Value, Count;
  23.    // Initialize the position and direction vector:
  24.    Pos.Lx = Sx; Pos.Ly = Sy; Pos.Lz = Sz;
  25.    Dir.Lx = Sx; Dir.Ly = Sy; Dir.Lz = Sz + 10.0F; // Point down Z
  26.    Width = Height = FrameNum = FrameCount = 0;
  27.    Health = 100;
  28.    Frame = NULL;
  29.    char String [ 256 ];
  30.    if ( Mode == ASCII )
  31.       {
  32.       if ( ( InFile = fopen ( IniFile, "rt" ) ) == 0 )
  33.          return;
  34.       Value = fscanf ( InFile, "%s", String );
  35.       while ( ( Value != EOF ) && ( Value != 0 ) )
  36.             {
  37.             ++FrameCount;
  38.             Value = fscanf ( InFile, "%s", String );
  39.             }
  40.       rewind ( InFile );
  41.       Frame = new BMPImage [ FrameCount ];
  42.       for ( Count = 0; Count < FrameCount; Count++ )
  43.           {
  44.           fscanf ( InFile, "%s", String );
  45.           Frame [ Count ].Load ( String );
  46.           }
  47.       Width  = Frame [ 0 ].Width;
  48.       Height = Frame [ 0 ].Height;
  49.       }
  50.    if ( Mode == Binary )
  51.       {
  52.       }
  53.    }
  54.  
  55. BOOL Monster::Show ( Matrix3D &M, BYTE *Dest )
  56.    {
  57.    float Px, Py, Pw, Ph, Ratio;
  58.    long TexLeft = 0, TexTop = 0, Tx, Ty,
  59.         Left, Top, Right, Bottom, TIndex,
  60.         DIndex, Z;
  61.    unsigned int Shade;
  62.    BYTE *Texture = Frame [ FrameNum ].Image, Color;
  63.    // Transform the position and direction vectors:
  64.    M.Transform ( Pos );
  65.    M.Transform ( Dir );
  66.    if ( Pos.Wz > MAXZ )
  67.       return FALSE;
  68.    Shade = Pos.Wz / SHADE_DIV;
  69.    if ( Shade > ( SHADE_COUNT - 1 ) )
  70.       Shade = ( SHADE_COUNT - 1 );
  71.    Shade = Shade << 8;
  72.    if ( Pos.Wz > MINZ )
  73.       {
  74.       Z = ( 1.0F / float ( Pos.Wz ) ) * float ( 1 << ZSTEP_PREC );
  75.       Z += ZTrans;
  76.       Px = Pos.Wx * XSCALE / Pos.Wz + XCENTER;
  77.       Py = Pos.Wy * YSCALE / Pos.Wz + YCENTER;
  78.       Pw = ( float ) Width  * fabs ( XSCALE ) * 10.0F / Pos.Wz;
  79.       Ph = ( float ) Height * fabs ( YSCALE ) * 10.0F / Pos.Wz;
  80.       Left   = Px - ( Pw / 2.0F );
  81.       Right  = Px + ( Pw / 2.0F );
  82.       Top    = Py - ( Ph / 2.0F );
  83.       Bottom = Py + ( Ph / 2.0F );
  84.       // Take care of the easy clipping first:
  85.       if ( Right > MAXX )
  86.          Right = MAXX;
  87.       if ( Bottom > MAXY )
  88.          Bottom = MAXY;
  89.       // Make sure the monster is on the screen:
  90.       if ( Right < MINX )
  91.          return FALSE;
  92.       if ( Bottom < MINY )
  93.          return FALSE;
  94.       if ( Left >= MAXX )
  95.          return FALSE;
  96.       if ( Top >= MAXY )
  97.          return FALSE;
  98.       // Take care of the hard clipping next:
  99.       if ( Left < MINX )
  100.          {
  101.          Ratio = ( float ) Width  / Pw;
  102.          TexLeft += ( MINX - Left ) * Ratio;
  103.          Left = MINX;
  104.          }
  105.       if ( Top < MINY )
  106.          {
  107.          Ratio = ( float ) Height / Ph;
  108.          TexTop += ( MINY - Top ) * Ratio;
  109.          Top = MINY;
  110.          }
  111.       long U, V, UStep, VStep;
  112.       V = ( TexTop << 16 );
  113.       UStep = ( Width  << 16 ) / Pw;
  114.       VStep = ( Height << 16 ) / Ph;
  115.       for ( Ty = Top; Ty < Bottom; Ty++ )
  116.           {
  117.           U = ( TexLeft << 16 );
  118.           DIndex = Ty * 320 + Left;
  119.           TIndex = ( V >> 16 ) * Width;
  120.           for ( Tx = Left; Tx < Right; Tx++ )
  121.               {
  122.               if ( ZBuffer [ DIndex ] < Z )
  123.                  {
  124.                  Color  = Texture [ TIndex + ( U >> 16 ) ];
  125.                  if ( Color )
  126.                     {
  127.                     Dest [ DIndex ] = TextShade.Shade [ Shade + Color ];
  128.                     }
  129.                  }
  130.               U += UStep;
  131.               ++DIndex;
  132.               }
  133.           V += VStep;
  134.           }
  135.       }
  136.    return TRUE;
  137.    }
  138.  
  139. void Monster::MoveToward ( float Tx, float Ty, float Tz,
  140.                            float Steps, Matrix3D &M )
  141.    {
  142.    Tx; Ty; Tz; Steps;
  143.    Point3D P;
  144.    M.Transform ( Pos );
  145.    P.Wx = Pos.Wx + ( Tx - Pos.Wx ) / Steps;
  146.    P.Wy = Pos.Wy; // Don't change the monster's height
  147.    P.Wz = Pos.Wz + ( Tz - Pos.Wz ) / Steps;
  148.    M.Untransform ( P );
  149.    Pos.Lx = P.Lx;
  150.    Pos.Ly = P.Ly;
  151.    Pos.Lz = P.Lz;
  152.    }
  153.