Parent and Child Deletion Improvements (#649)

* Fix timers

* Update Entity.cpp

* Fix timers

Fix timers

Remove debug logs

remove _dynamic

* I like to move it move it

* Child Deletion Improvements

* Check bounds
This commit is contained in:
David Markowitz 2022-07-16 21:39:13 -07:00 committed by GitHub
parent 77d35019cc
commit c689b3d3d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 4 deletions

View File

@ -109,6 +109,11 @@ Entity::~Entity() {
m_Components.erase(pair.first); m_Components.erase(pair.first);
} }
for (auto child : m_ChildEntities) {
if (child) child->RemoveParent();
}
if (m_ParentEntity) { if (m_ParentEntity) {
m_ParentEntity->RemoveChild(this); m_ParentEntity->RemoveChild(this);
} }
@ -1662,15 +1667,21 @@ void Entity::AddChild(Entity* child) {
void Entity::RemoveChild(Entity* child) { void Entity::RemoveChild(Entity* child) {
if (!child) return; if (!child) return;
for (auto entity = m_ChildEntities.begin(); entity != m_ChildEntities.end(); entity++) { uint32_t entityPosition = 0;
if (*entity && (*entity)->GetObjectID() == child->GetObjectID()) { while (entityPosition < m_ChildEntities.size()) {
if (!m_ChildEntities[entityPosition] || (m_ChildEntities[entityPosition])->GetObjectID() == child->GetObjectID()) {
m_IsParentChildDirty = true; m_IsParentChildDirty = true;
m_ChildEntities.erase(entity); m_ChildEntities.erase(m_ChildEntities.begin() + entityPosition);
return; } else {
entityPosition++;
} }
} }
} }
void Entity::RemoveParent() {
this->m_ParentEntity = nullptr;
}
void Entity::AddTimer(std::string name, float time) { void Entity::AddTimer(std::string name, float time) {
EntityTimer* timer = new EntityTimer(name, time); EntityTimer* timer = new EntityTimer(name, time);
m_PendingTimers.push_back(timer); m_PendingTimers.push_back(timer);

View File

@ -144,6 +144,7 @@ public:
void AddChild(Entity* child); void AddChild(Entity* child);
void RemoveChild(Entity* child); void RemoveChild(Entity* child);
void RemoveParent();
void AddTimer(std::string name, float time); void AddTimer(std::string name, float time);
void AddCallbackTimer(float time, std::function<void()> callback); void AddCallbackTimer(float time, std::function<void()> callback);
bool HasTimer(const std::string& name); bool HasTimer(const std::string& name);