1
0
cuberite-2a/src/Entities/ExpOrb.cpp

132 lines
2.5 KiB
C++
Raw Normal View History

2013-11-25 14:05:52 -05:00
#include "Globals.h"
#include "ExpOrb.h"
#include "Player.h"
#include "../ClientHandle.h"
cExpOrb::cExpOrb(Vector3d a_Pos, int a_Reward):
2021-04-06 11:09:16 -04:00
Super(etExpOrb, a_Pos, 0.5f, 0.5f),
m_Reward(a_Reward),
m_Timer(0)
2013-11-25 14:05:52 -05:00
{
2014-03-14 19:32:49 -04:00
SetMaxHealth(5);
SetHealth(5);
SetGravity(-16);
SetAirDrag(0.02f);
2013-11-25 14:05:52 -05:00
}
void cExpOrb::SpawnOn(cClientHandle & a_Client)
{
a_Client.SendExperienceOrb(*this);
m_bDirtyOrientation = false;
m_bDirtyHead = false;
}
void cExpOrb::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
2013-11-25 14:05:52 -05:00
{
DetectCacti();
m_TicksAlive++;
// Find closest player within 6.5 meter (slightly increase detect range to have same effect in client)
bool FoundPlayer = m_World->DoWithNearestPlayer(GetPosition(), 6.5, [&](cPlayer & a_Player) -> bool
2013-11-25 14:05:52 -05:00
{
Vector3f a_PlayerPos(a_Player.GetPosition());
a_PlayerPos.y += 0.8f;
Vector3f a_Distance = a_PlayerPos - GetPosition();
double Distance = a_Distance.Length();
if (Distance < 0.7f)
{
a_Player.DeltaExperience(m_Reward);
m_World->BroadcastSoundEffect("entity.experience_orb.pickup", GetPosition(), 0.5f, (0.75f + (static_cast<float>((GetUniqueID() * 23) % 32)) / 64));
Destroy();
return true;
}
// Experience orb will "float" or glide toward the player up to a distance of 6 blocks.
// speeding up as they get nearer to the player, Speed range 6 - 12 m per second, accelerate 60 m per second^2
Vector3d SpeedDelta(a_Distance);
SpeedDelta.Normalize();
SpeedDelta *= 3;
Vector3d CurrentSpeed = GetSpeed();
CurrentSpeed += SpeedDelta;
if (CurrentSpeed.Length() > 12)
2013-11-25 14:05:52 -05:00
{
CurrentSpeed.Normalize();
CurrentSpeed *= 12;
2013-11-25 14:05:52 -05:00
}
SetSpeed(CurrentSpeed);
m_Gravity = 0;
return true;
}, false, true); // Don't check line of sight, ignore spectator mode player
if (!FoundPlayer)
{
m_Gravity = -16;
2013-11-25 14:05:52 -05:00
}
2013-12-11 10:14:08 -05:00
HandlePhysics(a_Dt, a_Chunk);
BroadcastMovementUpdate();
2016-02-05 16:45:45 -05:00
2015-01-16 08:27:10 -05:00
m_Timer += a_Dt;
if (m_Timer >= std::chrono::minutes(5))
2014-03-14 19:32:49 -04:00
{
Destroy();
2014-03-14 19:32:49 -04:00
}
2014-02-14 16:49:23 -05:00
}
bool cExpOrb::DoTakeDamage(TakeDamageInfo & a_TDI)
{
if (a_TDI.DamageType == dtCactusContact)
{
Destroy();
return true;
}
2020-04-13 12:38:06 -04:00
return Super::DoTakeDamage(a_TDI);
}
std::vector<int> cExpOrb::Split(int a_Reward)
{
const static std::array<int, 11> BaseValue = {{1, 3, 7, 17, 37, 73, 149, 307, 617, 1237, 2477}};
std::vector<int> Rewards;
size_t Index = BaseValue.size() - 1; // Last one
while (a_Reward > 0)
{
while (a_Reward < BaseValue[Index])
{
Index--;
}
a_Reward -= BaseValue[Index];
Rewards.push_back(BaseValue[Index]);
}
return Rewards;
}