diff options
author | LogicParrot <LogicParrot@users.noreply.github.com> | 2017-09-02 09:45:06 +0200 |
---|---|---|
committer | Alexander Harkness <me@bearbin.net> | 2017-09-02 09:50:23 +0200 |
commit | 49c443896dcac8c4eaf08c4024e8bd2366ad899a (patch) | |
tree | b1ec46cab2b4e5731860c7136f1bbfca6fe9d458 /src/Entities/SplashPotionEntity.cpp | |
parent | SetSwimState now takes into account head height (diff) | |
download | cuberite-49c443896dcac8c4eaf08c4024e8bd2366ad899a.tar cuberite-49c443896dcac8c4eaf08c4024e8bd2366ad899a.tar.gz cuberite-49c443896dcac8c4eaf08c4024e8bd2366ad899a.tar.bz2 cuberite-49c443896dcac8c4eaf08c4024e8bd2366ad899a.tar.lz cuberite-49c443896dcac8c4eaf08c4024e8bd2366ad899a.tar.xz cuberite-49c443896dcac8c4eaf08c4024e8bd2366ad899a.tar.zst cuberite-49c443896dcac8c4eaf08c4024e8bd2366ad899a.zip |
Diffstat (limited to '')
-rw-r--r-- | src/Entities/SplashPotionEntity.cpp | 80 |
1 files changed, 56 insertions, 24 deletions
diff --git a/src/Entities/SplashPotionEntity.cpp b/src/Entities/SplashPotionEntity.cpp index 35afe0fea..af4008e83 100644 --- a/src/Entities/SplashPotionEntity.cpp +++ b/src/Entities/SplashPotionEntity.cpp @@ -17,6 +17,60 @@ //////////////////////////////////////////////////////////////////////////////// +// cSplashPotionEntityCallback: + +/** Used to distribute the splashed potion effect among nearby entities */ +class cSplashPotionCallback : + public cEntityCallback +{ +public: + /** Creates the callback. + @param a_HitPos The position where the splash potion has splashed + @param a_EntityEffectType The effect type of the potion + @param a_EntityEffect The effect description */ + cSplashPotionCallback(const Vector3d & a_HitPos, cEntityEffect::eType a_EntityEffectType, const cEntityEffect & a_EntityEffect) : + m_HitPos(a_HitPos), + m_EntityEffectType(a_EntityEffectType), + m_EntityEffect(a_EntityEffect) + { + } + + /** Called by cWorld::ForEachEntity(), adds the stored entity effect to the entity, if it is close enough. */ + virtual bool Item(cEntity * a_Entity) override + { + if (!a_Entity->IsPawn()) + { + // Not an entity that can take effects + return false; + } + + double SplashDistance = (a_Entity->GetPosition() - m_HitPos).Length(); + if (SplashDistance >= 20) + { + // Too far away + return false; + } + + // y = -0.25x + 1, where x is the distance from the player. Approximation for potion splash. + // TODO: better equation + double Reduction = -0.25 * SplashDistance + 1.0; + Reduction = std::max(Reduction, 0.0); + + static_cast<cPawn *>(a_Entity)->AddEntityEffect(m_EntityEffectType, m_EntityEffect.GetDuration(), m_EntityEffect.GetIntensity(), Reduction); + return false; + } + +private: + const Vector3d & m_HitPos; + cEntityEffect::eType m_EntityEffectType; + const cEntityEffect & m_EntityEffect; +}; + + + + + +//////////////////////////////////////////////////////////////////////////////// // cSplashPotionEntity: cSplashPotionEntity::cSplashPotionEntity( @@ -65,30 +119,8 @@ void cSplashPotionEntity::OnHitEntity(cEntity & a_EntityHit, const Vector3d & a_ void cSplashPotionEntity::Splash(const Vector3d & a_HitPos) { - m_World->ForEachEntity([=](cEntity & a_Entity) - { - if (!a_Entity.IsPawn()) - { - // Not an entity that can take effects - return false; - } - - double SplashDistance = (a_Entity.GetPosition() - a_HitPos).Length(); - if (SplashDistance >= 20) - { - // Too far away - return false; - } - - // y = -0.25x + 1, where x is the distance from the player. Approximation for potion splash. - // TODO: better equation - double Reduction = -0.25 * SplashDistance + 1.0; - Reduction = std::max(Reduction, 0.0); - - static_cast<cPawn &>(a_Entity).AddEntityEffect(m_EntityEffectType, m_EntityEffect.GetDuration(), m_EntityEffect.GetIntensity(), Reduction); - return false; - } - ); + cSplashPotionCallback Callback(a_HitPos, m_EntityEffectType, m_EntityEffect); + m_World->ForEachEntity(Callback); m_World->BroadcastSoundParticleEffect( EffectID::PARTICLE_SPLASH_POTION, |