From 7272d727271b226948ff56f486a3f322fe1762bd Mon Sep 17 00:00:00 2001 From: Thorbjørn Lindeijer Date: Wed, 18 Jan 2012 23:01:52 +0100 Subject: Fixed the click area for NPCs and other players By taking into account that the sprite is drawn half a tile lower than the position of the being (that is messy on its own, but hard to fix while supporting both pixel and tile based movement). It also makes sure that the click area is at least 32x32 pixels, no matter how small the being. This fixes the lack of a clickable area for NPCs without a sprite (like some chests or signs) and makes small monsters like maggots easier to hit. Reviewed-by: Yohann Ferreira --- src/actorspritemanager.cpp | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'src/actorspritemanager.cpp') diff --git a/src/actorspritemanager.cpp b/src/actorspritemanager.cpp index 82e14de0..2928aa45 100644 --- a/src/actorspritemanager.cpp +++ b/src/actorspritemanager.cpp @@ -173,6 +173,12 @@ Being *ActorSpriteManager::findBeing(int x, int y, ActorSprite::Type type) const Being *ActorSpriteManager::findBeingByPixel(int x, int y) const { + Map *map = Game::instance() ? Game::instance()->getCurrentMap() : 0; + if (!map) + return 0; + + const int halfTileHeight = map->getTileHeight() / 2; + for_actors { if ((*it)->getType() == ActorSprite::FLOOR_ITEM) @@ -180,15 +186,18 @@ Being *ActorSpriteManager::findBeingByPixel(int x, int y) const Being *being = static_cast(*it); - int xtol = being->getWidth() / 2; - int uptol = being->getHeight(); + const int halfWidth = std::max(16, being->getWidth() / 2); + const int height = std::max(32, being->getHeight()); + + // 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() - xtol <= x) && - (being->getPixelX() + xtol >= x) && - (being->getPixelY() - uptol <= y) && - (being->getPixelY() >= y)) + if (being->isAlive() && + being != local_player && + being->getPixelX() - halfWidth <= x && + being->getPixelX() + halfWidth >= x && + bottom - height <= y && + bottom >= y) return being; } -- cgit v1.2.3-70-g09d2