summaryrefslogtreecommitdiffstats
path: root/src/Entities/ExpOrb.cpp
blob: 2d59206c29b3f5e5db47aab91aa19381683353c1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#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);
	SetGravity(0);
}





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);
	SetGravity(0);
}





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)
{
	// Check player proximity no more than twice per second
	if ((m_TicksAlive % 10) == 0)
	{
		cPlayer * a_ClosestPlayer(m_World->FindClosestPlayer(Vector3f(GetPosition()), 5, false));
		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.5f)
			{
				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(true);
				return;
			}
			SetSpeedX((a_PlayerPos.x - GetPosition().x) * 2.0);
			SetSpeedY((a_PlayerPos.y - GetPosition().y) * 2.0);
			SetSpeedZ((a_PlayerPos.z - GetPosition().z) * 2.0);
		}
	}
	HandlePhysics(a_Dt, a_Chunk);

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