aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/CactusTile.cpp
blob: c306f11a93594c2bd21465513a683a06878b8685 (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
#include "stdafx.h"
#include "net.minecraft.world.level.h"
#include "net.minecraft.world.level.material.h"
#include "net.minecraft.world.entity.h"
#include "net.minecraft.world.phys.h"
#include "net.minecraft.world.damagesource.h"
#include "net.minecraft.h"
#include "net.minecraft.world.h"
#include "CactusTile.h"

CactusTile::CactusTile(int id) : Tile(id, Material::cactus,isSolidRender())
{
	setTicking(true);
	iconTop = nullptr;
	iconBottom = nullptr;
}

void CactusTile::tick(Level *level, int x, int y, int z, Random *random)
{
	if (level->isEmptyTile(x, y + 1, z))
	{ 
		int height = 1;
		while (level->getTile(x, y - height, z) == id)
		{
			height++;
		}
		if (height < 3)
		{
			int age = level->getData(x, y, z);
			if (age == 15)
			{
				level->setTileAndUpdate(x, y + 1, z, id);
				level->setData(x, y, z, 0, Tile::UPDATE_NONE);
				neighborChanged(level, x, y + 1, z, id);
			}
			else
			{
				level->setData(x, y, z, age + 1, Tile::UPDATE_NONE);
			}
		}
	}
}

AABB *CactusTile::getAABB(Level *level, int x, int y, int z)
{
	float r = 1 / 16.0f;
	return AABB::newTemp(x + r, y, z + r, x + 1 - r, y + 1 - r, z + 1 - r);

}

AABB *CactusTile::getTileAABB(Level *level, int x, int y, int z)
{
	float r = 1 / 16.0f;
	return AABB::newTemp(x + r, y, z + r, x + 1 - r, y + 1, z + 1 - r);
}

Icon *CactusTile::getTexture(int face, int data)
{
	if (face == Facing::UP) return iconTop;
	if (face == Facing::DOWN) return iconBottom;
	else return icon;
}

bool CactusTile::isCubeShaped()
{
	return false;
}

bool CactusTile::isSolidRender(bool isServerLevel)
{
	return false;
}

int CactusTile::getRenderShape()
{
	return Tile::SHAPE_CACTUS;
}

bool CactusTile::mayPlace(Level *level, int x, int y, int z)
{
	if (!Tile::mayPlace(level, x, y, z)) return false;

	return canSurvive(level, x, y, z);
}

void CactusTile::neighborChanged(Level *level, int x, int y, int z, int type)
{
	if (!canSurvive(level, x, y, z))
	{
		level->destroyTile(x, y, z, true);
	}
}

bool CactusTile::canSurvive(Level *level, int x, int y, int z)
{
	if (level->getMaterial(x - 1, y, z)->isSolid()) return false;
	if (level->getMaterial(x + 1, y, z)->isSolid()) return false;
	if (level->getMaterial(x, y, z - 1)->isSolid()) return false;
	if (level->getMaterial(x, y, z + 1)->isSolid()) return false;
	int below = level->getTile(x, y - 1, z);
	return below == Tile::cactus_Id || below == Tile::sand_Id;
}

void CactusTile::entityInside(Level *level, int x, int y, int z, shared_ptr<Entity> entity)
{
	entity->hurt(DamageSource::cactus, 1);
}

void CactusTile::registerIcons(IconRegister *iconRegister)
{
	icon = iconRegister->registerIcon(L"cactus_side");
	iconTop = iconRegister->registerIcon(L"cactus_top");
	iconBottom = iconRegister->registerIcon(L"cactus_bottom");
}

bool CactusTile::shouldTileTick(Level *level, int x,int y,int z)
{
	return level->isEmptyTile(x, y + 1, z);
}