supertux/sector.[ch]pp: Implement Sector::get_nearest_player.
authorflorianf <florianf@837edb03-e0f3-0310-88ca-d4d4e8b29345>
Sun, 28 Feb 2010 10:28:49 +0000 (10:28 +0000)
committerflorianf <florianf@837edb03-e0f3-0310-88ca-d4d4e8b29345>
Sun, 28 Feb 2010 10:28:49 +0000 (10:28 +0000)
git-svn-id: http://supertux.lethargik.org/svn/supertux/trunk/supertux@6444 837edb03-e0f3-0310-88ca-d4d4e8b29345

src/supertux/sector.cpp
src/supertux/sector.hpp

index aa85697..7d9178c 100644 (file)
@@ -1893,5 +1893,39 @@ Sector::get_gravity() const
   return gravity;
 }
 
+Player *
+Sector::get_nearest_player (const Vector& pos)
+{
+  Player *nearest_player;
+  float nearest_dist;
+
+  nearest_player = NULL;
+
+  std::vector<Player*> players = Sector::current()->get_players();
+  for (std::vector<Player*>::iterator playerIter = players.begin();
+      playerIter != players.end();
+      ++playerIter)
+  {
+    Player *this_player = *playerIter;
+    float x_dist;
+    float y_dist;
+    float this_dist;
+
+    if (this_player->is_dying() || this_player->is_dead())
+      continue;
+
+    x_dist = fabs ((this_player->get_pos ().x) - pos.x);
+    y_dist = fabs ((this_player->get_pos ().y) - pos.y);
+    this_dist = sqrtf (x_dist*x_dist + y_dist*y_dist);
+
+    if ((nearest_player == NULL) || (nearest_dist > this_dist)) {
+      nearest_player = this_player;
+      nearest_dist = this_dist;
+    }
+  }
+
+  return (nearest_player);
+} /* Player *get_nearest_player */
+
 /* vim: set sw=2 sts=2 et : */
 /* EOF */
index f154766..4bf38d4 100644 (file)
@@ -158,6 +158,7 @@ public:
   std::vector<Player*> get_players() {
     return std::vector<Player*>(1, this->player);
   }
+  Player *get_nearest_player (const Vector& pos);
 
   Rectf get_active_region();