summaryrefslogtreecommitdiffstats
path: root/src/Mobs/Slime.cpp
blob: 320fa3c8a773c9ac878365e42329266110fb792c (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

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

#include "Slime.h"
#include "../FastRandom.h"
#include "../World.h"





cSlime::cSlime(int a_Size) :
	Super("Slime",
		mtSlime,
		fmt::format(FMT_STRING("entity.{}slime.hurt"),  GetSizeName(a_Size)),
		fmt::format(FMT_STRING("entity.{}slime.death"), GetSizeName(a_Size)),
		"",
		0.51f * a_Size,
		0.51f * a_Size
	),
	m_Size(a_Size)
{
	SetMaxHealth(static_cast<float>(a_Size * a_Size));
	SetAttackDamage(a_Size);
}





void cSlime::GetDrops(cItems & a_Drops, cEntity * a_Killer)
{
	unsigned int LootingLevel = 0;
	if (a_Killer != nullptr)
	{
		LootingLevel = a_Killer->GetEquippedWeapon().m_Enchantments.GetLevel(cEnchantments::enchLooting);
	}

	// Only slimes with the size 1 can drop slimeballs.
	if (m_Size == 1)
	{
		AddRandomDropItem(a_Drops, 0, 2 + LootingLevel, E_ITEM_SLIMEBALL);
	}
}





bool cSlime::Attack(std::chrono::milliseconds a_Dt)
{
	if (m_Size > 1)
	{
		// Only slimes larger than size 1 attack a player.
		return Super::Attack(a_Dt);
	}

	return false;
}





void cSlime::KilledBy(TakeDamageInfo & a_TDI)
{
	if (GetHealth() > 0)
	{
		return;
	}

	if (m_Size != 1)
	{
		auto & Random = GetRandomProvider();
		int SpawnAmount = Random.RandInt(2, 4);

		for (int i = 0; i < SpawnAmount; ++i)
		{
			double AddX = (i % 2 - 0.5) * m_Size / 4.0;
			double AddZ = (i / 2 - 0.5) * m_Size / 4.0;

			// Queue slimes to be spawned after the 1 second death animation has finished playing:
			m_World->ScheduleTask(cTickTime(20), [
				Position = GetPosition() + Vector3d(AddX, 0.5, AddZ),
				Yaw = Random.RandReal(360.0f),
				Size = m_Size / 2
			](cWorld & a_World)
			{
				auto NewSlime = std::make_unique<cSlime>(Size);
				NewSlime->SetPosition(Position);
				NewSlime->SetYaw(Yaw);
				a_World.SpawnMobFinalize(std::move(NewSlime));
			});
		}
	}
	Super::KilledBy(a_TDI);
}





AString cSlime::GetSizeName(int a_Size)
{
	if (a_Size == 1)
	{
		return "small_";
	}
	return "";
}