GameObject.h
上传用户:royluo
上传日期:2007-01-05
资源大小:1584k
文件大小:8k
源码类别:

游戏

开发平台:

Visual C++

  1. /*****************************************************************************
  2. *                                                                             
  3. *   GameObject.h
  4. *                                                                             
  5. *   Electrical Engineering Faculty - Software Lab                             
  6. *   Spring semester 1998                                                      
  7. *                                                                             
  8. *   Tanks game                                                                
  9. *                                                                             
  10. *   Module description: Implements the game object class hierarchy.
  11. *                       
  12. *                                                                             
  13. *   Authors: Eran Yariv - 28484475                                           
  14. *            Moshe Zur  - 24070856                                           
  15. *                                                                            
  16. *                                                                            
  17. *   Date: 23/09/98                                                           
  18. *                                                                            
  19. ******************************************************************************/
  20. #ifndef _GAME_OBJECT_H
  21. #define _GAME_OBJECT_H
  22. #include <ImageManager.h>
  23. #include <GameConsts.h>
  24. #include <Reaction.h>
  25. class CGameObjectsList; // Pre-declaration
  26. typedef struct {
  27.     double dXDir, dYDir;
  28. } DirectionType;
  29. typedef struct {
  30.     double dXPos, dYPos;
  31. } PrecisePosType;
  32. typedef enum {STATE_DEAD, STATE_ALIVE} StateType;
  33. typedef enum {TANK, BOARD, SHELL, 
  34.               BULLET, MINE, BOMB, 
  35.               BOMBER, BONUS, GAMEOVER} GameObjectType;
  36. typedef enum {GROUND_LEVEL,     // Add object at ground level (lowest) - there's room for only one object there.
  37.               LOWER_LEVEL,      // Add object above ground level 
  38.               HIGHER_LEVEL,     // Add object above lower level
  39.               SKY_LEVEL         // Add object at sky level (highest) - there's room for only one object there.
  40.              } ObjectHeightType;
  41. class CGameObject : public CObject
  42. {
  43. public:
  44. typedef enum {  X_FULL_IN_MAP       =   0x01,   // Map fully contains the object in the X axis
  45.                 X_PARTIAL_IN_MAP    =   0x02,   // Map partailly contains the object in the X axis
  46.                 X_NOT_IN_MAP        =   0x04,   // Map doesn't contains the object in the X axis
  47.                 Y_FULL_IN_MAP       =   0x08,   // Map fully contains the object in the Y axis
  48.                 Y_PARTIAL_IN_MAP    =   0x10,   // Map partailly contains the object in the Y axis
  49.                 Y_NOT_IN_MAP        =   0x20    // Map doesn't contains the object in the Y axis
  50.              } MapPositionFlags;
  51.     CGameObject ();
  52.     void SetPos (UINT uXPos, UINT uYPos);
  53.     void SetSize (UINT uXSize, UINT uYSize);
  54.     virtual ~CGameObject () {}
  55.     virtual StateType           CalcState (DWORD dwCurTime)     = 0;
  56.     virtual CPoint &            GetPos();
  57.     virtual ObjectHeightType    GetHeight()                     = 0;
  58.     virtual HIMAGE              GetImage()                 = 0;
  59.     virtual CReaction           React(CGameObject *pTo)         = 0;
  60.     virtual GameObjectType      GetType()                       = 0;
  61.     virtual void                Kill();
  62.     virtual CRect &             GetUpdateRectangle();
  63.     virtual CSize &             GetDimensions();
  64.         // This method is here only for the tank objects:
  65.     virtual                     UINT GetID ();
  66.     BOOL                        HasImageChanged ();
  67.     BYTE                        GetSector();        // Gets sector on map
  68.     BYTE                        GetPosCheckSum();   // Get position packed into a byte
  69.         // Returns bits representing flags from MapPositionFlags
  70.     WORD                        GetMapPosition (CPoint &pos); 
  71.     void                        SetListPosition (POSITION);
  72.     POSITION                    GetListPosition ();
  73.     void                        SetArrayPosition (CGameObject**);
  74.     CGameObject**               GetArrayPosition ();
  75.     BOOL                        CollidesWith (CGameObject *);
  76.     BOOL                        CollidesWith (CGameObject* pOtherObj, CRect& MyRect);
  77. protected:
  78.     CPoint          m_Pos;                  // Object position
  79.     CSize           m_Size;                 // Object size
  80.     POSITION        m_ListPosition;         // Object position in objects list
  81.     CGameObject**   m_ppArrayPosition;      // Object position in objects array
  82.     CRect           m_UpdateRect;           // Update rectangle
  83.     BOOL            m_bImageChanged;        // Indicate a change in display
  84.     BOOL BitMaskCollides (CRect& IntersectRect, CPoint& myPos, CPoint& otherPos,
  85.                           CBitMask* myBitMask, CBitMask* otherBitMask);
  86.         // The following refrences are shortcuts to global structures:
  87.     CImageManager      &m_GlobalImageManager;
  88.     CGameObjectsList   &m_GlobalObjsList;
  89. };
  90. /* ------------------------- Moving object ------------------------- */
  91. class CMovingGameObject : virtual public CGameObject
  92. {
  93. public:
  94.     CMovingGameObject (UINT uXPos, UINT uYPos, UINT uXSize, UINT uYSize,
  95.                        UINT uDirectionIndex = 12, 
  96.                        double dVelocity = 0.0);
  97.     virtual ~CMovingGameObject () {}
  98.         // Calculates new position. Returns move distance (squared) or -1 if out of map
  99.     int             CalcNewPos(DWORD dwCurTime, BOOL bMustBeFullyInMap = TRUE);
  100.     void            SaveMovement ();
  101.     void            UndoMovement ();
  102.     void            StopMovement ();
  103.     virtual CRect & GetUpdateRectangle();
  104.     void            SetNewPos (CPoint &);
  105.     void            SetNewPos (int x, int y);
  106. protected:
  107.     UINT                        m_uDirectionIndex;  // Direction in m_dDirectionsArr
  108.     static const DirectionType  m_dDirectionsArr[]; // all possible directions
  109.     DWORD                       m_uLastTick;        // Holds the last time tick the tank state was updated
  110.     double                      m_dVelocity;        // Velocity in pixels per second
  111.     BOOL                        CalcPosAtTime (CPoint &StartPos,
  112.                                                DWORD dwTimeGap,
  113.                                                CPoint &ResPos,
  114.                                                BOOL bMustBeFullyInMap = TRUE);
  115. private:
  116.     PrecisePosType              m_PrecPos,          // Precise position
  117.                                 m_PrecPosCopy,      // Copy of precise position (for SaveMovement / UndoMovement)
  118.                                 m_PrevPrecPos,      // Previous precise position
  119.                                 m_PrevPrecPosCopy;  // Copy of previous precise position (for SaveMovement / UndoMovement)
  120.     CPoint                      m_PosCopy;          // Copy of integer position
  121.     static const double         m_cdThousandth;     // Constant 1/1000 - for faster calcs.
  122. };
  123. /* ------------------------- Exploding object ------------------------- */
  124. class CExplodingGameObject : virtual public CGameObject
  125. {
  126. public:
  127.     CExplodingGameObject (UINT uXPos, UINT uYPos, UINT uXSize, UINT uYSize,
  128.                           UINT uMaxIntensity,
  129.                           CImageManager::ImageType uExplosionImageIndex,
  130.                           BOOL InitiallyExploding = FALSE);
  131.     virtual ~CExplodingGameObject() {}
  132.     virtual HIMAGE  GetImage();
  133.     virtual CRect & GetUpdateRectangle();
  134.     UINT    CalcRelativeExplosionIntensity (CGameObject *pOtherObject, UINT MinRadius, UINT MaxRadius);
  135.     void    Explode ();    // Set explosion to true and switch image
  136.     BOOL    IsExplosionOver (); // Are we exploding and after last frame ?
  137.     BOOL    CheckAndSetTankNotification (UINT uTankID);   // TankID = 0..3
  138. protected:
  139.     BOOL            m_bIsExploding,                 // Are we exploding now ?
  140.                     m_bNotifiedTanks[MAX_TANKS];    // What tanks did we hit already?
  141.     HIMAGE          m_himgExplode,                  // Explosion image
  142.                    *m_pCurImage;                    // Current image
  143.     UINT            m_uMaxIntensity;                // Maximal intensity
  144. };
  145. // Inline sections:
  146. #include <GameObject.inl>
  147. #endif