refactoring: Task Class

This commit is contained in:
Nomango 2018-09-07 18:20:07 +08:00
parent f417ccc9ef
commit ad5a9ed52f
12 changed files with 226 additions and 268 deletions

View File

@ -305,6 +305,59 @@ namespace e2d
};
// 定时任务
class Task :
public Ref
{
friend class Node;
public:
explicit Task(
const Function& func, /* 执行函数 */
const String& name = L"" /* 任务名称 */
);
explicit Task(
const Function& func, /* 执行函数 */
float delay, /* 时间间隔(秒) */
int times = -1, /* 执行次数(设 -1 为永久执行) */
const String& name = L"" /* 任务名称 */
);
// 启动任务
void Start();
// 停止任务
void Stop();
// 任务是否正在执行
bool IsRunning() const;
// 获取任务名称
const String& GetName() const;
// 任务是否就绪
bool IsReady() const;
// 执行任务
void Update();
// 重置计时
void ResetTime();
protected:
bool running_;
bool stopped_;
int run_times_;
int total_times_;
String name_;
Duration delay_;
Time last_time_;
Function callback_;
Node * target_;
};
// 绘图接口
class Drawable
{
@ -380,6 +433,7 @@ namespace e2d
public:
typedef std::vector<Node*> Nodes;
typedef std::vector<Action*> Actions;
typedef std::vector<Task*> Tasks;
Node();
@ -713,8 +767,40 @@ namespace e2d
// 获取所有动作
const Actions& GetAllActions() const;
// 刷新动作进度
void UpdateActionsTime();
// 添加任务
void AddTask(
Task * task
);
// 启动任务
void StartTasks(
const String& task_name
);
// 停止任务
void StopTasks(
const String& task_name
);
// 移除任务
void RemoveTasks(
const String& task_name
);
// 启动所有任务
void StartAllTasks();
// 停止所有任务
void StopAllTasks();
// 移除所有任务
void RemoveAllTasks();
// 获取所有任务
const Tasks& GetAllTasks() const;
// 更新节点时间
void UpdateTime();
// 分发鼠标消息
virtual bool Dispatch(
@ -757,6 +843,9 @@ namespace e2d
// 更新动作
void UpdateActions();
// 更新任务
void UpdateTasks();
protected:
String name_;
size_t hash_name_;
@ -780,6 +869,7 @@ namespace e2d
Property extrapolate_;
Color border_color_;
Actions actions_;
Tasks tasks_;
Nodes children_;
ID2D1Geometry* border_;
D2D1::Matrix3x2F initial_matrix_;

View File

@ -251,112 +251,6 @@ namespace e2d
};
class Timer;
// 定时任务
class Task :
public Ref
{
friend class Timer;
public:
explicit Task(
const Function& func, /* 执行函数 */
const String& name = L"" /* 任务名称 */
);
explicit Task(
const Function& func, /* 执行函数 */
float delay, /* 时间间隔(秒) */
int times = -1, /* 执行次数(设 -1 为永久执行) */
const String& name = L"" /* 任务名称 */
);
// 启动任务
void Start();
// 停止任务
void Stop();
// 任务是否正在执行
bool IsRunning() const;
// 获取任务名称
const String& GetName() const;
protected:
// 执行任务
void Update();
// 任务是否就绪
bool IsReady() const;
protected:
bool running_;
bool stopped_;
int run_times_;
int total_times_;
String name_;
Duration delay_;
Time last_time_;
Function callback_;
};
// 定时器
class Timer
{
public:
// 获取定时器实例
static Timer * GetInstance();
// 添加任务
void AddTask(
Task * task
);
// 启动任务
void StartTasks(
const String& task_name
);
// 停止任务
void StopTasks(
const String& task_name
);
// 移除任务
void RemoveTasks(
const String& task_name
);
// 启动所有任务
void StartAllTasks();
// 停止所有任务
void StopAllTasks();
// 移除所有任务
void RemoveAllTasks();
// 更新定时器
void Update();
// 刷新所有任务计时
void UpdateTime();
private:
Timer();
~Timer();
E2D_DISABLE_COPY(Timer);
private:
std::vector<Task*> tasks_;
};
// 数据管理工具
class Data
{

View File

@ -2,7 +2,6 @@
#include "..\e2dobject.h"
#include "..\e2dtransition.h"
#include "..\e2dmanager.h"
#include "..\e2dtool.h"
#include <thread>
@ -50,7 +49,6 @@ void e2d::Game::Start()
auto window = Window::GetInstance();
auto input = Input::GetInstance();
auto renderer = Renderer::GetInstance();
auto timer = Timer::GetInstance();
const int minInterval = 5;
Time last = Time::Now();
@ -103,9 +101,10 @@ void e2d::Game::Resume()
{
if (paused_ && !quit_)
{
Timer::GetInstance()->UpdateTime();
if (curr_scene_)
curr_scene_->GetRoot()->UpdateActionsTime();
{
curr_scene_->GetRoot()->UpdateTime();
}
}
paused_ = false;
}

View File

@ -52,6 +52,7 @@ e2d::Node::Node()
, anchor_()
, children_()
, actions_()
, tasks_()
, initial_matrix_(D2D1::Matrix3x2F::Identity())
, final_matrix_(D2D1::Matrix3x2F::Identity())
, border_color_(Color::Red, 0.6f)
@ -82,6 +83,7 @@ void e2d::Node::Visit()
if (!Game::GetInstance()->IsPaused())
{
UpdateActions();
UpdateTasks();
auto updatableNode = dynamic_cast<Updatable*>(this);
if (updatableNode)
@ -978,16 +980,125 @@ const e2d::Node::Actions & e2d::Node::GetAllActions() const
return actions_;
}
void e2d::Node::UpdateActionsTime()
void e2d::Node::AddTask(Task * task)
{
if (task)
{
auto iter = std::find(tasks_.begin(), tasks_.end(), task);
if (iter == tasks_.end())
{
task->Retain();
task->last_time_ = Time::Now();
tasks_.push_back(task);
}
}
}
void e2d::Node::StopTasks(const String& name)
{
for (const auto& task : tasks_)
{
if (task->GetName() == name)
{
task->Stop();
}
}
}
void e2d::Node::StartTasks(const String& name)
{
for (const auto& task : tasks_)
{
if (task->GetName() == name)
{
task->Start();
}
}
}
void e2d::Node::RemoveTasks(const String& name)
{
for (const auto& task : tasks_)
{
if (task->GetName() == name)
{
task->stopped_ = true;
}
}
}
void e2d::Node::StopAllTasks()
{
for (const auto& task : tasks_)
{
task->Stop();
}
}
void e2d::Node::StartAllTasks()
{
for (const auto& task : tasks_)
{
task->Start();
}
}
void e2d::Node::RemoveAllTasks()
{
for (const auto& task : tasks_)
{
task->stopped_ = true;
}
}
void e2d::Node::UpdateTasks()
{
if (tasks_.empty())
return;
std::vector<Task*> currTasks;
currTasks.reserve(tasks_.size());
std::copy_if(
tasks_.begin(),
tasks_.end(),
std::back_inserter(currTasks),
[](Task* task) { return task->IsReady() && !task->stopped_; }
);
// 遍历就绪的任务
for (const auto& task : currTasks)
task->Update();
// 清除结束的任务
for (auto iter = tasks_.begin(); iter != tasks_.end();)
{
if ((*iter)->stopped_)
{
(*iter)->Release();
iter = tasks_.erase(iter);
}
else
{
++iter;
}
}
}
void e2d::Node::UpdateTime()
{
for (const auto& action : actions_)
{
action->ResetTime();
}
for (const auto& task : tasks_)
{
task->ResetTime();
}
for (const auto& child : children_)
{
child->UpdateActionsTime();
child->UpdateTime();
}
}

View File

@ -1,4 +1,4 @@
#include "..\e2dtool.h"
#include "..\e2dobject.h"
e2d::Task::Task(const Function & func, const String & name)
@ -57,6 +57,11 @@ void e2d::Task::Update()
}
}
void e2d::Task::ResetTime()
{
last_time_ = Time::Now();
}
bool e2d::Task::IsReady() const
{
if (running_)

View File

@ -1,129 +0,0 @@
#include "..\e2dtool.h"
e2d::Timer * e2d::Timer::GetInstance()
{
static Timer instance;
return &instance;
}
e2d::Timer::Timer()
: tasks_()
{
}
e2d::Timer::~Timer()
{
}
void e2d::Timer::AddTask(Task * task)
{
if (task)
{
auto iter = std::find(tasks_.begin(), tasks_.end(), task);
if (iter == tasks_.end())
{
task->Retain();
task->last_time_ = Time::Now();
tasks_.push_back(task);
}
}
}
void e2d::Timer::StopTasks(const String& name)
{
for (const auto& task : tasks_)
{
if (task->GetName() == name)
{
task->Stop();
}
}
}
void e2d::Timer::StartTasks(const String& name)
{
for (const auto& task : tasks_)
{
if (task->GetName() == name)
{
task->Start();
}
}
}
void e2d::Timer::RemoveTasks(const String& name)
{
for (const auto& task : tasks_)
{
if (task->GetName() == name)
{
task->stopped_ = true;
}
}
}
void e2d::Timer::StopAllTasks()
{
for (const auto& task : tasks_)
{
task->Stop();
}
}
void e2d::Timer::StartAllTasks()
{
for (const auto& task : tasks_)
{
task->Start();
}
}
void e2d::Timer::RemoveAllTasks()
{
for (const auto& task : tasks_)
{
task->stopped_ = true;
}
}
void e2d::Timer::Update()
{
if (tasks_.empty())
return;
std::vector<Task*> currTasks;
currTasks.reserve(tasks_.size());
std::copy_if(
tasks_.begin(),
tasks_.end(),
std::back_inserter(currTasks),
[](Task* task) { return task->IsReady() && !task->stopped_; }
);
// 遍历就绪的任务
for (const auto& task : currTasks)
task->Update();
// 清除结束的任务
for (auto iter = tasks_.begin(); iter != tasks_.end();)
{
if ((*iter)->stopped_)
{
(*iter)->Release();
iter = tasks_.erase(iter);
}
else
{
++iter;
}
}
}
void e2d::Timer::UpdateTime()
{
for (const auto& task : tasks_)
{
task->last_time_ = Time::Now();
}
}

View File

@ -64,14 +64,13 @@
<ClCompile Include="..\..\core\objects\Scene.cpp" />
<ClCompile Include="..\..\core\objects\Sprite.cpp" />
<ClCompile Include="..\..\core\objects\Text.cpp" />
<ClCompile Include="..\..\core\objects\Task.cpp" />
<ClCompile Include="..\..\core\tools\Data.cpp" />
<ClCompile Include="..\..\core\tools\File.cpp" />
<ClCompile Include="..\..\core\tools\Music.cpp" />
<ClCompile Include="..\..\core\tools\Path.cpp" />
<ClCompile Include="..\..\core\tools\Player.cpp" />
<ClCompile Include="..\..\core\tools\Random.cpp" />
<ClCompile Include="..\..\core\tools\Task.cpp" />
<ClCompile Include="..\..\core\tools\Timer.cpp" />
<ClCompile Include="..\..\core\transitions\BoxTransition.cpp" />
<ClCompile Include="..\..\core\transitions\EmergeTransition.cpp" />
<ClCompile Include="..\..\core\transitions\FadeTransition.cpp" />

View File

@ -195,12 +195,6 @@
<ClCompile Include="..\..\core\tools\Random.cpp">
<Filter>tools</Filter>
</ClCompile>
<ClCompile Include="..\..\core\tools\Task.cpp">
<Filter>tools</Filter>
</ClCompile>
<ClCompile Include="..\..\core\tools\Timer.cpp">
<Filter>tools</Filter>
</ClCompile>
<ClCompile Include="..\..\core\transitions\BoxTransition.cpp">
<Filter>transitions</Filter>
</ClCompile>
@ -237,6 +231,9 @@
<ClCompile Include="..\..\core\objects\Text.cpp">
<Filter>objects</Filter>
</ClCompile>
<ClCompile Include="..\..\core\objects\Task.cpp">
<Filter>objects</Filter>
</ClCompile>
<ClCompile Include="..\..\core\utils\Ref.cpp">
<Filter>utils</Filter>
</ClCompile>

View File

@ -208,14 +208,13 @@
<ClCompile Include="..\..\core\objects\Scene.cpp" />
<ClCompile Include="..\..\core\objects\Sprite.cpp" />
<ClCompile Include="..\..\core\objects\Text.cpp" />
<ClCompile Include="..\..\core\objects\Task.cpp" />
<ClCompile Include="..\..\core\tools\Data.cpp" />
<ClCompile Include="..\..\core\tools\File.cpp" />
<ClCompile Include="..\..\core\tools\Music.cpp" />
<ClCompile Include="..\..\core\tools\Path.cpp" />
<ClCompile Include="..\..\core\tools\Player.cpp" />
<ClCompile Include="..\..\core\tools\Random.cpp" />
<ClCompile Include="..\..\core\tools\Task.cpp" />
<ClCompile Include="..\..\core\tools\Timer.cpp" />
<ClCompile Include="..\..\core\transitions\BoxTransition.cpp" />
<ClCompile Include="..\..\core\transitions\EmergeTransition.cpp" />
<ClCompile Include="..\..\core\transitions\FadeTransition.cpp" />

View File

@ -195,12 +195,6 @@
<ClCompile Include="..\..\core\tools\Random.cpp">
<Filter>tools</Filter>
</ClCompile>
<ClCompile Include="..\..\core\tools\Task.cpp">
<Filter>tools</Filter>
</ClCompile>
<ClCompile Include="..\..\core\tools\Timer.cpp">
<Filter>tools</Filter>
</ClCompile>
<ClCompile Include="..\..\core\transitions\BoxTransition.cpp">
<Filter>transitions</Filter>
</ClCompile>
@ -237,6 +231,9 @@
<ClCompile Include="..\..\core\objects\Text.cpp">
<Filter>objects</Filter>
</ClCompile>
<ClCompile Include="..\..\core\objects\Task.cpp">
<Filter>objects</Filter>
</ClCompile>
<ClCompile Include="..\..\core\utils\Ref.cpp">
<Filter>utils</Filter>
</ClCompile>

View File

@ -241,14 +241,13 @@
<ClCompile Include="..\..\core\objects\Scene.cpp" />
<ClCompile Include="..\..\core\objects\Sprite.cpp" />
<ClCompile Include="..\..\core\objects\Text.cpp" />
<ClCompile Include="..\..\core\objects\Task.cpp" />
<ClCompile Include="..\..\core\tools\Data.cpp" />
<ClCompile Include="..\..\core\tools\File.cpp" />
<ClCompile Include="..\..\core\tools\Music.cpp" />
<ClCompile Include="..\..\core\tools\Path.cpp" />
<ClCompile Include="..\..\core\tools\Player.cpp" />
<ClCompile Include="..\..\core\tools\Random.cpp" />
<ClCompile Include="..\..\core\tools\Task.cpp" />
<ClCompile Include="..\..\core\tools\Timer.cpp" />
<ClCompile Include="..\..\core\transitions\BoxTransition.cpp" />
<ClCompile Include="..\..\core\transitions\EmergeTransition.cpp" />
<ClCompile Include="..\..\core\transitions\FadeTransition.cpp" />

View File

@ -195,12 +195,6 @@
<ClCompile Include="..\..\core\tools\Random.cpp">
<Filter>tools</Filter>
</ClCompile>
<ClCompile Include="..\..\core\tools\Task.cpp">
<Filter>tools</Filter>
</ClCompile>
<ClCompile Include="..\..\core\tools\Timer.cpp">
<Filter>tools</Filter>
</ClCompile>
<ClCompile Include="..\..\core\transitions\BoxTransition.cpp">
<Filter>transitions</Filter>
</ClCompile>
@ -237,6 +231,9 @@
<ClCompile Include="..\..\core\objects\Text.cpp">
<Filter>objects</Filter>
</ClCompile>
<ClCompile Include="..\..\core\objects\Task.cpp">
<Filter>objects</Filter>
</ClCompile>
<ClCompile Include="..\..\core\utils\Ref.cpp">
<Filter>utils</Filter>
</ClCompile>