diff options
author | Alec Thomas <alec@swapoff.org> | 2014-06-08 02:58:04 +1000 |
---|---|---|
committer | Alec Thomas <alec@swapoff.org> | 2014-06-08 02:58:04 +1000 |
commit | 13c786cbeceb7d0d385a30326e34caaec7b84a17 (patch) | |
tree | d73a1e58e12b139ae3c57b0ff17ef5de9526b152 | |
parent | c9feb382ac12d11e45a811fc2f18e5bb98b4bbbe (diff) |
Add convenience `Component<T>::Handle` typedef.
-rw-r--r-- | README.md | 12 | ||||
-rw-r--r-- | entityx/Entity.h | 2 | ||||
-rw-r--r-- | entityx/Entity_test.cc | 22 |
3 files changed, 19 insertions, 17 deletions
@@ -118,8 +118,8 @@ entity.assign<Position>(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> position; -ComponentHandle<Direction> 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>()``: ```c++ -ComponentHandle<Position> position = entity.component<Position>(); +Position::Handle position = entity.component<Position>(); 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<MovementSystem> { void update(entityx::EntityManager &es, entityx::EventManager &events, double dt) override { - ComponentHandle<Position> position; - ComponentHandle<Direction> 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<CollisionSystem> { public: void update(entityx::EntityManager &es, entityx::EventManager &events, double dt) override { - ComponentHandle<Position> 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 <typename Derived> struct Component : public BaseComponent { public: + typedef ComponentHandle<Derived> 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<std::pair<ComponentHandle<Position>, ComponentHandle<Direction>>> position_directions; + std::vector<std::pair<Position::Handle, Direction::Handle>> position_directions; position_directions.push_back(std::make_pair( e.assign<Position>(1.0f, 2.0f), e.assign<Direction>(3.0f, 4.0f))); position_directions.push_back(std::make_pair( @@ -206,10 +206,10 @@ TEST_CASE_METHOD(EntityManagerFixture, "TestGetEntitiesWithComponentAndUnpacking g.assign<Position>(5.0f, 6.0f); int i = 0; - ComponentHandle<Position> position; + Position::Handle position; REQUIRE(3 == size(em.entities_with_components(position))); - ComponentHandle<Direction> 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; + 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<Direction>(3.0, 4.0); auto t = e.assign<Tag>("tag"); - ComponentHandle<Position> up; - ComponentHandle<Direction> ud; - ComponentHandle<Tag> 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<Direction> removed; + Direction::Handle removed; }; ComponentRemovedReceiver receiver; @@ -377,7 +377,7 @@ TEST_CASE_METHOD(EntityManagerFixture, "TestComponentRemovedEvent") { REQUIRE(!(receiver.removed)); Entity e = em.create(); - ComponentHandle<Direction> p = e.assign<Direction>(1.0, 2.0); + Direction::Handle p = e.assign<Direction>(1.0, 2.0); e.remove<Direction>(); REQUIRE(receiver.removed == p); REQUIRE(!(e.component<Direction>())); @@ -431,7 +431,7 @@ TEST_CASE_METHOD(EntityManagerFixture, "TestEntityDestroyHole") { TEST_CASE_METHOD(EntityManagerFixture, "TestComponentHandleInvalidatedWhenEntityDestroyed") { Entity a = em.create(); - ComponentHandle<Position> position = a.assign<Position>(1, 2); + Position::Handle position = a.assign<Position>(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> position = a.assign<Position>(1, 2); + Position::Handle position = a.assign<Position>(1, 2); REQUIRE(position); REQUIRE(position->x == 1); REQUIRE(position->y == 2); |