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);
mRightGadget = window.add<tgui::Label>();
mRightGadget->setTextSize(14);
mPickupInstruction = window.add<tgui::Label>();
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();
}
/**

View file

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

View file

@ -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<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;
std::vector<std::shared_ptr<Sprite> > getNearbySprites(
const Vector2f& position, float radius) const;
std::shared_ptr<Item> getClosestItem(const Vector2f& position) const;
private:

View file

@ -270,18 +270,7 @@ Character::getRightGadgetName() const {
*/
void
Character::pickUpItem() {
auto sprites = mWorld.getNearbySprites(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());
}
}
std::shared_ptr<Item> closest = mWorld.getClosestItem(getPosition());
if (closest == nullptr) {
std::swap(mLeftGadget, mRightGadget);

View file

@ -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;

View file

@ -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<Sprite> other,
Vector2f& offsetFirst, const Vector2f& offsetSecond);