From 1444d389cf841a0df151c387fab8296fe8866bf7 Mon Sep 17 00:00:00 2001 From: Erik Schilling Date: Fri, 17 Feb 2012 19:48:04 +0100 Subject: Fixed selecting of beings that are flanked between two others Before it was a bit tricky to select the a being if there are two others around. I fixed this by going through all sprites and looking for the closest one to the mouse. Reviewed-by: bjorn. --- src/actorspritemanager.cpp | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/actorspritemanager.cpp b/src/actorspritemanager.cpp index 88ecd86b..2ee80d03 100644 --- a/src/actorspritemanager.cpp +++ b/src/actorspritemanager.cpp @@ -179,6 +179,9 @@ Being *ActorSpriteManager::findBeingByPixel(int x, int y) const const int halfTileHeight = map->getTileHeight() / 2; + Being *closest = 0; + int closestDist; + for_actors { if ((*it)->getType() == ActorSprite::FLOOR_ITEM) @@ -188,20 +191,28 @@ Being *ActorSpriteManager::findBeingByPixel(int x, int y) const const int halfWidth = std::max(16, being->getWidth() / 2); const int height = std::max(32, being->getHeight()); + const int halfHeight = height / 2; // Being sprites are drawn half a tile lower than they actually stand const int bottom = being->getPixelY() + halfTileHeight; - if (being->isAlive() && - being != local_player && - being->getPixelX() - halfWidth <= x && - being->getPixelX() + halfWidth >= x && - bottom - height <= y && - bottom >= y) - return being; + const int dist = std::max(std::abs(bottom - halfHeight - y), + std::abs(being->getPixelX() - x)); + + if ((being->isAlive() && + being != local_player && + being->getPixelX() - halfWidth <= x && + being->getPixelX() + halfWidth >= x && + bottom - height <= y && + bottom >= y) && + (!closest || closestDist > dist)) + { + closest = being; + closestDist = dist; + } } - return NULL; + return closest; } FloorItem *ActorSpriteManager::findItem(int id) const -- cgit v1.2.3-60-g2f50