diff --git a/core/Manager/ActionManager.cpp b/core/Manager/ActionManager.cpp index 76b68a4d..e23d109c 100644 --- a/core/Manager/ActionManager.cpp +++ b/core/Manager/ActionManager.cpp @@ -36,24 +36,30 @@ void e2d::ActionManager::update() if (_runningActions.empty() || Game::getInstance()->isPaused()) return; - // 循环遍历所有正在运行的动作 - for (size_t i = 0; i < _runningActions.size(); ++i) + std::vector currActions; + 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 (action->_isDone()) + if ((*iter)->_isDone()) { - action->release(); - action->_target = nullptr; - _runningActions.erase(_runningActions.begin() + i); + (*iter)->release(); + (*iter)->_target = nullptr; + iter = _runningActions.erase(iter); } else { - if (action->isRunning()) - { - // 执行动作 - action->_update(); - } + ++iter; } } } @@ -195,18 +201,16 @@ void e2d::ActionManager::clearAllBindedWith(Node * 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]; - if (action->getTarget() == target) - { - action->release(); - _runningActions.erase(_runningActions.begin() + i); - } - else - { - ++i; - } + (*iter)->release(); + _runningActions.erase(iter); } } } diff --git a/core/Manager/CollisionManager.cpp b/core/Manager/CollisionManager.cpp index 34f40b0c..f6b06c90 100644 --- a/core/Manager/CollisionManager.cpp +++ b/core/Manager/CollisionManager.cpp @@ -50,34 +50,38 @@ void e2d::CollisionManager::__updateCollider(Collider* collider) SceneManager::getInstance()->isTransitioning()) return; - for (size_t i = 0; i < _colliders.size(); i++) - { - // 判断与其他碰撞体的交集情况 - auto active = collider->getNode(); - auto passive = _colliders[i]->getNode(); - // 判断两物体是否是相互冲突的物体 - if (active == passive || - !passive->isVisible() || - active->getParentScene() != passive->getParentScene() || - !CollisionManager::isCollidable(active, passive)) + std::vector currColliders; + std::copy_if( + _colliders.begin(), + _colliders.end(), + std::back_inserter(currColliders), + [this, collider](Collider* passive) -> bool { - 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 情况 if (relation != Collider::Relation::Unknown && relation != Collider::Relation::Disjoin) { + auto activeNode = collider->getNode(); + auto passiveNode = passive->getNode(); // 触发两次碰撞事件 - Collision activeCollision(passive, relation); - active->getParentScene()->onCollision(activeCollision); - active->onCollision(activeCollision); + Collision activeCollision(passiveNode, relation); + activeNode->getParentScene()->onCollision(activeCollision); + activeNode->onCollision(activeCollision); - Collision passiveCollision(active, passive->getCollider()->getRelationWith(active->getCollider())); - passive->getParentScene()->onCollision(passiveCollision); - passive->onCollision(passiveCollision); + Collision passiveCollision(activeNode, passive->getRelationWith(collider)); + passiveNode->getParentScene()->onCollision(passiveCollision); + passiveNode->onCollision(passiveCollision); } } } diff --git a/core/Node/Menu.cpp b/core/Node/Menu.cpp index a0ee2780..68e3a17a 100644 --- a/core/Node/Menu.cpp +++ b/core/Node/Menu.cpp @@ -58,16 +58,13 @@ bool e2d::Menu::removeButton(Button * button) if (button) { - size_t size = _buttons.size(); - for (size_t i = 0; i < size; ++i) + auto iter = std::find(_buttons.begin(), _buttons.end(), button); + if (iter != _buttons.end()) { - if (_buttons[i] == button) - { - // 移除按钮前,将它启用 - button->setEnabled(true); - _buttons.erase(_buttons.begin() + i); - return true; - } + // 移除按钮前,将它启用 + button->setEnabled(true); + _buttons.erase(iter); + return true; } } return false; diff --git a/core/Node/Node.cpp b/core/Node/Node.cpp index 302dd4f3..efb98d96 100644 --- a/core/Node/Node.cpp +++ b/core/Node/Node.cpp @@ -797,20 +797,21 @@ void e2d::Node::removeChildren(const String& childName) // 计算名称 Hash 值 size_t hash = childName.getHashCode(); - size_t size = _children.size(); - for (size_t i = 0; i < size; ++i) + auto iter = std::find_if( + _children.begin(), + _children.end(), + [childName, hash](Node* child) ->bool { return child->_hashName == hash && child->_name == childName; } + ); + + if (iter != _children.end()) { - auto child = _children[i]; - if (child->_hashName == hash && child->_name == childName) + (*iter)->_parent = nullptr; + if ((*iter)->_parentScene) { - _children.erase(_children.begin() + i); - child->_parent = nullptr; - if (child->_parentScene) - { - child->_setParentScene(nullptr); - } - child->release(); + (*iter)->_setParentScene(nullptr); } + (*iter)->release(); + _children.erase(iter); } } diff --git a/core/Tool/Timer.cpp b/core/Tool/Timer.cpp index f9c06a41..57a04a84 100644 --- a/core/Tool/Timer.cpp +++ b/core/Tool/Timer.cpp @@ -115,24 +115,29 @@ void e2d::Timer::update() if (_tasks.empty() || Game::getInstance()->isPaused()) return; - for (size_t i = 0; i < _tasks.size();) + std::vector 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 (task->_stopped) + if ((*iter)->_stopped) { - task->release(); - _tasks.erase(_tasks.begin() + i); + (*iter)->release(); + iter = _tasks.erase(iter); } else { - ++i; - - // 更新定时器 - if (task->_isReady()) - { - task->_update(); - } + ++iter; } } }