Magic_Game/core/Tool/Task.cpp

85 lines
1.1 KiB
C++
Raw Normal View History

#include "..\e2dtool.h"
e2d::Task::Task(const Function & func, const String & name)
: _running(true)
, _stopped(false)
, _runTimes(0)
, _totalTimes(-1)
2018-08-02 00:27:45 +08:00
, _delay()
, _callback(func)
, _name(name)
{
}
2018-07-28 20:06:27 +08:00
e2d::Task::Task(const Function & func, float delay, int times, const String & name)
: _running(true)
, _stopped(false)
, _runTimes(0)
2018-07-28 20:06:27 +08:00
, _delay(std::max(delay, 0.f))
2018-08-02 00:27:45 +08:00
, _totalTimes(times)
, _callback(func)
, _name(name)
{
}
2018-08-12 13:24:31 +08:00
void e2d::Task::start()
{
2018-08-12 13:24:31 +08:00
_running = true;
_lastTime = Time::now();
}
2018-08-12 13:24:31 +08:00
void e2d::Task::stop()
{
2018-08-12 13:24:31 +08:00
_running = false;
}
2018-08-12 13:24:31 +08:00
void e2d::Task::_update()
{
2018-08-02 00:27:45 +08:00
if (_totalTimes == 0)
{
2018-08-02 00:27:45 +08:00
_stopped = true;
return;
}
++_runTimes;
_lastTime += _delay;
2018-08-02 00:27:45 +08:00
if (_callback)
{
_callback();
}
if (_runTimes == _totalTimes)
{
_stopped = true;
2018-08-02 00:27:45 +08:00
return;
}
}
2018-08-12 13:24:31 +08:00
bool e2d::Task::_isReady() const
{
if (_running)
{
2018-08-02 00:27:45 +08:00
if (_delay.milliseconds() == 0)
{
return true;
}
2018-08-02 00:27:45 +08:00
if (Time::now() - _lastTime >= _delay)
{
return true;
}
}
return false;
}
bool e2d::Task::isRunning() const
{
return _running;
}
e2d::String e2d::Task::name() const
{
return _name;
}