]> code.bitgloo.com Git - clyne/entityx.git/commitdiff
Add convenience `Component<T>::Handle` typedef.
authorAlec Thomas <alec@swapoff.org>
Sat, 7 Jun 2014 16:58:04 +0000 (02:58 +1000)
committerAlec Thomas <alec@swapoff.org>
Sat, 7 Jun 2014 16:58:04 +0000 (02:58 +1000)
README.md
entityx/Entity.h
entityx/Entity_test.cc

index 9f8ee622043752d36faf4f1c64966318bcf04b43..e6cc70c6e22a5b61852bb8156db7692da18e857b 100644 (file)
--- a/README.md
+++ b/README.md
@@ -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)) {
index 5f51b50129385d668351beb4dd05454945dc9a3f..3504f6f1317c37e8b3e941a43549820fee1b77d0 100644 (file)
@@ -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();
 };
index 1f87473ff95c051a83ec45c7ad21716f6968d181..107f0a3015c8c7c5df88577eaa0205248536a32a 100644 (file)
@@ -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);