From a99d4ca8bc1079ed2e7bc3d1e8f4061fcb96b88d Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Mon, 9 Feb 2015 11:30:14 +1100 Subject: Components no longer need to be inherited from Component<>. See #85. --- README.md | 12 ++++++------ entityx/Entity.h | 26 +++++++++++++------------- entityx/Entity_test.cc | 32 ++++++++++++++++---------------- 3 files changed, 35 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 174a673..bfbdfc1 100644 --- a/README.md +++ b/README.md @@ -143,8 +143,8 @@ entity.assign(1.0f, 2.0f); To query all entities with a set of components assigned, use ``entityx::EntityManager::entities_with_components()``. This method will return only those entities that have *all* of the specified components associated with them, assigning each component pointer to the corresponding component instance: ```c++ -Position::Handle position; -Direction::Handle direction; +ComponentHandle position; +ComponentHandle direction; for (Entity entity : entities.entities_with_components(position, direction)) { // Do things with entity, position and direction. } @@ -153,7 +153,7 @@ for (Entity entity : entities.entities_with_components(position, direction)) { To retrieve a component associated with an entity use ``entityx::Entity::component()``: ```c++ -Position::Handle position = entity.component(); +ComponentHandle position = entity.component(); if (position) { // Do stuff with position } @@ -186,8 +186,8 @@ A basic movement system might be implemented with something like the following: ```c++ struct MovementSystem : public System { void update(entityx::EntityManager &es, entityx::EventManager &events, TimeDelta dt) override { - Position::Handle position; - Direction::Handle direction; + ComponentHandle position; + ComponentHandle direction; for (Entity entity : es.entities_with_components(position, direction)) { position->x += direction->x * dt; position->y += direction->y * dt; @@ -223,7 +223,7 @@ Next we implement our collision system, which emits ``Collision`` objects via an class CollisionSystem : public System { public: void update(entityx::EntityManager &es, entityx::EventManager &events, TimeDelta dt) override { - Position::Handle left_position, right_position; + ComponentHandle left_position, right_position; for (Entity left_entity : es.entities_with_components(left_position)) { for (Entity right_entity : es.entities_with_components(right_position)) { if (collide(left_position, right_position)) { diff --git a/entityx/Entity.h b/entityx/Entity.h index 85033c9..af2fafb 100644 --- a/entityx/Entity.h +++ b/entityx/Entity.h @@ -577,7 +577,7 @@ class EntityManager : entityx::help::NonCopyable { template ComponentHandle assign(Entity::Id id, Args && ... args) { assert_valid(id); - const BaseComponent::Family family = C::family(); + const BaseComponent::Family family = Component::family(); assert(!entity_component_mask_[id.index()].test(family)); // Placement new into the component pool. @@ -601,7 +601,7 @@ class EntityManager : entityx::help::NonCopyable { template void remove(Entity::Id id) { assert_valid(id); - const BaseComponent::Family family = C::family(); + const BaseComponent::Family family = Component::family(); const uint32_t index = id.index(); // Find the pool for this component family. @@ -622,7 +622,7 @@ class EntityManager : entityx::help::NonCopyable { template bool has_component(Entity::Id id) const { assert_valid(id); - size_t family = C::family(); + size_t family = Component::family(); // We don't bother checking the component mask, as we return a nullptr anyway. if (family >= component_pools_.size()) return false; @@ -640,7 +640,7 @@ class EntityManager : entityx::help::NonCopyable { template ComponentHandle component(Entity::Id id) { assert_valid(id); - size_t family = C::family(); + size_t family = Component::family(); // We don't bother checking the component mask, as we return a nullptr anyway. if (family >= component_pools_.size()) return ComponentHandle(); @@ -658,7 +658,7 @@ class EntityManager : entityx::help::NonCopyable { template const ComponentHandle component(Entity::Id id) const { assert_valid(id); - size_t family = C::family(); + size_t family = Component::family(); // We don't bother checking the component mask, as we return a nullptr anyway. if (family >= component_pools_.size()) return ComponentHandle(); @@ -769,7 +769,7 @@ class EntityManager : entityx::help::NonCopyable { template C *get_component_ptr(Entity::Id id) { assert(valid(id)); - BasePool *pool = component_pools_[C::family()]; + BasePool *pool = component_pools_[Component::family()]; assert(pool); return static_cast(pool->get(id.index())); } @@ -777,7 +777,7 @@ class EntityManager : entityx::help::NonCopyable { template const C *get_component_ptr(Entity::Id id) const { assert_valid(id); - BasePool *pool = component_pools_[C::family()]; + BasePool *pool = component_pools_[Component::family()]; assert(pool); return static_cast(pool->get(id.index())); } @@ -790,7 +790,7 @@ class EntityManager : entityx::help::NonCopyable { template ComponentMask component_mask() { ComponentMask mask; - mask.set(C::family()); + mask.set(Component::family()); return mask; } @@ -818,18 +818,18 @@ class EntityManager : entityx::help::NonCopyable { } } - template - Pool *accomodate_component() { - BaseComponent::Family family = T::family(); + template + Pool *accomodate_component() { + BaseComponent::Family family = Component::family(); if (component_pools_.size() <= family) { component_pools_.resize(family + 1, nullptr); } if (!component_pools_[family]) { - Pool *pool = new Pool(); + Pool *pool = new Pool(); pool->expand(index_counter_); component_pools_[family] = pool; } - return static_cast*>(component_pools_[family]); + return static_cast*>(component_pools_[family]); } diff --git a/entityx/Entity_test.cc b/entityx/Entity_test.cc index 9872116..6d0dfa1 100644 --- a/entityx/Entity_test.cc +++ b/entityx/Entity_test.cc @@ -40,7 +40,7 @@ int size(const T &t) { return n; } -struct Position : Component { +struct Position { Position(float x = 0.0f, float y = 0.0f) : x(x), y(y) {} bool operator==(const Position &other) const { @@ -55,7 +55,7 @@ ostream &operator<<(ostream &out, const Position &position) { return out; } -struct Direction : Component { +struct Direction { Direction(float x = 0.0f, float y = 0.0f) : x(x), y(y) {} bool operator==(const Direction &other) const { @@ -202,7 +202,7 @@ TEST_CASE_METHOD(EntityManagerFixture, "TestGetEntitiesWithComponentAndUnpacking Entity e = em.create(); Entity f = em.create(); Entity g = em.create(); - std::vector> position_directions; + std::vector, ComponentHandle>> position_directions; position_directions.push_back(std::make_pair( e.assign(1.0f, 2.0f), e.assign(3.0f, 4.0f))); position_directions.push_back(std::make_pair( @@ -211,10 +211,10 @@ TEST_CASE_METHOD(EntityManagerFixture, "TestGetEntitiesWithComponentAndUnpacking g.assign(5.0f, 6.0f); int i = 0; - Position::Handle position; + ComponentHandle position; REQUIRE(3 == size(em.entities_with_components(position))); - Direction::Handle direction; + ComponentHandle direction; for (auto unused_entity : em.entities_with_components(position, direction)) { (void)unused_entity; REQUIRE(position); @@ -225,7 +225,7 @@ TEST_CASE_METHOD(EntityManagerFixture, "TestGetEntitiesWithComponentAndUnpacking ++i; } REQUIRE(2 == i); - Tag::Handle tag; + ComponentHandle tag; i = 0; for (auto unused_entity : em.entities_with_components(position, direction, tag)) { @@ -261,9 +261,9 @@ TEST_CASE_METHOD(EntityManagerFixture, "TestUnpack") { auto d = e.assign(3.0, 4.0); auto t = e.assign("tag"); - Position::Handle up; - Direction::Handle ud; - Tag::Handle ut; + ComponentHandle up; + ComponentHandle ud; + ComponentHandle ut; e.unpack(up); REQUIRE(p == up); e.unpack(up, ud); @@ -290,7 +290,7 @@ TEST_CASE_METHOD(EntityManagerFixture, "TestUnpack") { // } TEST_CASE_METHOD(EntityManagerFixture, "TestComponentIdsDiffer") { - REQUIRE(Position::family() != Direction::family()); + REQUIRE(Component::family() != Component::family()); } TEST_CASE_METHOD(EntityManagerFixture, "TestEntityCreatedEvent") { @@ -391,7 +391,7 @@ TEST_CASE_METHOD(EntityManagerFixture, "TestComponentRemovedEvent") { removed = event.component; } - Direction::Handle removed; + ComponentHandle removed; }; ComponentRemovedReceiver receiver; @@ -399,7 +399,7 @@ TEST_CASE_METHOD(EntityManagerFixture, "TestComponentRemovedEvent") { REQUIRE(!(receiver.removed)); Entity e = em.create(); - Direction::Handle p = e.assign(1.0, 2.0); + ComponentHandle p = e.assign(1.0, 2.0); e.remove(); REQUIRE(receiver.removed == p); REQUIRE(!(e.component())); @@ -453,7 +453,7 @@ TEST_CASE_METHOD(EntityManagerFixture, "TestEntityDestroyHole") { TEST_CASE_METHOD(EntityManagerFixture, "TestComponentHandleInvalidatedWhenEntityDestroyed") { Entity a = em.create(); - Position::Handle position = a.assign(1, 2); + ComponentHandle position = a.assign(1, 2); REQUIRE(position); REQUIRE(position->x == 1); REQUIRE(position->y == 2); @@ -473,7 +473,7 @@ struct CopyVerifier : Component { TEST_CASE_METHOD(EntityManagerFixture, "TestComponentAssignmentFromCopy") { Entity a = em.create(); CopyVerifier original; - CopyVerifier::Handle copy = a.assign_from_copy(original); + ComponentHandle copy = a.assign_from_copy(original); REQUIRE(copy); REQUIRE(copy->copied == 1); a.destroy(); @@ -482,7 +482,7 @@ TEST_CASE_METHOD(EntityManagerFixture, "TestComponentAssignmentFromCopy") { TEST_CASE_METHOD(EntityManagerFixture, "TestComponentHandleInvalidatedWhenComponentDestroyed") { Entity a = em.create(); - Position::Handle position = a.assign(1, 2); + ComponentHandle position = a.assign(1, 2); REQUIRE(position); REQUIRE(position->x == 1); REQUIRE(position->y == 2); @@ -525,7 +525,7 @@ TEST_CASE_METHOD(EntityManagerFixture, "TestEntityComponentsFromTuple") { e.assign(1, 2); e.assign(3, 4); - std::tuple components = e.components(); + std::tuple, ComponentHandle> components = e.components(); REQUIRE(std::get<0>(components)->x == 1); REQUIRE(std::get<0>(components)->y == 2); -- cgit v1.2.3