游戏中道具、技能经常会有冷却时间这么个概念,在使用一次时,下次使用时要检测是否是在冷却时间内,不在则可用。
- //冷却时间管理
- #include <iostream>
- #include <map>
- #include <boost/date_time/posix_time/posix_time.hpp>
- using namespace boost::posix_time;
- class CoolDownMgr
- {
- public:
- bool Load(char* pData, int len);
- bool Save();
- void Add(int guid, int dums)
- {
- CoolDownMap::iterator iter = m_CoolDownMap.find(guid);
- if (m_CoolDownMap.end() != iter)
- {
- (iter->second).m_SaveTime = second_clock::utc_time();
- }
- else
- {
- CoolDownVal val(dums);
- m_CoolDownMap.insert(std::pair(guid, val));
- }
- }
- bool IsCoolDown(int guid)
- {
- CoolDownMap::iterator iter = m_CoolDownMap.find(guid);
- if (m_CoolDownMap.end() == iter)
- {
- return false;
- }
- CoolDownVal& val = iter->second;
- return (econd_clock::utc_time() - val.m_SaveTime) > val.m_Duration;
- }
- private:
- struct CoolDownVal
- {
- ptime m_SaveTime;
- int m_Duration;
- CoolDownVal(duration):
- m_SaveTime(second_clock::utc_time())
- , m_Duration(duration)
- {
- }
- };
- typedef std::map<int, CoolDownVal> CoolDownMap;
- CoolDownMap m_CoolDownMap;
- }