aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/OzelotAttackGoal.cpp
blob: 27ca2b5c1c905b696d776f645591bd2b2af3a0c9 (plain)
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
#include "stdafx.h"
#include "net.minecraft.world.entity.ai.control.h"
#include "net.minecraft.world.entity.ai.navigation.h"
#include "net.minecraft.world.entity.h"
#include "net.minecraft.world.entity.animal.h"
#include "net.minecraft.world.phys.h"
#include "OzelotAttackGoal.h"

OzelotAttackGoal::OzelotAttackGoal(Mob *mob)
{
	target = weak_ptr<Mob>();
	attackTime = 0;
	speed = 0;
	trackTarget = false;

	this->mob = mob;
	this->level = mob->level;
	setRequiredControlFlags(Control::MoveControlFlag | Control::LookControlFlag);
}

bool OzelotAttackGoal::canUse()
{
	shared_ptr<Mob> bestTarget = mob->getTarget();
	if (bestTarget == NULL) return false;
	target = weak_ptr<Mob>(bestTarget);
	return true;
}

bool OzelotAttackGoal::canContinueToUse()
{
	if (target.lock() == NULL || !target.lock()->isAlive()) return false;
	if (mob->distanceToSqr(target.lock()) > 15 * 15) return false;
	return !mob->getNavigation()->isDone() || canUse();
}

void OzelotAttackGoal::stop()
{
	target = weak_ptr<Mob>();
	mob->getNavigation()->stop();
}

void OzelotAttackGoal::tick()
{
	mob->getLookControl()->setLookAt(target.lock(), 30, 30);

	double meleeRadiusSqr = (mob->bbWidth * 2) * (mob->bbWidth * 2);
	double distSqr = mob->distanceToSqr(target.lock()->x, target.lock()->bb->y0, target.lock()->z);

	float speed = Ozelot::WALK_SPEED;
	if (distSqr > meleeRadiusSqr && distSqr < 4 * 4) speed = Ozelot::SPRINT_SPEED;
	else if (distSqr < 15 * 15) speed = Ozelot::SNEAK_SPEED;

	mob->getNavigation()->moveTo(target.lock(), speed);

	attackTime = max(attackTime - 1, 0);

	if (distSqr > meleeRadiusSqr) return;
	if (attackTime > 0) return;
	attackTime = 20;
	mob->doHurtTarget(target.lock());
}