summaryrefslogtreecommitdiffstats
path: root/src/Mobs/Behaviors/BehaviorAggressive.cpp
blob: 3b0cf6eea1a534f8c9e005478f9ef223b78f35c5 (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
#include "Globals.h"  // NOTE: MSVC stupidness requires this to be the same across all modules

#include "BehaviorAggressive.h"
#include "BehaviorAttacker.h"
#include "../Monster.h"
#include "../../Chunk.h"
#include "../../Entities/Player.h"



cBehaviorAggressive::cBehaviorAggressive(ShouldBeAggressiveFunction a_ShouldBeAggressiveFunction)
	: m_ShouldBeAggressiveFunction(a_ShouldBeAggressiveFunction)
	, m_ShouldBeAgressive(true)
	, m_AgressionCheckCountdown(1)
{

}

void cBehaviorAggressive::AttachToMonster(cMonster & a_Parent)
{
	m_Parent = &a_Parent;
	m_Parent->AttachPreTickBehavior(this);
}


void cBehaviorAggressive::PreTick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
{
	UNUSED(a_Dt);
	UNUSED(a_Chunk);

	if (m_ShouldBeAggressiveFunction != nullptr)
	{
		if (--m_AgressionCheckCountdown == 0)
		{
			m_AgressionCheckCountdown = 40;
			m_ShouldBeAgressive = m_ShouldBeAggressiveFunction(*this, *m_Parent);
		}
	}

	if (!m_ShouldBeAgressive)
	{
		return;
	}

	// Target something new if we have no target
	cBehaviorAttacker * BehaviorAttacker = m_Parent->GetBehaviorAttacker();
	if ((BehaviorAttacker != nullptr) && (BehaviorAttacker->GetTarget() == nullptr))
	{
		// mobTodo enhance this
		BehaviorAttacker->SetTarget(FindNewTarget());
	}
}





cPawn * cBehaviorAggressive::FindNewTarget()
{
	cPlayer * Closest = m_Parent->GetNearestPlayer();
	if ((Closest != nullptr) && (!Closest->CanMobsTarget()))
	{
		return nullptr;
	}
	return Closest;  // May be null
}