From 31b86f3886e4b8991861b007df091faafc8be782 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Sun, 18 Dec 2016 20:41:26 +0000 Subject: test --- src/Entities/Entity.cpp | 51 ++++++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 20 deletions(-) (limited to 'src/Entities') diff --git a/src/Entities/Entity.cpp b/src/Entities/Entity.cpp index 8d74ee99a..489b55cf8 100644 --- a/src/Entities/Entity.cpp +++ b/src/Entities/Entity.cpp @@ -132,7 +132,9 @@ const char * cEntity::GetParentClass(void) const bool cEntity::Initialize(cWorld & a_World) { - if (cPluginManager::Get()->CallHookSpawningEntity(a_World, *this)) + ASSERT(a_EntityWorld.IsInTickThread()); + + if (cPluginManager::Get()->CallHookSpawningEntity(a_EntityWorld, *this) && !IsPlayer()) { return false; } @@ -144,15 +146,22 @@ bool cEntity::Initialize(cWorld & a_World) ); */ - ASSERT(m_World == nullptr); ASSERT(GetParentChunk() == nullptr); - a_World.AddEntity(this); - ASSERT(m_World != nullptr); + cpp14::move_on_copy_wrapper Entity(std::move(a_Entity)); + + // So that entities do not appear in the middle of ticks + a_EntityWorld.QueueTask( + [Entity, &a_EntityWorld](cWorld & a_World) + { + auto & EntityPtr = *Entity.value; - cPluginManager::Get()->CallHookSpawnedEntity(a_World, *this); + EntityPtr.SetWorld(&a_EntityWorld); + a_EntityWorld.AddEntity(std::move(Entity.value)); - // Spawn the entity on the clients: - a_World.BroadcastSpawnEntity(*this); + a_EntityWorld.BroadcastSpawnEntity(EntityPtr); + cPluginManager::Get()->CallHookSpawnedEntity(a_EntityWorld, EntityPtr); + } + ); return true; } @@ -209,8 +218,7 @@ cChunk * cEntity::GetParentChunk() const void cEntity::Destroy(bool a_ShouldBroadcast) { - ASSERT(IsTicking()); - ASSERT(GetParentChunk() != nullptr); + ASSERT(GetWorld()->IsInTickThread()); SetIsTicking(false); if (a_ShouldBroadcast) @@ -218,17 +226,20 @@ void cEntity::Destroy(bool a_ShouldBroadcast) m_World->BroadcastDestroyEntity(*this); } - cChunk * ParentChunk = GetParentChunk(); - m_World->QueueTask([this, ParentChunk](cWorld & a_World) - { - LOGD("Destroying entity #%i (%s) from chunk (%d, %d)", - this->GetUniqueID(), this->GetClass(), - ParentChunk->GetPosX(), ParentChunk->GetPosZ() - ); - ParentChunk->RemoveEntity(this); - delete this; - }); - Destroyed(); + // So that entities do not disappear unexpectedly during ticks + m_World->QueueTask( + [this](cWorld & a_World) + { + auto ParentChunk = GetParentChunk(); + LOGD("Destroying entity #%i (%s) from chunk (%d, %d)", + GetUniqueID(), GetClass(), + ParentChunk->GetPosX(), ParentChunk->GetPosZ() + ); + + Destroyed(); // TODO: rename to OnPreDestroy() + ParentChunk->RemoveEntity(*this); + } + ); } -- cgit v1.2.3