aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/LeapAtTargetGoal.cpp
blob: b559ef7db33997affedc05b0ad9a8fb67fd53885 (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
#include "stdafx.h"
#include "net.minecraft.world.entity.ai.control.h"
#include "net.minecraft.world.entity.h"
#include "LeapAtTargetGoal.h"

LeapAtTargetGoal::LeapAtTargetGoal(Mob *mob, float yd)
{
	target = weak_ptr<LivingEntity>();

	this->mob = mob;
	this->yd = yd;
	setRequiredControlFlags(Control::JumpControlFlag | Control::MoveControlFlag);
}

bool LeapAtTargetGoal::canUse()
{
	target = weak_ptr<LivingEntity>(mob->getTarget());
	if (target.lock() == nullptr) return false;
	double d = mob->distanceToSqr(target.lock());
	if (d < 2 * 2 || d > 4 * 4) return false;
	if (!mob->onGround) return false;
	if (mob->getRandom()->nextInt(5) != 0) return false;
	return true;
}

bool LeapAtTargetGoal::canContinueToUse()
{
	return target.lock() != nullptr && !mob->onGround;
}

void LeapAtTargetGoal::start()
{
	// TODO: move to control?
	double xdd = target.lock()->x - mob->x;
	double zdd = target.lock()->z - mob->z;
	float dd = sqrt(xdd * xdd + zdd * zdd);
	mob->xd += (xdd / dd * 0.5f) * 0.8f + mob->xd * 0.2f;
	mob->zd += (zdd / dd * 0.5f) * 0.8f + mob->zd * 0.2f;
	mob->yd = yd;
}