游戏中冷却时间的管理

游戏中道具、技能经常会有冷却时间这么个概念,在使用一次时,下次使用时要检测是否是在冷却时间内,不在则可用。

[cpp]  view plain  copy
  1. //冷却时间管理  
  2.   
  3. #include <iostream>  
  4. #include <map>  
  5. #include <boost/date_time/posix_time/posix_time.hpp>  
  6.   
  7. using namespace boost::posix_time;  
  8.   
  9. class CoolDownMgr  
  10. {  
  11.     public:  
  12.         bool Load(char* pData, int len);  
  13.         bool Save();  
  14.           
  15.         void Add(int guid, int dums)  
  16.         {  
  17.             CoolDownMap::iterator iter = m_CoolDownMap.find(guid);  
  18.             if (m_CoolDownMap.end() != iter)  
  19.             {  
  20.                 (iter->second).m_SaveTime = second_clock::utc_time();  
  21.             }  
  22.             else  
  23.             {  
  24.                 CoolDownVal val(dums);  
  25.                 m_CoolDownMap.insert(std::pair(guid, val));  
  26.             }  
  27.         }  
  28.         bool IsCoolDown(int guid)  
  29.         {  
  30.             CoolDownMap::iterator iter = m_CoolDownMap.find(guid);  
  31.             if (m_CoolDownMap.end() == iter)  
  32.             {  
  33.                 return false;  
  34.             }  
  35.             CoolDownVal& val = iter->second;  
  36.             return (econd_clock::utc_time() - val.m_SaveTime) > val.m_Duration;  
  37.         }  
  38.           
  39.     private:  
  40.         struct CoolDownVal  
  41.         {  
  42.             ptime m_SaveTime;  
  43.             int m_Duration;  
  44.             CoolDownVal(duration):  
  45.                 m_SaveTime(second_clock::utc_time())  
  46.                 , m_Duration(duration)  
  47.                 {  
  48.                 }  
  49.         };  
  50.         typedef std::map<int, CoolDownVal> CoolDownMap;  
  51.         CoolDownMap m_CoolDownMap;  
  52. }   

猜你喜欢

转载自blog.csdn.net/larry_zeng1/article/details/80074712