summaryrefslogblamecommitdiffstats
path: root/src/Entities/ExpOrb.cpp
blob: 1b4845c0953edcbd18d1d5e58a2fa802adfe9139 (plain) (tree)
1
2
3
4
5
6
7
8
9






                            
                                                                  
                                                      
                            
                    
 

                        





 
                                                      
                                                                  
                            
                    
 

                        








                                               







                                    
                                                                    
 
                                                                                          
                                                                                      

                                                                     
                                
                                                                 

                                                     
                 
                                                                                                                              
                                                                   
 
                                                                                                                                                                                             
 
                                  
                 
                                       
                                                                   


                                         
                                          
         
                                     
 

                                               


                              
 
#include "Globals.h"

#include "ExpOrb.h"
#include "Player.h"
#include "../ClientHandle.h"


cExpOrb::cExpOrb(double a_X, double a_Y, double a_Z, int a_Reward)
	: cEntity(etExpOrb, a_X, a_Y, a_Z, 0.98, 0.98)
	, m_Reward(a_Reward)
	, m_Timer(0)
{
	SetMaxHealth(5);
	SetHealth(5);
}





cExpOrb::cExpOrb(const Vector3d & a_Pos, int a_Reward)
	: cEntity(etExpOrb, a_Pos.x, a_Pos.y, a_Pos.z, 0.98, 0.98)
	, m_Reward(a_Reward)
	, m_Timer(0)
{
	SetMaxHealth(5);
	SetHealth(5);
}





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)
{
	cPlayer * a_ClosestPlayer(m_World->FindClosestPlayer(Vector3f(GetPosition()), 5));
	if ((a_ClosestPlayer != nullptr) && (!a_ClosestPlayer->IsGameModeSpectator()))
	{
		Vector3f a_PlayerPos(a_ClosestPlayer->GetPosition());
		a_PlayerPos.y++;
		Vector3f a_Distance(a_PlayerPos - GetPosition());
		double Distance(a_Distance.Length());
		if (Distance < 0.1f)
		{
			LOGD("Player %s picked up an ExpOrb. His reward is %i", a_ClosestPlayer->GetName().c_str(), m_Reward);
			a_ClosestPlayer->DeltaExperience(m_Reward);

			m_World->BroadcastSoundEffect("entity.experience_orb.pickup", GetPosX(), GetPosY(), GetPosZ(), 0.5f, (0.75f + (static_cast<float>((GetUniqueID() * 23) % 32)) / 64));

			Destroy();
		}
		a_Distance.Normalize();
		a_Distance *= (static_cast<float>(5.5 - Distance));
		SetSpeedX( a_Distance.x);
		SetSpeedY( a_Distance.y);
		SetSpeedZ( a_Distance.z);
		BroadcastMovementUpdate();
	}
	HandlePhysics(a_Dt, a_Chunk);

	m_Timer += a_Dt;
	if (m_Timer >= std::chrono::minutes(5))
	{
		Destroy(true);
	}
}