summaryrefslogtreecommitdiff
path: root/src/actorspritemanager.cpp
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <bjorn@lindeijer.nl>2024-03-24 19:17:41 +0100
committerThorbjørn Lindeijer <bjorn@lindeijer.nl>2024-04-02 20:57:16 +0200
commit59919635523d41f3a15120c83db4b7d080c155de (patch)
tree0da989bd2342b5a53d49c26893a3a544e7432fcc /src/actorspritemanager.cpp
parent7ea1c4574d6e845f95f2c4c3a1dd4a5d730bc6ba (diff)
downloadmana-59919635523d41f3a15120c83db4b7d080c155de.tar.gz
mana-59919635523d41f3a15120c83db4b7d080c155de.tar.bz2
mana-59919635523d41f3a15120c83db4b7d080c155de.tar.xz
mana-59919635523d41f3a15120c83db4b7d080c155de.zip
General code cleanups
* Removed some unused includes * Removed unused ListBox::mFont * Removed wrong cast to SDL_Scancode * Removed superfluous .c_str() * Removed superfluous explicit std::string construction * Removed unused variable * Use more emplace_back * Turned FindBeingFunctor into a lambda * Avoid needless pointer references for ambient layers and use a vector
Diffstat (limited to 'src/actorspritemanager.cpp')
-rw-r--r--src/actorspritemanager.cpp68
1 files changed, 29 insertions, 39 deletions
diff --git a/src/actorspritemanager.cpp b/src/actorspritemanager.cpp
index 75e3fcc2..5308b3ad 100644
--- a/src/actorspritemanager.cpp
+++ b/src/actorspritemanager.cpp
@@ -26,32 +26,6 @@
#include <algorithm>
-class FindBeingFunctor
-{
- public:
- bool operator() (ActorSprite *actor) const
- {
- if (actor->getType() == ActorSprite::FLOOR_ITEM)
- return false;
- Game *game = Game::instance();
- if (!game)
- return false;
-
- auto *b = static_cast<Being*>(actor);
-
- uint16_t other_y = y + ((b->getType() == ActorSprite::NPC) ? 1 : 0);
- const Vector &pos = b->getPosition();
- return ((int) pos.x / game->getCurrentTileWidth() == x &&
- ((int) pos.y / game->getCurrentTileHeight() == y
- || (int) pos.y / game->getCurrentTileHeight() == other_y) &&
- b->isAlive() &&
- (type == ActorSprite::UNKNOWN || b->getType() == type));
- }
-
- uint16_t x, y;
- ActorSprite::Type type;
-};
-
class PlayerNamesLister : public AutoCompleteLister
{
void getAutoCompleteList(std::vector<std::string>& names) const override
@@ -59,11 +33,11 @@ class PlayerNamesLister : public AutoCompleteLister
names.clear();
for (auto actor : actorSpriteManager->getAll()) {
- if (actor->getType() == ActorSprite::FLOOR_ITEM)
+ if (actor->getType() != ActorSprite::PLAYER)
continue;
auto *being = static_cast<Being *>(actor);
- if (being->getType() == Being::PLAYER && !being->getName().empty())
+ if (!being->getName().empty())
names.push_back(being->getName());
}
}
@@ -156,14 +130,30 @@ Being *ActorSpriteManager::findBeing(int id) const
Being *ActorSpriteManager::findBeing(int x, int y, ActorSprite::Type type) const
{
- FindBeingFunctor beingFinder;
- beingFinder.x = x;
- beingFinder.y = y;
- beingFinder.type = type;
-
- auto it = find_if(mActors.begin(), mActors.end(), beingFinder);
+ const Game *game = Game::instance();
+ if (!game)
+ return nullptr;
- return (it == mActors.end()) ? nullptr : static_cast<Being*>(*it);
+ const int tileWidth = game->getCurrentTileWidth();
+ const int tileHeight = game->getCurrentTileHeight();
+
+ auto it = find_if(mActors.begin(), mActors.end(), [=] (ActorSprite *actor) {
+ const auto actorType = actor->getType();
+ if (actorType == ActorSprite::FLOOR_ITEM)
+ return false;
+ if (type != ActorSprite::UNKNOWN && actorType != type)
+ return false;
+
+ auto *b = static_cast<Being*>(actor);
+ uint16_t other_y = y + (actorType == ActorSprite::NPC ? 1 : 0);
+ const Vector &pos = b->getPosition();
+ return ((int) pos.x / tileWidth == x &&
+ ((int) pos.y / tileHeight == y
+ || (int) pos.y / tileHeight == other_y) &&
+ b->isAlive());
+ });
+
+ return it == mActors.end() ? nullptr : static_cast<Being*>(*it);
}
Being *ActorSpriteManager::findBeingByPixel(int x, int y) const
@@ -336,12 +326,12 @@ bool ActorSpriteManager::hasActorSprite(ActorSprite *someActor) const
return mActors.find(someActor) != mActors.end();
}
-AutoCompleteLister *ActorSpriteManager::getPlayerNameLister()
+AutoCompleteLister *ActorSpriteManager::getPlayerNameLister() const
{
return mPlayerNames;
}
-AutoCompleteLister *ActorSpriteManager::getPlayerNPCNameLister()
+AutoCompleteLister *ActorSpriteManager::getPlayerNPCNameLister() const
{
return mPlayerNPCNames;
}
@@ -350,11 +340,11 @@ void ActorSpriteManager::updatePlayerNames()
{
for (auto actor : mActors)
{
- if (actor->getType() == ActorSprite::FLOOR_ITEM)
+ if (actor->getType() != ActorSprite::PLAYER)
continue;
auto *being = static_cast<Being *>(actor);
- if (being->getType() == ActorSprite::PLAYER && !being->getName().empty())
+ if (!being->getName().empty())
being->updateName();
}
}