代码优化

This commit is contained in:
Nomango 2018-08-12 15:38:02 +08:00
parent fe52ebf451
commit 00fb2b89f4
5 changed files with 86 additions and 75 deletions

View File

@ -36,24 +36,30 @@ void e2d::ActionManager::update()
if (_runningActions.empty() || Game::getInstance()->isPaused()) if (_runningActions.empty() || Game::getInstance()->isPaused())
return; return;
// 循环遍历所有正在运行的动作 std::vector<Action*> currActions;
for (size_t i = 0; i < _runningActions.size(); ++i) std::copy_if(
_runningActions.begin(),
_runningActions.end(),
std::back_inserter(currActions),
[](Action* action) { return action->isRunning() && !action->_isDone(); }
);
// 遍历所有正在运行的动作
for (const auto& action : currActions)
action->_update();
// 清除完成的动作
for (auto iter = _runningActions.begin(); iter != _runningActions.end();)
{ {
auto action = _runningActions[i]; if ((*iter)->_isDone())
// 获取动作运行状态
if (action->_isDone())
{ {
action->release(); (*iter)->release();
action->_target = nullptr; (*iter)->_target = nullptr;
_runningActions.erase(_runningActions.begin() + i); iter = _runningActions.erase(iter);
} }
else else
{ {
if (action->isRunning()) ++iter;
{
// 执行动作
action->_update();
}
} }
} }
} }
@ -195,18 +201,16 @@ void e2d::ActionManager::clearAllBindedWith(Node * target)
{ {
if (target) if (target)
{ {
for (size_t i = 0; i < _runningActions.size();) auto iter = std::find_if(
_runningActions.begin(),
_runningActions.end(),
[target](Action* action) ->bool { return action->getTarget() == target; }
);
if (iter != _runningActions.end())
{ {
auto action = _runningActions[i]; (*iter)->release();
if (action->getTarget() == target) _runningActions.erase(iter);
{
action->release();
_runningActions.erase(_runningActions.begin() + i);
}
else
{
++i;
}
} }
} }
} }

View File

@ -50,34 +50,38 @@ void e2d::CollisionManager::__updateCollider(Collider* collider)
SceneManager::getInstance()->isTransitioning()) SceneManager::getInstance()->isTransitioning())
return; return;
for (size_t i = 0; i < _colliders.size(); i++) std::vector<Collider*> currColliders;
{ std::copy_if(
// 判断与其他碰撞体的交集情况 _colliders.begin(),
auto active = collider->getNode(); _colliders.end(),
auto passive = _colliders[i]->getNode(); std::back_inserter(currColliders),
// 判断两物体是否是相互冲突的物体 [this, collider](Collider* passive) -> bool
if (active == passive ||
!passive->isVisible() ||
active->getParentScene() != passive->getParentScene() ||
!CollisionManager::isCollidable(active, passive))
{ {
continue; return collider != passive &&
passive->getNode()->isVisible() &&
collider->getNode()->getParentScene() == passive->getNode()->getParentScene() &&
this->isCollidable(collider->getNode(), passive->getNode());
} }
);
for (const auto& passive : currColliders)
{
// 判断两碰撞体交集情况 // 判断两碰撞体交集情况
Collider::Relation relation = active->getCollider()->getRelationWith(passive->getCollider()); Collider::Relation relation = collider->getRelationWith(passive);
// 忽略 UNKNOWN 和 DISJOIN 情况 // 忽略 UNKNOWN 和 DISJOIN 情况
if (relation != Collider::Relation::Unknown && if (relation != Collider::Relation::Unknown &&
relation != Collider::Relation::Disjoin) relation != Collider::Relation::Disjoin)
{ {
auto activeNode = collider->getNode();
auto passiveNode = passive->getNode();
// 触发两次碰撞事件 // 触发两次碰撞事件
Collision activeCollision(passive, relation); Collision activeCollision(passiveNode, relation);
active->getParentScene()->onCollision(activeCollision); activeNode->getParentScene()->onCollision(activeCollision);
active->onCollision(activeCollision); activeNode->onCollision(activeCollision);
Collision passiveCollision(active, passive->getCollider()->getRelationWith(active->getCollider())); Collision passiveCollision(activeNode, passive->getRelationWith(collider));
passive->getParentScene()->onCollision(passiveCollision); passiveNode->getParentScene()->onCollision(passiveCollision);
passive->onCollision(passiveCollision); passiveNode->onCollision(passiveCollision);
} }
} }
} }

View File

@ -58,16 +58,13 @@ bool e2d::Menu::removeButton(Button * button)
if (button) if (button)
{ {
size_t size = _buttons.size(); auto iter = std::find(_buttons.begin(), _buttons.end(), button);
for (size_t i = 0; i < size; ++i) if (iter != _buttons.end())
{ {
if (_buttons[i] == button) // 移除按钮前,将它启用
{ button->setEnabled(true);
// 移除按钮前,将它启用 _buttons.erase(iter);
button->setEnabled(true); return true;
_buttons.erase(_buttons.begin() + i);
return true;
}
} }
} }
return false; return false;

View File

@ -797,20 +797,21 @@ void e2d::Node::removeChildren(const String& childName)
// ¼ÆËãÃû³Æ Hash Öµ // ¼ÆËãÃû³Æ Hash Öµ
size_t hash = childName.getHashCode(); size_t hash = childName.getHashCode();
size_t size = _children.size(); auto iter = std::find_if(
for (size_t i = 0; i < size; ++i) _children.begin(),
_children.end(),
[childName, hash](Node* child) ->bool { return child->_hashName == hash && child->_name == childName; }
);
if (iter != _children.end())
{ {
auto child = _children[i]; (*iter)->_parent = nullptr;
if (child->_hashName == hash && child->_name == childName) if ((*iter)->_parentScene)
{ {
_children.erase(_children.begin() + i); (*iter)->_setParentScene(nullptr);
child->_parent = nullptr;
if (child->_parentScene)
{
child->_setParentScene(nullptr);
}
child->release();
} }
(*iter)->release();
_children.erase(iter);
} }
} }

View File

@ -115,24 +115,29 @@ void e2d::Timer::update()
if (_tasks.empty() || Game::getInstance()->isPaused()) if (_tasks.empty() || Game::getInstance()->isPaused())
return; return;
for (size_t i = 0; i < _tasks.size();) std::vector<Task*> currTasks;
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();)
{ {
auto task = _tasks[i]; if ((*iter)->_stopped)
// 清除已停止的任务
if (task->_stopped)
{ {
task->release(); (*iter)->release();
_tasks.erase(_tasks.begin() + i); iter = _tasks.erase(iter);
} }
else else
{ {
++i; ++iter;
// 更新定时器
if (task->_isReady())
{
task->_update();
}
} }
} }
} }