Magic_Game/core/Tool/Timer.cpp

146 lines
2.6 KiB
C++
Raw Normal View History

2017-10-21 19:09:31 +08:00
#include "..\etools.h"
#include "..\enodes.h"
#include "..\emanagers.h"
2017-10-17 21:22:25 +08:00
e2d::ETimer::ETimer()
: m_bRunning(false)
, m_nRunTimes(0)
, m_pParentNode(nullptr)
, m_Callback(nullptr)
2018-01-30 16:45:38 +08:00
, m_fInterval(0)
, m_fLast(0)
2017-12-15 21:51:07 +08:00
, m_nRepeatTimes(-1)
2017-10-21 19:09:31 +08:00
, m_bAtOnce(false)
2017-10-17 21:22:25 +08:00
{
}
2018-01-30 16:45:38 +08:00
e2d::ETimer::ETimer(const TimerCallback & callback, int repeatTimes /* = -1 */, float interval /* = 0 */, bool atOnce /* = false */)
2017-12-15 21:51:07 +08:00
: m_bRunning(false)
, m_nRunTimes(0)
, m_pParentNode(nullptr)
, m_Callback(nullptr)
2018-01-30 16:45:38 +08:00
, m_fInterval(0)
, m_fLast(0)
2017-12-15 21:51:07 +08:00
, m_nRepeatTimes(-1)
, m_bAtOnce(false)
2017-10-17 21:22:25 +08:00
{
2017-10-21 19:09:31 +08:00
this->setCallback(callback);
this->setRepeatTimes(repeatTimes);
this->setInterval(interval);
m_bAtOnce = atOnce;
2017-10-17 21:22:25 +08:00
}
2018-01-30 16:45:38 +08:00
e2d::ETimer::ETimer(const EString & name, const TimerCallback & callback, int repeatTimes /* = -1 */, float interval /* = 0 */, bool atOnce /* = false */)
2017-12-15 21:51:07 +08:00
: m_bRunning(false)
, m_nRunTimes(0)
, m_pParentNode(nullptr)
, m_Callback(nullptr)
2018-01-30 16:45:38 +08:00
, m_fInterval(0)
, m_fLast(0)
2017-12-15 21:51:07 +08:00
, m_nRepeatTimes(-1)
, m_bAtOnce(false)
2017-10-17 21:22:25 +08:00
{
2017-10-21 19:09:31 +08:00
this->setName(name);
this->setCallback(callback);
this->setRepeatTimes(repeatTimes);
this->setInterval(interval);
m_bAtOnce = atOnce;
2017-10-17 21:22:25 +08:00
}
bool e2d::ETimer::isRunning() const
{
return m_bRunning;
2017-10-17 21:22:25 +08:00
}
void e2d::ETimer::start()
{
m_bRunning = true;
2018-01-30 16:45:38 +08:00
m_fLast = ETime::getTotalTime();
2017-10-17 21:22:25 +08:00
}
void e2d::ETimer::stop()
{
m_bRunning = false;
}
e2d::EString e2d::ETimer::getName() const
{
return m_sName;
}
e2d::ENode * e2d::ETimer::getParentNode() const
{
return m_pParentNode;
}
void e2d::ETimer::setName(const EString & name)
{
m_sName = name;
}
2018-01-30 16:45:38 +08:00
void e2d::ETimer::setInterval(float interval)
2017-10-17 21:22:25 +08:00
{
2018-01-30 16:45:38 +08:00
m_fInterval = max(interval, 0);
2017-10-17 21:22:25 +08:00
}
2018-01-30 16:45:38 +08:00
void e2d::ETimer::setCallback(const TimerCallback & callback)
2017-10-21 19:09:31 +08:00
{
m_Callback = callback;
}
void e2d::ETimer::setRepeatTimes(int repeatTimes)
{
m_nRepeatTimes = repeatTimes;
}
void e2d::ETimer::setRunAtOnce(bool bAtOnce)
{
m_bAtOnce = bAtOnce;
}
2017-10-17 21:22:25 +08:00
void e2d::ETimer::bindWith(EScene * pParentScene)
{
ETimerManager::bindTimer(this, pParentScene);
}
void e2d::ETimer::bindWith(ENode * pParentNode)
{
ETimerManager::bindTimer(this, pParentNode);
}
2017-10-19 00:50:04 +08:00
void e2d::ETimer::_callOn()
2017-10-17 21:22:25 +08:00
{
2017-10-21 19:09:31 +08:00
if (m_nRunTimes == m_nRepeatTimes)
{
this->stop();
return;
}
if (m_Callback)
{
m_Callback(m_nRunTimes);
}
2017-10-17 21:22:25 +08:00
m_nRunTimes++;
}
2017-10-21 19:09:31 +08:00
bool e2d::ETimer::_isReady()
{
if (m_bRunning &&
m_pParentNode &&
2018-01-30 16:45:38 +08:00
m_pParentNode->getParentScene() == ESceneManager::getCurrentScene())
{
if (m_bAtOnce && m_nRunTimes == 0)
return true;
2017-10-21 19:09:31 +08:00
2018-01-30 16:45:38 +08:00
if (m_fInterval == 0)
return true;
2017-10-21 19:09:31 +08:00
2018-01-30 16:45:38 +08:00
if ((ETime::getTotalTime() - m_fLast) >= m_fInterval)
{
2018-01-30 16:45:38 +08:00
m_fLast += m_fInterval;
return true;
}
2017-10-21 19:09:31 +08:00
}
return false;
}