From 8a4b82e3ed75e426d44b69640f009e1e731c75c4 Mon Sep 17 00:00:00 2001 From: Erik Schilling Date: Thu, 19 Jan 2012 18:15:54 +0100 Subject: Prevented looking at item when picking up while moving + Fixes from Bjorns review. Reviewed-by: Bjorn. --- src/actorspritemanager.cpp | 15 ++++++++++----- src/actorspritemanager.h | 2 +- src/game.cpp | 26 ++++---------------------- src/localplayer.cpp | 4 ++++ src/vector.h | 5 +++++ 5 files changed, 24 insertions(+), 28 deletions(-) diff --git a/src/actorspritemanager.cpp b/src/actorspritemanager.cpp index 2928aa45..683f346d 100644 --- a/src/actorspritemanager.cpp +++ b/src/actorspritemanager.cpp @@ -218,18 +218,23 @@ FloorItem *ActorSpriteManager::findItem(int id) const return NULL; } -FloorItem *ActorSpriteManager::findItem(int x, int y) const +FloorItem *ActorSpriteManager::findItem(int x, int y, int maxDist) const { + FloorItem *item = 0; + int smallestDist = 0; for_actors { - if ((*it)->getTileX() == x && (*it)->getTileY() == y && - (*it)->getType() == ActorSprite::FLOOR_ITEM) + int dist = std::max(std::abs((*it)->getTileX() - x), + std::abs((*it)->getTileY() - y)); + if (((*it)->getType() == ActorSprite::FLOOR_ITEM) && + ((!item && dist <= maxDist) || dist < smallestDist)) { - return static_cast(*it); + item = static_cast(*it); + smallestDist = dist; } } - return NULL; + return item; } Being *ActorSpriteManager::findBeingByName(const std::string &name, diff --git a/src/actorspritemanager.h b/src/actorspritemanager.h index 64703396..ee41ae6d 100644 --- a/src/actorspritemanager.h +++ b/src/actorspritemanager.h @@ -96,7 +96,7 @@ class ActorSpriteManager /** * Returns a FloorItem at specific coordinates. */ - FloorItem *findItem(int x, int y) const; + FloorItem *findItem(int x, int y, int maxDist = 0) const; /** * Returns a being nearest to specific coordinates. diff --git a/src/game.cpp b/src/game.cpp index 1c24d5d1..7eb2cef1 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -377,29 +377,11 @@ static void handleItemPickUp() int y = local_player->getTileY(); // Let's look for items around until you find one. - bool found = false; - for (int xX = x - 1; xX < x + 2; ++xX) - { - for (int yY = y - 1; yY < y + 2; ++yY) - { - FloorItem *item = actorSpriteManager->findItem(xX, yY); - if (item) - { - found = true; - local_player->pickUp(item); - - // We found it, so set the player - // direction accordingly, - local_player->lookAt( - local_player->getMap()->getTileCenter(xX, yY)); + FloorItem *item = actorSpriteManager->findItem(x, y, 1); + if (!item) + return; - // Get out of the loops - break; - } - } - if (found) - break; - } + local_player->pickUp(item); } /** diff --git a/src/localplayer.cpp b/src/localplayer.cpp index a0350ce8..612fdd82 100644 --- a/src/localplayer.cpp +++ b/src/localplayer.cpp @@ -605,6 +605,10 @@ void LocalPlayer::pickUp(FloorItem *item) if (dx * dx + dy * dy < 4) { Net::getPlayerHandler()->pickUp(item); + // We found it, so set the player direction to it + // if the player does not move + if (getDestination() == getPosition()) + lookAt(item->getPosition()); mPickUpTarget = NULL; } else diff --git a/src/vector.h b/src/vector.h index 3360c20b..c0457bd7 100644 --- a/src/vector.h +++ b/src/vector.h @@ -182,6 +182,11 @@ class Vector float x, y, z; }; +inline bool operator == (const Vector &a, const Vector &b) +{ + return a.x == b.x && a.y == b.y && a.z == b.z; +} + /** * Appends a string representation of a vector to the output stream. */ -- cgit v1.2.3-70-g09d2