From 49c443896dcac8c4eaf08c4024e8bd2366ad899a Mon Sep 17 00:00:00 2001 From: LogicParrot Date: Sat, 2 Sep 2017 10:45:06 +0300 Subject: Revert "Replace ItemCallbacks with lambdas (#3948)" This reverts commit 496c337cdfa593654018c171f6a74c28272265b5. --- src/Entities/LeashKnot.cpp | 61 +++++++++++++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 19 deletions(-) (limited to 'src/Entities/LeashKnot.cpp') diff --git a/src/Entities/LeashKnot.cpp b/src/Entities/LeashKnot.cpp index 51002531d..a86dc9d53 100644 --- a/src/Entities/LeashKnot.cpp +++ b/src/Entities/LeashKnot.cpp @@ -1,4 +1,4 @@ - + #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules #include "LeashKnot.h" @@ -38,42 +38,58 @@ void cLeashKnot::OnRightClicked(cPlayer & a_Player) -void cLeashKnot::TiePlayersLeashedMobs(cPlayer & a_Player, bool a_ShouldBroadcast) +void cLeashKnot::TiePlayersLeashedMobs(cPlayer & a_Player, bool a_ShouldBroadCast) { // Check leashed nearby mobs to tie them to this knot - // taking world from player (instead from this) because this can be called before entity was initialized - a_Player.GetWorld()->ForEachEntityInBox(cBoundingBox(GetPosition(), 8, 8, -4), [&](cEntity & a_Entity) + class LookForLeasheds : public cEntityCallback + { + public: + cLeashKnot * m_Knot; + cPlayer * m_Player; + bool m_ShouldBroadcast; + + LookForLeasheds(cLeashKnot * a_Knot, cPlayer * a_PlayerLeashedTo, bool a_ShouldBroadcast) : + m_Knot(a_Knot), + m_Player(a_PlayerLeashedTo), + m_ShouldBroadcast(a_ShouldBroadcast) + { + } + + virtual bool Item(cEntity * a_Entity) override { // If the entity is not a monster skip it - if (a_Entity.GetEntityType() != cEntity::eEntityType::etMonster) + if (a_Entity->GetEntityType() != cEntity::eEntityType::etMonster) { return false; } - auto & PotentialLeashed = static_cast(a_Entity); + cMonster * PotentialLeashed = static_cast(a_Entity); // If can't be leashed skip it - if (!PotentialLeashed.CanBeLeashed()) + if (!PotentialLeashed->CanBeLeashed()) { return false; } // If it's not leashed to the player skip it if ( - !PotentialLeashed.IsLeashed() || - !PotentialLeashed.GetLeashedTo()->IsPlayer() || - (PotentialLeashed.GetLeashedTo()->GetUniqueID() != a_Player.GetUniqueID()) + !PotentialLeashed->IsLeashed() || + !PotentialLeashed->GetLeashedTo()->IsPlayer() || + (PotentialLeashed->GetLeashedTo()->GetUniqueID() != m_Player->GetUniqueID()) ) { return false; } // All conditions met, unleash from player and leash to fence - PotentialLeashed.Unleash(false, false); - PotentialLeashed.LeashTo(*this, a_ShouldBroadcast); + PotentialLeashed->Unleash(false, false); + PotentialLeashed->LeashTo(*m_Knot, m_ShouldBroadcast); return false; } - ); + } LookForLeashedsCallback(this, &a_Player, a_ShouldBroadCast); + + // taking world from player (instead from this) because this can be called before entity was initialized + a_Player.GetWorld()->ForEachEntityInBox(cBoundingBox(GetPosition(), 8, 8, -4), LookForLeashedsCallback); } @@ -142,19 +158,26 @@ void cLeashKnot::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) cLeashKnot * cLeashKnot::FindKnotAtPos(cWorldInterface & a_WorldInterface, Vector3i a_BlockPos) { - cLeashKnot * LeashKnot = nullptr; - a_WorldInterface.ForEachEntityInBox(cBoundingBox(a_BlockPos, 0.5, 1), [&](cEntity & a_Entity) + class LookForKnot : public cEntityCallback + { + public: + cLeashKnot * m_LeashKnot = nullptr; + + virtual bool Item(cEntity * a_Entity) override { - if (a_Entity.IsLeashKnot()) + if (a_Entity->IsLeashKnot()) { - LeashKnot = static_cast(&a_Entity); + m_LeashKnot = reinterpret_cast(a_Entity); return true; } return false; } - ); - return LeashKnot; + } CallbackFindKnot; + + a_WorldInterface.ForEachEntityInBox(cBoundingBox(a_BlockPos, 0.5, 1), CallbackFindKnot); + + return CallbackFindKnot.m_LeashKnot; } -- cgit v1.2.3