From 212f52d5556e6ea554b3adadefeabe05930a8ffd Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Sun, 18 Aug 2013 14:02:43 +0200 Subject: [PATCH] Added item pickup instruction whenever an item can be picked up --- source/Game.cpp | 15 ++++++++++++++- source/Game.h | 1 + source/World.cpp | 21 +++++++++++++++++++++ source/World.h | 1 + source/abstract/Character.cpp | 13 +------------ source/abstract/Character.h | 4 ++-- source/items/Item.h | 1 + 7 files changed, 41 insertions(+), 15 deletions(-) diff --git a/source/Game.cpp b/source/Game.cpp index 6bea712..5da6e8a 100644 --- a/source/Game.cpp +++ b/source/Game.cpp @@ -50,7 +50,9 @@ Game::Game(tgui::Window& window) : mLeftGadget = window.add(); mLeftGadget->setTextSize(14); mRightGadget = window.add(); - mRightGadget->setTextSize(14); + mRightGadget->setTextSize(14); + mPickupInstruction = window.add(); + mPickupInstruction->setTextSize(14); } /** @@ -120,6 +122,17 @@ Game::updateGui() { mWindow.getSize().y - mLeftGadget->getSize().y); mRightGadget->setPosition(mWindow.getSize().x / 2 + 10, mWindow.getSize().y - mRightGadget->getSize().y); + + auto item = mWorld.getClosestItem(mPlayer->getPosition()); + if (item.get() != nullptr) { + mPickupInstruction->setText("Press F to pick up " + item->getName()); + mPickupInstruction->setPosition( + mWindow.getSize().x / 2 - mPickupInstruction->getSize().x / 2, + mWindow.getSize().y * 0.66f); + mPickupInstruction->show(); + } + else + mPickupInstruction->hide(); } /** diff --git a/source/Game.h b/source/Game.h index 2540da9..6924ff7 100644 --- a/source/Game.h +++ b/source/Game.h @@ -51,6 +51,7 @@ private: tgui::Label* mCurrentWeapon; tgui::Label* mLeftGadget; tgui::Label* mRightGadget; + tgui::Label* mPickupInstruction; World mWorld; Pathfinder mPathfinder; diff --git a/source/World.cpp b/source/World.cpp index b1e93ef..053ac62 100755 --- a/source/World.cpp +++ b/source/World.cpp @@ -204,3 +204,24 @@ World::getNearbySprites(const Vector2f& position, float distance) const { ret.push_back(d); return ret; } + +/** + * Returns the item closest to position, or null if it is further than + * Character::ITEM_PICKUP_MAX_DISTANCE away. + */ +std::shared_ptr +World::getClosestItem(const Vector2f& position) const { + float distance = std::numeric_limits::max(); + std::shared_ptr closest; + for (auto& s : getNearbySprites(position, Character::ITEM_PICKUP_MAX_DISTANCE)) { + std::shared_ptr converted = std::dynamic_pointer_cast(s); + if (converted.get() != nullptr && + thor::squaredLength(position - converted->getPosition()) < distance * distance) { + closest = converted; + distance = thor::squaredLength(position - converted->getPosition()); + } + } + return (distance <= Character::ITEM_PICKUP_MAX_DISTANCE * Character::ITEM_PICKUP_MAX_DISTANCE) + ? closest + : std::shared_ptr(); +} diff --git a/source/World.h b/source/World.h index e39c148..60d63b1 100755 --- a/source/World.h +++ b/source/World.h @@ -33,6 +33,7 @@ public: const Vector2f& lineEnd) const; std::vector > getNearbySprites( const Vector2f& position, float radius) const; + std::shared_ptr getClosestItem(const Vector2f& position) const; private: diff --git a/source/abstract/Character.cpp b/source/abstract/Character.cpp index 35d7f5c..e6bd1ff 100644 --- a/source/abstract/Character.cpp +++ b/source/abstract/Character.cpp @@ -270,18 +270,7 @@ Character::getRightGadgetName() const { */ void Character::pickUpItem() { - auto sprites = mWorld.getNearbySprites(getPosition(), - ITEM_PICKUP_MAX_DISTANCE); - float distance = std::numeric_limits::max(); - std::shared_ptr closest; - for (auto& s : sprites) { - std::shared_ptr converted = std::dynamic_pointer_cast(s); - if (converted.get() != nullptr && - thor::squaredLength(getPosition() - converted->getPosition()) < distance) { - closest = converted; - distance = thor::squaredLength(getPosition() - converted->getPosition()); - } - } + std::shared_ptr closest = mWorld.getClosestItem(getPosition()); if (closest == nullptr) { std::swap(mLeftGadget, mRightGadget); diff --git a/source/abstract/Character.h b/source/abstract/Character.h index 1c317c5..ca676bd 100644 --- a/source/abstract/Character.h +++ b/source/abstract/Character.h @@ -29,6 +29,8 @@ public: /// Maximum distance where an enemy will be detected. static const float VISION_DISTANCE; + /// Maximum distance from character where an item can be picked up. + static const float ITEM_PICKUP_MAX_DISTANCE; public: explicit Character(const Vector2f& position, Category category, @@ -76,8 +78,6 @@ private: /// Distance to a path point where it will be considered as reached (to /// avoid floating point equality check). static const float POINT_REACHED_DISTANCE; - /// Maximum distance from character where an item can be picked up. - static const float ITEM_PICKUP_MAX_DISTANCE; friend class World; World& mWorld; Pathfinder& mPathfinder; diff --git a/source/items/Item.h b/source/items/Item.h index f8dbcef..d4f803a 100644 --- a/source/items/Item.h +++ b/source/items/Item.h @@ -17,6 +17,7 @@ public: Item(const Vector2f& size, const std::string& texture); virtual ~Item() {}; + virtual std::string getName() const = 0; void drop(const Vector2f& position); bool testCollision(std::shared_ptr other, Vector2f& offsetFirst, const Vector2f& offsetSecond);