summaryrefslogtreecommitdiffstats
path: root/src/Mobs/Ghast.cpp
blob: 40a7bdf0ff0cf088f6baa80f18aefbcc4c334ffb (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

#include "Globals.h"  // NOTE: MSVC stupidness requires this to be the same across all modules

#include "Ghast.h"
#include "../World.h"
#include "../Entities/GhastFireballEntity.h"




cGhast::cGhast(void) :
	super("Ghast", mtGhast, "entity.ghast.hurt", "entity.ghast.death", "entity.ghast.ambient", 4, 4),
	m_IsCharging(false),
	m_FlightCooldown(5),
	m_TicksUntilShot(10)
{
	SetGravity(0);
	SetAirDrag(0);
}





void cGhast::GetDrops(cItems & a_Drops, cEntity * a_Killer)
{
	unsigned int LootingLevel = 0;
	if (a_Killer != nullptr)
	{
		LootingLevel = a_Killer->GetEquippedWeapon().m_Enchantments.GetLevel(cEnchantments::enchLooting);
	}
	AddRandomDropItem(a_Drops, 0, 2 + LootingLevel, E_ITEM_GUNPOWDER);
	AddRandomDropItem(a_Drops, 0, 1 + LootingLevel, E_ITEM_GHAST_TEAR);
}





bool cGhast::Attack(std::chrono::milliseconds a_Dt)
{
	if ((GetTarget() != nullptr) && (m_AttackCoolDownTicksLeft == 0) && (!m_IsCharging))
	{
		auto & Random = GetRandomProvider();
		auto SoundPitchMultiplier = 1.0f + (Random.RandReal(1.0f) - Random.RandReal(1.0f)) * 0.2f;

		m_World->BroadcastSoundEffect("entity.ghast.warn", GetPosition(), 4.0f, SoundPitchMultiplier * 0.9f);
		m_IsCharging = true;
		m_World->BroadcastEntityMetadata(*this);
		return true;
	}
	return false;
}





bool cGhast::DoTakeDamage(TakeDamageInfo & a_TDI)
{
	// No fall damage
	if (a_TDI.DamageType == dtFalling)
	{
		return false;
	}

	return super::DoTakeDamage(a_TDI);
}





void cGhast::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
{
	super::Tick(a_Dt, a_Chunk);
	if (!IsTicking())
	{
		// The base class tick destroyed us
		return;
	}

	if ((m_IsCharging) && (m_TicksUntilShot-- == 0))
	{
		m_TicksUntilShot = 10;
		m_IsCharging = false;
		m_World->BroadcastEntityMetadata(*this);

		Vector3d Speed = GetLookVector() * 20;
		Speed.y = Speed.y + 1;

		auto GhastBall = cpp14::make_unique<cGhastFireballEntity>(this, GetPosition(), Speed);
		auto GhastBallPtr = GhastBall.get();
		GhastBallPtr->Initialize(std::move(GhastBall), *m_World);

		m_World->BroadcastSoundEffect("entity.ghast.shoot", GetPosition(), 4.0f, 1.0f);

		ResetAttackCooldown();
	}

	// TODO: Better flying
	if (m_FlightCooldown-- == 0)
	{
		m_FlightCooldown = 5;
		auto & Random = GetRandomProvider();
		auto SpeedVector = Vector3d(Random.RandReal(-0.3, 0.3), Random.RandReal(-0.4, 0.4), Random.RandReal(-0.3, 0.3));

		if (GetPosY() > 120)
		{
			SpeedVector = Vector3d(Random.RandReal(-0.4, 0.4), Random.RandReal(-0.45, 0.4), Random.RandReal(-0.4, 0.4));
		}

		AddSpeed(SpeedVector);
	}
}