From 37b9691e0d5fb07c2bd08160391c1883e4199e1c Mon Sep 17 00:00:00 2001 From: florianf Date: Sun, 28 Feb 2010 10:28:49 +0000 Subject: [PATCH] supertux/sector.[ch]pp: Implement Sector::get_nearest_player. git-svn-id: http://supertux.lethargik.org/svn/supertux/trunk/supertux@6444 837edb03-e0f3-0310-88ca-d4d4e8b29345 --- src/supertux/sector.cpp | 34 ++++++++++++++++++++++++++++++++++ src/supertux/sector.hpp | 1 + 2 files changed, 35 insertions(+) diff --git a/src/supertux/sector.cpp b/src/supertux/sector.cpp index aa856975a..7d9178c27 100644 --- a/src/supertux/sector.cpp +++ b/src/supertux/sector.cpp @@ -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 players = Sector::current()->get_players(); + for (std::vector::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 */ diff --git a/src/supertux/sector.hpp b/src/supertux/sector.hpp index f154766c9..4bf38d4ee 100644 --- a/src/supertux/sector.hpp +++ b/src/supertux/sector.hpp @@ -158,6 +158,7 @@ public: std::vector get_players() { return std::vector(1, this->player); } + Player *get_nearest_player (const Vector& pos); Rectf get_active_region(); -- 2.11.0