aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Client/CompassTexture.cpp
blob: 148069916312b398a18f71700ef7e9523d00e645 (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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "stdafx.h"
#include "Minecraft.h"
#include "..\Minecraft.World\net.minecraft.world.level.h"
#include "..\Minecraft.World\net.minecraft.world.level.dimension.h"
#include "MultiplayerLocalPlayer.h"
#include "..\Minecraft.World\JavaMath.h"
#include "Texture.h"
#include "CompassTexture.h"

CompassTexture *CompassTexture::instance = nullptr;

CompassTexture::CompassTexture() : StitchedTexture(L"compass",L"compass")
{
	instance = this;

	m_dataTexture = nullptr;
	m_iPad = XUSER_INDEX_ANY;

	rot = rota = 0.0;
}

CompassTexture::CompassTexture(int iPad, CompassTexture *dataTexture) : StitchedTexture(L"compass",L"compass")
{
	m_dataTexture = dataTexture;
	m_iPad = iPad;

	rot = rota = 0.0;
}

void CompassTexture::cycleFrames()
{
	Minecraft *mc = Minecraft::GetInstance();

	if (m_iPad >= 0 && m_iPad < XUSER_MAX_COUNT && mc->level != nullptr && mc->localplayers[m_iPad] != nullptr)
	{
		updateFromPosition(mc->localplayers[m_iPad]->level, mc->localplayers[m_iPad]->x, mc->localplayers[m_iPad]->z, mc->localplayers[m_iPad]->yRot, false, false);
	}
	else
	{
		frame = 1;
		updateFromPosition(nullptr, 0, 0, 0, false, true);
	}
}

void CompassTexture::updateFromPosition(Level *level, double x, double z, double yRot, bool noNeedle, bool instant)
{
	double rott = 0;
	if (level != nullptr && !noNeedle)
	{
		Pos *spawnPos = level->getSharedSpawnPos();
		double xa = spawnPos->x - x;
		double za = spawnPos->z - z;
		delete spawnPos;
		yRot = static_cast<int>(yRot) % 360;
		rott = -((yRot - 90) * PI / 180 - atan2(za, xa));
		if (!level->dimension->isNaturalDimension())
		{
			rott = Math::random() * PI * 2;
		}
	}

	if (instant)
	{
		rot = rott;
	}
	else
	{
		double rotd = rott - rot;
		while (rotd < -PI)
			rotd += PI * 2;
		while (rotd >= PI)
			rotd -= PI * 2;
		if (rotd < -1) rotd = -1;
		if (rotd > 1) rotd = 1;
		rota += rotd * 0.1;
		rota *= 0.8;
		rot += rota;
	}

	// 4J Stu - We share data with another texture
	if(m_dataTexture != nullptr)
	{
		int newFrame = static_cast<int>(((rot / (PI * 2)) + 1.0) * m_dataTexture->frames->size()) % m_dataTexture->frames->size();
		while (newFrame < 0)
		{
			newFrame = (newFrame + m_dataTexture->frames->size()) % m_dataTexture->frames->size();
		}
		if (newFrame != frame)
		{
			frame = newFrame;
			m_dataTexture->source->blit(this->x, this->y, m_dataTexture->frames->at(this->frame), rotated);
		}
	}
	else
	{
		int newFrame = static_cast<int>(((rot / (PI * 2)) + 1.0) * frames->size()) % frames->size();
		while (newFrame < 0)
		{
			newFrame = (newFrame + frames->size()) % frames->size();
		}
		if (newFrame != frame)
		{
			frame = newFrame;
			source->blit(this->x, this->y, frames->at(this->frame), rotated);
		}
	}
}

int CompassTexture::getSourceWidth() const
{
	return source->getWidth();
}

int CompassTexture::getSourceHeight() const
{
	return source->getHeight();
}

int CompassTexture::getFrames()
{
	if(m_dataTexture == nullptr)
	{
		return StitchedTexture::getFrames();
	}
	else
	{
		return m_dataTexture->getFrames();
	}
}

void CompassTexture::freeFrameTextures()
{
	if(m_dataTexture == nullptr)
	{
		StitchedTexture::freeFrameTextures();
	}
}

bool CompassTexture::hasOwnData()
{
	return m_dataTexture == nullptr;
}