代码优化
This commit is contained in:
parent
fe52ebf451
commit
00fb2b89f4
|
|
@ -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<Action*> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<Collider*> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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<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 (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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue