From 8e65b94667b96a26a6ca19ab0be5ba2532066b8f Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 29 Mar 2013 18:48:49 +0100 Subject: [PATCH] Added function to get nearby characters. --- source/World.cpp | 14 ++++++++++++++ source/World.h | 2 ++ source/abstract/Character.cpp | 8 ++++++++ source/abstract/Character.h | 1 + 4 files changed, 25 insertions(+) diff --git a/source/World.cpp b/source/World.cpp index fe6d239..8f31c92 100755 --- a/source/World.cpp +++ b/source/World.cpp @@ -226,6 +226,20 @@ World::getPath(const sf::Vector2f& start, const sf::Vector2f& end, return path; } +/** + * Returns all characters that are within maxDistance from position. + */ +std::vector > + World::getCharacters(const sf::Vector2f& position, float maxDistance) const { + std::vector > visible; + for (auto it : mCharacters) { + if (thor::squaredLength(position - it->getPosition()) <= + maxDistance * maxDistance) { + visible.push_back(it); + } + } + return visible; +} /** * Returns the linear distance between two areas (using their center). */ diff --git a/source/World.h b/source/World.h index 35a7b83..57defed 100755 --- a/source/World.h +++ b/source/World.h @@ -32,6 +32,8 @@ public: void generateAreas(); std::vector getPath(const sf::Vector2f& start, const sf::Vector2f& end, float radius) const; + std::vector > + getCharacters(const sf::Vector2f& position, float maxDistance) const; // Private types. private: diff --git a/source/abstract/Character.cpp b/source/abstract/Character.cpp index c6effce..0a67dde 100644 --- a/source/abstract/Character.cpp +++ b/source/abstract/Character.cpp @@ -132,3 +132,11 @@ Character::move() { } } } + +/** + * Calls World::getCharacters with current position. + */ +std::vector > +Character::getCharacters(float maxDistance) const { + return mWorld.getCharacters(getPosition(), maxDistance); +} diff --git a/source/abstract/Character.h b/source/abstract/Character.h index 6dc9a1d..38532d2 100644 --- a/source/abstract/Character.h +++ b/source/abstract/Character.h @@ -34,6 +34,7 @@ protected: void releaseTrigger(); bool setDestination(const sf::Vector2f& destination); void move(); + std::vector > getCharacters(float maxDistance) const; // Private variables. private: