From 13c786cbeceb7d0d385a30326e34caaec7b84a17 Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Sun, 8 Jun 2014 02:58:04 +1000 Subject: Add convenience `Component::Handle` typedef. --- README.md | 12 ++++++------ entityx/Entity.h | 2 ++ entityx/Entity_test.cc | 22 +++++++++++----------- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 9f8ee62..e6cc70c 100644 --- a/README.md +++ b/README.md @@ -118,8 +118,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++ -ComponentHandle position; -ComponentHandle direction; +Position::Handle position; +Direction::Handle direction; for (Entity entity : entities.entities_with_components(position, direction)) { // Do things with entity, position and direction. } @@ -128,7 +128,7 @@ for (Entity entity : entities.entities_with_components(position, direction)) { To retrieve a component associated with an entity use ``entityx::Entity::component()``: ```c++ -ComponentHandle position = entity.component(); +Position::Handle position = entity.component(); if (position) { // Do stuff with position } @@ -161,8 +161,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, double dt) override { - ComponentHandle position; - ComponentHandle direction; + Position::Handle position; + Direction::Handle direction; for (Entity entity : es.entities_with_components(position, direction)) { position->x += direction->x * dt; position->y += direction->y * dt; @@ -198,7 +198,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, double dt) override { - ComponentHandle left_position, right_position; + Position::Handle 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 5f51b50..3504f6f 100644 --- a/entityx/Entity.h +++ b/entityx/Entity.h @@ -240,6 +240,8 @@ struct BaseComponent { template struct Component : public BaseComponent { public: + typedef ComponentHandle Handle; + /// Used internally for registration. static Family family(); }; diff --git a/entityx/Entity_test.cc b/entityx/Entity_test.cc index 1f87473..107f0a3 100644 --- a/entityx/Entity_test.cc +++ b/entityx/Entity_test.cc @@ -197,7 +197,7 @@ TEST_CASE_METHOD(EntityManagerFixture, "TestGetEntitiesWithComponentAndUnpacking Entity e = em.create(); Entity f = em.create(); Entity g = em.create(); - std::vector, ComponentHandle>> position_directions; + std::vector> 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( @@ -206,10 +206,10 @@ TEST_CASE_METHOD(EntityManagerFixture, "TestGetEntitiesWithComponentAndUnpacking g.assign(5.0f, 6.0f); int i = 0; - ComponentHandle position; + Position::Handle position; REQUIRE(3 == size(em.entities_with_components(position))); - ComponentHandle direction; + Direction::Handle direction; for (auto unused_entity : em.entities_with_components(position, direction)) { (void)unused_entity; REQUIRE(position); @@ -220,7 +220,7 @@ TEST_CASE_METHOD(EntityManagerFixture, "TestGetEntitiesWithComponentAndUnpacking ++i; } REQUIRE(2 == i); - ComponentHandle tag; + Tag::Handle tag; i = 0; for (auto unused_entity : em.entities_with_components(position, direction, tag)) { @@ -243,9 +243,9 @@ TEST_CASE_METHOD(EntityManagerFixture, "TestUnpack") { auto d = e.assign(3.0, 4.0); auto t = e.assign("tag"); - ComponentHandle up; - ComponentHandle ud; - ComponentHandle ut; + Position::Handle up; + Direction::Handle ud; + Tag::Handle ut; e.unpack(up); REQUIRE(p == up); e.unpack(up, ud); @@ -369,7 +369,7 @@ TEST_CASE_METHOD(EntityManagerFixture, "TestComponentRemovedEvent") { removed = event.component; } - ComponentHandle removed; + Direction::Handle removed; }; ComponentRemovedReceiver receiver; @@ -377,7 +377,7 @@ TEST_CASE_METHOD(EntityManagerFixture, "TestComponentRemovedEvent") { REQUIRE(!(receiver.removed)); Entity e = em.create(); - ComponentHandle p = e.assign(1.0, 2.0); + Direction::Handle p = e.assign(1.0, 2.0); e.remove(); REQUIRE(receiver.removed == p); REQUIRE(!(e.component())); @@ -431,7 +431,7 @@ TEST_CASE_METHOD(EntityManagerFixture, "TestEntityDestroyHole") { TEST_CASE_METHOD(EntityManagerFixture, "TestComponentHandleInvalidatedWhenEntityDestroyed") { Entity a = em.create(); - ComponentHandle position = a.assign(1, 2); + Position::Handle position = a.assign(1, 2); REQUIRE(position); REQUIRE(position->x == 1); REQUIRE(position->y == 2); @@ -442,7 +442,7 @@ TEST_CASE_METHOD(EntityManagerFixture, "TestComponentHandleInvalidatedWhenEntity TEST_CASE_METHOD(EntityManagerFixture, "TestComponentHandleInvalidatedWhenComponentDestroyed") { Entity a = em.create(); - ComponentHandle position = a.assign(1, 2); + Position::Handle position = a.assign(1, 2); REQUIRE(position); REQUIRE(position->x == 1); REQUIRE(position->y == 2); -- cgit v1.2.3