From b35f44bfd9645f41ba7a1b7884a5624389a496f6 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Sun, 28 Feb 2010 10:28:49 +0000 Subject: [PATCH] supertux/sector.[ch]pp: Implement Sector::get_nearest_player. SVN-Revision: 6444 --- 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