Added item pickup instruction whenever an item can be picked up

This commit is contained in:
Felix Ableitner 2013-08-18 14:02:43 +02:00
parent 384ab6c486
commit 212f52d555
7 changed files with 41 additions and 15 deletions

View file

@ -51,6 +51,8 @@ Game::Game(tgui::Window& window) :
mLeftGadget->setTextSize(14); mLeftGadget->setTextSize(14);
mRightGadget = window.add<tgui::Label>(); mRightGadget = window.add<tgui::Label>();
mRightGadget->setTextSize(14); mRightGadget->setTextSize(14);
mPickupInstruction = window.add<tgui::Label>();
mPickupInstruction->setTextSize(14);
} }
/** /**
@ -120,6 +122,17 @@ Game::updateGui() {
mWindow.getSize().y - mLeftGadget->getSize().y); mWindow.getSize().y - mLeftGadget->getSize().y);
mRightGadget->setPosition(mWindow.getSize().x / 2 + 10, mRightGadget->setPosition(mWindow.getSize().x / 2 + 10,
mWindow.getSize().y - mRightGadget->getSize().y); 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();
} }
/** /**

View file

@ -51,6 +51,7 @@ private:
tgui::Label* mCurrentWeapon; tgui::Label* mCurrentWeapon;
tgui::Label* mLeftGadget; tgui::Label* mLeftGadget;
tgui::Label* mRightGadget; tgui::Label* mRightGadget;
tgui::Label* mPickupInstruction;
World mWorld; World mWorld;
Pathfinder mPathfinder; Pathfinder mPathfinder;

View file

@ -204,3 +204,24 @@ World::getNearbySprites(const Vector2f& position, float distance) const {
ret.push_back(d); ret.push_back(d);
return ret; return ret;
} }
/**
* Returns the item closest to position, or null if it is further than
* Character::ITEM_PICKUP_MAX_DISTANCE away.
*/
std::shared_ptr<Item>
World::getClosestItem(const Vector2f& position) const {
float distance = std::numeric_limits<float>::max();
std::shared_ptr<Item> closest;
for (auto& s : getNearbySprites(position, Character::ITEM_PICKUP_MAX_DISTANCE)) {
std::shared_ptr<Item> converted = std::dynamic_pointer_cast<Item>(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<Item>();
}

View file

@ -33,6 +33,7 @@ public:
const Vector2f& lineEnd) const; const Vector2f& lineEnd) const;
std::vector<std::shared_ptr<Sprite> > getNearbySprites( std::vector<std::shared_ptr<Sprite> > getNearbySprites(
const Vector2f& position, float radius) const; const Vector2f& position, float radius) const;
std::shared_ptr<Item> getClosestItem(const Vector2f& position) const;
private: private:

View file

@ -270,18 +270,7 @@ Character::getRightGadgetName() const {
*/ */
void void
Character::pickUpItem() { Character::pickUpItem() {
auto sprites = mWorld.getNearbySprites(getPosition(), std::shared_ptr<Item> closest = mWorld.getClosestItem(getPosition());
ITEM_PICKUP_MAX_DISTANCE);
float distance = std::numeric_limits<float>::max();
std::shared_ptr<Item> closest;
for (auto& s : sprites) {
std::shared_ptr<Item> converted = std::dynamic_pointer_cast<Item>(s);
if (converted.get() != nullptr &&
thor::squaredLength(getPosition() - converted->getPosition()) < distance) {
closest = converted;
distance = thor::squaredLength(getPosition() - converted->getPosition());
}
}
if (closest == nullptr) { if (closest == nullptr) {
std::swap(mLeftGadget, mRightGadget); std::swap(mLeftGadget, mRightGadget);

View file

@ -29,6 +29,8 @@ public:
/// Maximum distance where an enemy will be detected. /// Maximum distance where an enemy will be detected.
static const float VISION_DISTANCE; static const float VISION_DISTANCE;
/// Maximum distance from character where an item can be picked up.
static const float ITEM_PICKUP_MAX_DISTANCE;
public: public:
explicit Character(const Vector2f& position, Category category, 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 /// Distance to a path point where it will be considered as reached (to
/// avoid floating point equality check). /// avoid floating point equality check).
static const float POINT_REACHED_DISTANCE; 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; friend class World;
World& mWorld; World& mWorld;
Pathfinder& mPathfinder; Pathfinder& mPathfinder;

View file

@ -17,6 +17,7 @@ public:
Item(const Vector2f& size, const std::string& texture); Item(const Vector2f& size, const std::string& texture);
virtual ~Item() {}; virtual ~Item() {};
virtual std::string getName() const = 0;
void drop(const Vector2f& position); void drop(const Vector2f& position);
bool testCollision(std::shared_ptr<Sprite> other, bool testCollision(std::shared_ptr<Sprite> other,
Vector2f& offsetFirst, const Vector2f& offsetSecond); Vector2f& offsetFirst, const Vector2f& offsetSecond);