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

游戏

开发平台:

Visual C++

  1. /*****************************************************************************
  2. *                                                                             
  3. *   GameObjectsList.inl
  4. *                                                                             
  5. *   Electrical Engineering Faculty - Software Lab                             
  6. *   Spring semester 1998                                                      
  7. *                                                                             
  8. *   Tanks game                                                                
  9. *                                                                             
  10. *   Contents: Inline functions implementations.
  11. *                                                                             
  12. *   Authors: Eran Yariv - 28484475                                           
  13. *            Moshe Zur  - 24070856                                           
  14. *                                                                            
  15. *                                                                            
  16. *   Date: 23/09/98                                                           
  17. *                                                                            
  18. ******************************************************************************/
  19. inline CGameObjectsList::CGameObjectsList ()
  20. {
  21.     m_aArrays[GROUND_LEVEL].Init(1);      // Game board only
  22.     m_aArrays[LOWER_LEVEL].Init(MAX_POSSIBLE_OBJECTS);
  23.     m_aArrays[HIGHER_LEVEL].Init(MAX_POSSIBLE_OBJECTS);
  24.     m_aArrays[SKY_LEVEL].Init(MAX_POSSIBLE_OBJECTS); // Airplanes and game-over objects
  25. }
  26. inline CGameObjectsList::~CGameObjectsList ()
  27. {
  28.     KillList();
  29. }
  30. inline void
  31. CGameObjectsList::KillList ()
  32. {
  33.     for (int i = 0; i < 4; i++)
  34.         m_aArrays[i].Empty();
  35. }
  36. inline void
  37. CGameObjectsList::AddObject (CGameObject *pGameObj)
  38. {
  39.     ASSERT(pGameObj);
  40.     Freeze();
  41.     VERIFY(m_aArrays[pGameObj->GetHeight()].Add(pGameObj));
  42.     Thaw();
  43. }
  44. inline void 
  45. CGameObjectsList::RemoveObject (CGameObject *pGameObj)
  46. {
  47.     ASSERT(pGameObj);
  48.     Freeze();
  49.     VERIFY(m_aArrays[pGameObj->GetHeight()].Remove(pGameObj));
  50.     Thaw();
  51. }
  52. inline int 
  53. CGameObjectsList::GetObjectsCount () const
  54. {
  55.     return  m_aArrays[0].GetObjectsCount() +
  56.             m_aArrays[1].GetObjectsCount() +
  57.             m_aArrays[2].GetObjectsCount() +
  58.             m_aArrays[3].GetObjectsCount();
  59. }
  60. inline LIST_POS
  61. CGameObjectsList::GetHeadPosition ()
  62. {
  63.     LIST_POS lp;
  64.     lp.ind = 0;
  65.     lp.pos = m_aArrays[0].GetHeadPosition();
  66.     return lp;
  67. }
  68. inline CGameObject* 
  69. CGameObjectsList::GetNext (LIST_POS& lp)
  70. {
  71.     if (lp.ind < 0 || lp.ind > 3)
  72.         return NULL;
  73.     CGameObject* RetVal = m_aArrays[lp.ind].GetNext(lp.pos);
  74.     if (! RetVal) {     // We have reached current array's top
  75.         while (++lp.ind < 4)  // Advance to next non-empty array, if possible
  76.         {
  77.             lp.pos = m_aArrays[lp.ind].GetHeadPosition();
  78.             RetVal = m_aArrays[lp.ind].GetNext(lp.pos);
  79.             if (RetVal)
  80.                 // Success
  81.                 return RetVal;
  82.         }
  83.     }
  84.     return RetVal;
  85. }
  86. inline void
  87. CGameObjectsList::Freeze()
  88. {
  89.     m_CS.Lock();
  90. }
  91. inline void
  92. CGameObjectsList::Thaw()
  93. {
  94.     m_CS.Unlock();
  95. }