From 070cd89d2ed140594608d252318f444202579a52 Mon Sep 17 00:00:00 2001 From: Zack Mulgrew Date: Wed, 3 Feb 2016 23:49:41 -0800 Subject: Trigger ComponentRemovedEvent for each component before destroying --- entityx/Entity.h | 6 ++++-- entityx/EntityClass.h | 1 + entityx/help/Pool.h | 7 +++++++ entityx/help/Pool_test.cc | 3 ++- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/entityx/Entity.h b/entityx/Entity.h index 8e1e93e..2ef01f7 100644 --- a/entityx/Entity.h +++ b/entityx/Entity.h @@ -381,12 +381,14 @@ class EntityManager : entityx::help::NonCopyable { assert_valid(entity); uint32_t index = entity.index(); auto mask = entity_component_mask_[entity.index()]; - event_manager_.emit(Entity(this, entity)); for (size_t i = 0; i < component_pools_.size(); i++) { BasePool *pool = component_pools_[i]; - if (pool && mask.test(i)) + if (pool && mask.test(i)) { + pool->removeComponent(Entity(this, entity)); pool->destroy(index); + } } + event_manager_.emit(Entity(this, entity)); entity_component_mask_[index].reset(); entity_version_[index]++; free_list_.push_back(index); diff --git a/entityx/EntityClass.h b/entityx/EntityClass.h index 444e494..94a2a02 100644 --- a/entityx/EntityClass.h +++ b/entityx/EntityClass.h @@ -10,6 +10,7 @@ #pragma once +#include #include "entityx/config.h" namespace entityx { diff --git a/entityx/help/Pool.h b/entityx/help/Pool.h index f217ec2..c4b7d83 100644 --- a/entityx/help/Pool.h +++ b/entityx/help/Pool.h @@ -14,6 +14,8 @@ #include #include +#include "entityx/EntityClass.h" + namespace entityx { /** @@ -63,6 +65,7 @@ class BasePool { } virtual void destroy(std::size_t n) = 0; + virtual void removeComponent(Entity entity) = 0; protected: std::vector blocks_; @@ -90,6 +93,10 @@ class Pool : public BasePool { T *ptr = static_cast(get(n)); ptr->~T(); } + + virtual void removeComponent(Entity entity) override { + entity.remove(); + } }; } // namespace entityx diff --git a/entityx/help/Pool_test.cc b/entityx/help/Pool_test.cc index 56ca85a..08ef1d2 100644 --- a/entityx/help/Pool_test.cc +++ b/entityx/help/Pool_test.cc @@ -13,8 +13,9 @@ #include #include "entityx/3rdparty/catch.hpp" #include "entityx/help/Pool.h" +#include "entityx/Entity.h" -struct Position { +struct Position : public entityx::Component { explicit Position(int *ptr = nullptr) : ptr(ptr) { if (ptr) (*ptr)++; } -- cgit v1.2.3