template <typename C, typename ... Args>
entityx::shared_ptr<C> assign(Args && ... args);
+ template <typename C>
+ entityx::shared_ptr<C> remove();
+
template <typename C>
entityx::shared_ptr<C> component();
entityx::shared_ptr<T> component;
};
+/**
+ * Emitted when any component is removed from an entity.
+ */
+template <typename T>
+struct ComponentRemovedEvent : public Event<ComponentRemovedEvent<T>> {
+ ComponentRemovedEvent(Entity entity, entityx::shared_ptr<T> component) :
+ entity(entity), component(component) {}
+
+ Entity entity;
+ entityx::shared_ptr<T> component;
+};
/**
* Manages Entity::Id creation and component assignment.
return assign<C>(entity, entityx::make_shared<C>(args ...));
}
+ /**
+ * Remove a Component from an Entity::Id
+ *
+ * Emits a ComponentRemovedEvent<C> event.
+ */
+ template <typename C>
+ entityx::shared_ptr<C> remove(Entity::Id id) {
+ entityx::shared_ptr<C> component = entityx::static_pointer_cast<C>(entity_components_.at(C::family()).at(id.index()));
+ entity_components_.at(C::family()).at(id.index()).reset();
+ entity_component_mask_.at(id.index()) &= ~(uint64_t(1) << C::family());
+ if (component)
+ event_manager_->emit<ComponentRemovedEvent<C>>(Entity(shared_from_this(), id), component);
+ return component;
+ }
+
/**
* Retrieve a Component assigned to an Entity::Id.
*
return manager_.lock()->assign<C>(id_, args ...);
}
+template <typename C>
+entityx::shared_ptr<C> Entity::remove(){
+ assert(valid() && component<C>());
+ return manager_.lock()->remove<C>(id_);
+}
+
template <typename C>
entityx::shared_ptr<C> Entity::component() {
assert(valid());
ASSERT_EQ(10, receiver.direction_events);
}
+TEST_F(EntityManagerTest, TestComponentRemovedEvent) {
+ struct ComponentRemovedReceiver : public Receiver<ComponentRemovedReceiver> {
+ void receive(const ComponentRemovedEvent<Direction> &event) {
+ removed = event.component;
+ }
+
+ entityx::shared_ptr<Direction> removed;
+ };
+
+ ComponentRemovedReceiver receiver;
+ ev->subscribe<ComponentRemovedEvent<Direction>>(receiver);
+
+ ASSERT_FALSE(receiver.removed);
+ Entity e = em->create();
+ e.assign<Direction>(1.0, 2.0);
+ auto p = e.remove<Direction>();
+ ASSERT_EQ(receiver.removed, p);
+ ASSERT_FALSE(e.component<Direction>());
+}
+
TEST_F(EntityManagerTest, TestEntityAssignment) {
Entity a, b;
a = em->create();