]> code.bitgloo.com Git - clyne/entityx.git/commitdiff
Trigger ComponentRemovedEvent for each component before destroying
authorZack Mulgrew <zack@zackthehuman.com>
Thu, 4 Feb 2016 07:49:41 +0000 (23:49 -0800)
committerZack Mulgrew <zack@zackthehuman.com>
Thu, 4 Feb 2016 07:49:41 +0000 (23:49 -0800)
entityx/Entity.h
entityx/EntityClass.h
entityx/help/Pool.h
entityx/help/Pool_test.cc

index 8e1e93e60e152f97dd687c62beaa6fae21d1130f..2ef01f77d01fb38cd27028e9719aa3374327f04e 100644 (file)
@@ -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<EntityDestroyedEvent>(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<EntityDestroyedEvent>(Entity(this, entity));
     entity_component_mask_[index].reset();
     entity_version_[index]++;
     free_list_.push_back(index);
index 444e4948b7e4de1cbb80b568bb61c4ca4159d601..94a2a02ee024574529d6e7aab2667620da1959f7 100644 (file)
@@ -10,6 +10,7 @@
 
 #pragma once
 
+#include <bitset>
 #include "entityx/config.h"
 
 namespace entityx {
index f217ec25b0bb3f1fbd4af7e4ff21482f674b7561..c4b7d83d1c3c86e95c92de570521763bda208540 100644 (file)
@@ -14,6 +14,8 @@
 #include <cassert>
 #include <vector>
 
+#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<char *> blocks_;
@@ -90,6 +93,10 @@ class Pool : public BasePool {
     T *ptr = static_cast<T*>(get(n));
     ptr->~T();
   }
+
+  virtual void removeComponent(Entity entity) override {
+    entity.remove<T>();
+  }
 };
 
 }  // namespace entityx
index 56ca85af346e15829e13a030d1fa0d8382e28957..08ef1d2c3b54ea7c9a1facf34aebe7b61989b4a9 100644 (file)
@@ -13,8 +13,9 @@
 #include <vector>
 #include "entityx/3rdparty/catch.hpp"
 #include "entityx/help/Pool.h"
+#include "entityx/Entity.h"
 
-struct Position {
+struct Position : public entityx::Component<Position> {
   explicit Position(int *ptr = nullptr) : ptr(ptr) {
     if (ptr) (*ptr)++;
   }