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

游戏

开发平台:

Visual C++

  1. /*****************************************************************************
  2. *                                                                             
  3. *   Bullet.cpp                                                            
  4. *                                                                             
  5. *   Electrical Engineering Faculty - Software Lab                             
  6. *   Spring semester 1998                                                      
  7. *                                                                             
  8. *   Tanks game                                                                
  9. *                                                                             
  10. *   Module description: Implements the bullet object.
  11. *                       
  12. *                                                                             
  13. *   Authors: Eran Yariv - 28484475                                           
  14. *            Moshe Zur  - 24070856                                           
  15. *                                                                            
  16. *                                                                            
  17. *   Date: 23/09/98                                                           
  18. *                                                                            
  19. ******************************************************************************/
  20. #include <stdafx.h>
  21. #include <Bullet.h>
  22. CBullet::CBullet (UINT uXPos, UINT uYPos, UINT uDirectionIndex, UINT uParentTankID) :
  23.     CMovingGameObject (uXPos, uYPos, BULLET_WIDTH, BULLET_HEIGHT, 
  24.                        uDirectionIndex, BULLET_SPEED),
  25.     m_Death (DEATH_NOTDEAD),
  26.     m_bExplodedOnTank (FALSE),
  27.     m_uParentTankID (uParentTankID),
  28.     m_uWaitToTellTank (0)
  29. {
  30.     m_himgBullet = m_GlobalImageManager.GetImage (CImageManager::IMG_BULLET);
  31.     m_Origin = m_Pos;
  32. }
  33. StateType 
  34. CBullet::CalcState (DWORD dwCurTime)
  35. {
  36.     m_bImageChanged = FALSE;    // Assume no change since last CalcState
  37.     if (m_Death != DEATH_NOTDEAD &&     // Bullet is dead and
  38.         m_Death != DEATH_HITTANK)       // not waiting to tell that to any tank
  39.     {
  40.         return STATE_DEAD;
  41.     }
  42.     if (DEATH_HITTANK == m_Death)
  43.     {   // We are dead because we hit a tank
  44.         if (m_bExplodedOnTank)
  45.         {   // and he knows it
  46.             return STATE_DEAD;
  47.         }
  48.         else
  49.         {   // He doesn't know it yet...
  50.             if (m_uWaitToTellTank++ < (DEFAULT_RENDER_FREQ / 2))
  51.             {   // If less than half a second passed and the tank
  52.                 // still doesn't know it was hit, wait on
  53.                 return STATE_ALIVE;
  54.             }
  55.             else
  56.             {   // Otherwise, we can't just hang in one spot.
  57.                 // Kill the bullet.
  58.                 return STATE_DEAD;
  59.             }
  60.         }
  61.     }
  62.     // We're still alive here....
  63.     // Try to advance...
  64.     int iDistSqr = CalcNewPos (dwCurTime);
  65.     if (iDistSqr < 0) 
  66.     {   // Out of map situation
  67.         m_Death = DEATH_OUTOFSCREEN;
  68.         return STATE_DEAD;
  69.     }
  70.     // Recalc iDistSqr to be distance from point of fire
  71.     int iXDist = m_Origin.x - m_Pos.x,
  72.         iYDist = m_Origin.y - m_Pos.y;
  73.     iDistSqr = iXDist * iXDist + iYDist * iYDist;
  74.     if (BULLET_DISTANCE_SQR <= iDistSqr)
  75.     {   // Distance expired situation
  76.         m_Death = DEATH_OUTOFRANGE;
  77.         return STATE_DEAD;
  78.     }
  79.     // See what the rest of the world thinks about our movement
  80.     CReaction react = m_GlobalObjsList.GetGameReaction (this);
  81.     TerrainType ter = react.GetTerrainDifficulty();
  82.     if ((ter < TERR_BLOCKED) ||  
  83.             // Cool - didn't hit a thing or ...
  84.         ((HIT_TANK == ter) && (react.GetTankID() == m_uParentTankID)))
  85.             // Hit a tank but it's our father
  86.     {
  87.         m_bImageChanged = TRUE; // We moved a bit
  88.         return STATE_ALIVE;
  89.     }
  90.     if (ter == TERR_BLOCKED)
  91.     {   // Hit a wall
  92.         m_Death = DEATH_HITWALL;
  93.         return STATE_DEAD;
  94.     }
  95.     // Now we're in the (ter == HIT_TANK) situation
  96.     m_Death = DEATH_HITTANK;
  97.     m_uHitTankID = react.GetTankID();   // Store the tank ID we hit
  98.     return STATE_ALIVE; // We can't die until the tank get notified we hit it.
  99. }
  100.         
  101. CReaction
  102. CBullet::React(CGameObject *pTo)
  103. {
  104.     if ((m_Death == DEATH_NOTDEAD) ||   // Either we're still flying
  105.         (pTo->GetType() != TANK)   ||   // or we're not reacting to tanks
  106.         m_bExplodedOnTank)              // or we already exploded
  107.         return CReaction(); // empty reaction
  108.     // Otherwise, we're in STATE_ALIVE and DEATH_HITTANK and this is a tank
  109.     ASSERT (DEATH_HITTANK == m_Death);
  110.     ASSERT (m_uHitTankID < MAX_TANKS);
  111.     if ((TANK == pTo->GetType()) &&     // We're being asked to react to a tank and
  112.         (pTo->GetID() == m_uHitTankID)) // this tank is the tank we hit
  113.     {   // Now, this is the tank we're hitting
  114.         m_bExplodedOnTank = TRUE;   // Finally !!!
  115.         return CReaction (BULLET_INTENSITY);
  116.     }
  117.     return CReaction ();    // This is not the tank we're hitting
  118. }