summaryrefslogtreecommitdiffstats
path: root/source/FallingBlock.cpp
blob: 12362009df144b952bc7cbbbda66e2d2da201463 (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
#include "Globals.h"

#include "FallingBlock.h"
#include "World.h"
#include "ClientHandle.h"
#include "Simulator/SandSimulator.h"





cFallingBlock::cFallingBlock(const Vector3i & a_BlockPosition, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) :
	super(etFallingBlock, a_BlockPosition.x + 0.5f, a_BlockPosition.y + 0.5f, a_BlockPosition.z + 0.5f),
	m_BlockType(a_BlockType),
	m_BlockMeta(a_BlockMeta),
	m_OriginalPosition(a_BlockPosition)
{
}





void cFallingBlock::Initialize(cWorld * a_World)
{
	super::Initialize(a_World);
	a_World->BroadcastSpawn(*this);
}





void cFallingBlock::SpawnOn(cClientHandle & a_ClientHandle)
{
	a_ClientHandle.SendSpawnFallingBlock(*this);
}





void cFallingBlock::Tick(float a_Dt, MTRand & a_TickRandom)
{
	float MilliDt = a_Dt * 0.001f;
	m_Speed.y -= MilliDt * 9.8f;
	m_Pos.y += m_Speed.y * MilliDt;

	// GetWorld()->BroadcastTeleportEntity(*this);  // Test position
	
	int BlockX = (int)m_OriginalPosition.x;
	int BlockY = (int)(m_Pos.y - 0.5);
	int BlockZ = (int)m_OriginalPosition.z;
	
	if (BlockY < 0)
	{
		// Fallen out of this world, just continue falling until out of sight, then destroy:
		if (BlockY < 100)
		{
			Destroy();
		}
		return;
	}
	
	if (BlockY < cChunkDef::Height - 1)
	{
		BLOCKTYPE BlockBelow = GetWorld()->GetBlock(BlockX, BlockY, BlockZ);
		if (
			cSandSimulator::DoesBreakFallingThrough(BlockBelow) ||  // Fallen onto a block that breaks this into pickups (e. g. half-slab)
			!cSandSimulator::CanContinueFallThrough(BlockBelow)     // Fallen onto a solid block
		)
		{
			cSandSimulator::FinishFalling(m_World, BlockX, BlockY + 1, BlockZ, m_BlockType, m_BlockMeta);
			Destroy();
			return;
		}
	}
}