From 793acd267f4a5801e2aa9e9dca7cf51c5a595a02 Mon Sep 17 00:00:00 2001 From: Lane Kolbly Date: Wed, 12 Jul 2017 05:13:27 -0500 Subject: Changing pickup lifetime and combining semantics, making these adjustable in the lua api. (#3843) --- src/Entities/Pickup.cpp | 16 +++++++++++----- src/Entities/Pickup.h | 18 +++++++++++++++++- 2 files changed, 28 insertions(+), 6 deletions(-) (limited to 'src/Entities') diff --git a/src/Entities/Pickup.cpp b/src/Entities/Pickup.cpp index 5f64d6fbe..0c6c8feab 100644 --- a/src/Entities/Pickup.cpp +++ b/src/Entities/Pickup.cpp @@ -40,8 +40,9 @@ public: Vector3d EntityPos = a_Entity->GetPosition(); double Distance = (EntityPos - m_Position).Length(); - cItem & Item = static_cast(a_Entity)->GetItem(); - if ((Distance < 1.2) && Item.IsEqual(m_Pickup->GetItem())) + cPickup * OtherPickup = static_cast(a_Entity); + cItem & Item = OtherPickup->GetItem(); + if ((Distance < 1.2) && Item.IsEqual(m_Pickup->GetItem()) && OtherPickup->CanCombine()) { short CombineCount = Item.m_ItemCount; if ((CombineCount + m_Pickup->GetItem().m_ItemCount) > Item.GetMaxStackSize()) @@ -66,6 +67,9 @@ public: a_Entity->GetWorld()->BroadcastEntityRelMove(*a_Entity, static_cast(DiffX), static_cast(DiffY), static_cast(DiffZ)); /* End of experimental animation */ a_Entity->Destroy(); + + // Reset the timer + m_Pickup->SetAge(0); } else { @@ -92,12 +96,14 @@ protected: -cPickup::cPickup(double a_PosX, double a_PosY, double a_PosZ, const cItem & a_Item, bool IsPlayerCreated, float a_SpeedX /* = 0.f */, float a_SpeedY /* = 0.f */, float a_SpeedZ /* = 0.f */) +cPickup::cPickup(double a_PosX, double a_PosY, double a_PosZ, const cItem & a_Item, bool IsPlayerCreated, float a_SpeedX, float a_SpeedY, float a_SpeedZ, int a_LifetimeTicks, bool a_CanCombine) : cEntity(etPickup, a_PosX, a_PosY, a_PosZ, 0.2, 0.2) , m_Timer(0) , m_Item(a_Item) , m_bCollected(false) , m_bIsPlayerCreated(IsPlayerCreated) + , m_bCanCombine(a_CanCombine) + , m_Lifetime(cTickTime(a_LifetimeTicks)) { SetGravity(-16.0f); SetAirDrag(0.02f); @@ -165,7 +171,7 @@ void cPickup::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) } // Try to combine the pickup with adjacent same-item pickups: - if ((m_Item.m_ItemCount < m_Item.GetMaxStackSize()) && IsOnGround()) // Don't combine if already full or not on ground + if ((m_Item.m_ItemCount < m_Item.GetMaxStackSize()) && IsOnGround() && CanCombine()) // Don't combine if already full or not on ground { // By using a_Chunk's ForEachEntity() instead of cWorld's, pickups don't combine across chunk boundaries. // That is a small price to pay for not having to traverse the entire world for each entity. @@ -188,7 +194,7 @@ void cPickup::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) } } - if (m_Timer > std::chrono::minutes(5)) // 5 minutes + if (m_Timer > m_Lifetime) { Destroy(true); return; diff --git a/src/Entities/Pickup.h b/src/Entities/Pickup.h index ed5949f37..c2fcbd7f2 100644 --- a/src/Entities/Pickup.h +++ b/src/Entities/Pickup.h @@ -25,7 +25,7 @@ public: CLASS_PROTODEF(cPickup) - cPickup(double a_PosX, double a_PosY, double a_PosZ, const cItem & a_Item, bool IsPlayerCreated, float a_SpeedX = 0.f, float a_SpeedY = 0.f, float a_SpeedZ = 0.f); + cPickup(double a_PosX, double a_PosY, double a_PosZ, const cItem & a_Item, bool IsPlayerCreated, float a_SpeedX = 0.f, float a_SpeedY = 0.f, float a_SpeedZ = 0.f, int a_LifetimeTicks = 6000, bool a_CanCombine = true); cItem & GetItem(void) {return m_Item; } // tolua_export const cItem & GetItem(void) const {return m_Item; } @@ -36,12 +36,24 @@ public: virtual void Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) override; + /** Returns whether this pickup is allowed to combine with other similar pickups */ + bool CanCombine(void) const { return m_bCanCombine; } // tolua_export + + /** Sets whether this pickup is allowed to combine with other similar pickups */ + void SetCanCombine(bool a_CanCombine) { m_bCanCombine = a_CanCombine; } // tolua_export + /** Returns the number of ticks that this entity has existed */ int GetAge(void) const { return std::chrono::duration_cast(m_Timer).count(); } // tolua_export /** Set the number of ticks that this entity has existed */ void SetAge(int a_Age) { m_Timer = cTickTime(a_Age); } // tolua_export + /** Returns the number of ticks that this pickup should live for */ + int GetLifetime(void) const { return std::chrono::duration_cast(m_Lifetime).count(); } // tolua_export + + /** Set the number of ticks that this pickup should live for */ + void SetLifetime(int a_Lifetime) { m_Lifetime = cTickTime(a_Lifetime); } // tolua_export + /** Returns true if the pickup has already been collected */ bool IsCollected(void) const { return m_bCollected; } // tolua_export @@ -58,4 +70,8 @@ private: bool m_bCollected; bool m_bIsPlayerCreated; + + bool m_bCanCombine; + + std::chrono::milliseconds m_Lifetime; }; // tolua_export -- cgit v1.2.3