From 496c337cdfa593654018c171f6a74c28272265b5 Mon Sep 17 00:00:00 2001 From: peterbell10 Date: Fri, 1 Sep 2017 12:04:50 +0100 Subject: Replace ItemCallbacks with lambdas (#3948) --- src/Entities/Player.cpp | 73 ++++++++++++------------------------------------- 1 file changed, 17 insertions(+), 56 deletions(-) (limited to 'src/Entities/Player.cpp') diff --git a/src/Entities/Player.cpp b/src/Entities/Player.cpp index fb2274cad..93368f6fb 100644 --- a/src/Entities/Player.cpp +++ b/src/Entities/Player.cpp @@ -1005,36 +1005,21 @@ bool cPlayer::DoTakeDamage(TakeDamageInfo & a_TDI) void cPlayer::NotifyNearbyWolves(cPawn * a_Opponent, bool a_IsPlayerInvolved) { ASSERT(a_Opponent != nullptr); - class LookForWolves : public cEntityCallback - { - public: - cPlayer * m_Player; - cPawn * m_Attacker; - bool m_IsPlayerInvolved; - - LookForWolves(cPlayer * a_Me, cPawn * a_MyAttacker, bool a_PlayerInvolved) : - m_Player(a_Me), - m_Attacker(a_MyAttacker), - m_IsPlayerInvolved(a_PlayerInvolved) - { - } - virtual bool Item(cEntity * a_Entity) override + m_World->ForEachEntityInBox(cBoundingBox(GetPosition(), 16), [&] (cEntity & a_Entity) { - if (a_Entity->IsMob()) + if (a_Entity.IsMob()) { - cMonster * Mob = static_cast(a_Entity); - if (Mob->GetMobType() == mtWolf) + auto & Mob = static_cast(a_Entity); + if (Mob.GetMobType() == mtWolf) { - cWolf * Wolf = static_cast(Mob); - Wolf->ReceiveNearbyFightInfo(m_Player->GetUUID(), m_Attacker, m_IsPlayerInvolved); + auto & Wolf = static_cast(Mob); + Wolf.ReceiveNearbyFightInfo(GetUUID(), a_Opponent, a_IsPlayerInvolved); } } return false; } - } Callback(this, a_Opponent, a_IsPlayerInvolved); - - m_World->ForEachEntityInBox(cBoundingBox(GetPosition(), 16), Callback); + ); } @@ -2432,17 +2417,12 @@ void cPlayer::HandleFloater() { return; } - class cFloaterCallback : - public cEntityCallback - { - public: - virtual bool Item(cEntity * a_Entity) override + m_World->DoWithEntityByID(m_FloaterID, [](cEntity & a_Entity) { - a_Entity->Destroy(true); + a_Entity.Destroy(true); return true; } - } Callback; - m_World->DoWithEntityByID(m_FloaterID, Callback); + ); SetIsFishing(false); } @@ -2686,29 +2666,18 @@ bool cPlayer::DoesPlacingBlocksIntersectEntity(const sSetBlockVector & a_Blocks) cWorld * World = GetWorld(); // Check to see if any entity intersects any block being placed - class DoesIntersectBlock : public cEntityCallback - { - public: - const std::vector & m_BoundingBoxes; - - // The distance inside the block the entity can still be. - const double EPSILON = 0.0005; - - DoesIntersectBlock(const std::vector & a_BoundingBoxes) : - m_BoundingBoxes(a_BoundingBoxes) + return !World->ForEachEntityInBox(PlacingBounds, [&](cEntity & a_Entity) { - } + // The distance inside the block the entity can still be. + const double EPSILON = 0.0005; - virtual bool Item(cEntity * a_Entity) override - { - if (!a_Entity->DoesPreventBlockPlacement()) + if (!a_Entity.DoesPreventBlockPlacement()) { return false; } - cBoundingBox EntBox(a_Entity->GetPosition(), a_Entity->GetWidth() / 2, a_Entity->GetHeight()); - for (auto BlockBox: m_BoundingBoxes) + cBoundingBox EntBox(a_Entity.GetPosition(), a_Entity.GetWidth() / 2, a_Entity.GetHeight()); + for (auto BlockBox : PlacementBoxes) { - // Put in a little bit of wiggle room BlockBox.Expand(-EPSILON, -EPSILON, -EPSILON); if (EntBox.DoesIntersect(BlockBox)) @@ -2718,15 +2687,7 @@ bool cPlayer::DoesPlacingBlocksIntersectEntity(const sSetBlockVector & a_Blocks) } return false; } - } Callback(PlacementBoxes); - - // See if any entities in that bounding box collide with anyone - if (!World->ForEachEntityInBox(PlacingBounds, Callback)) - { - return true; - } - - return false; + ); } -- cgit v1.2.3