forked from Vogtinator/crafti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wheatrenderer.cpp
66 lines (52 loc) · 2.46 KB
/
wheatrenderer.cpp
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
#include <cstdlib>
#include "wheatrenderer.h"
void WheatRenderer::renderSpecialBlock(const BLOCK_WDATA block, GLFix x, GLFix y, GLFix z, Chunk &c)
{
BlockRenderer::renderBillboard(x / BLOCK_SIZE, y / BLOCK_SIZE, z / BLOCK_SIZE, terrain_atlas[8 + getBLOCKDATA(block)][5].current, c);
}
AABB WheatRenderer::getAABB(const BLOCK_WDATA block, GLFix x, GLFix y, GLFix z)
{
const GLFix height = BLOCK_SIZE / (max_growth + 1) * (getBLOCKDATA(block) + 1);
return {x, y, z, x + BLOCK_SIZE, y + height, z + BLOCK_SIZE};
}
void WheatRenderer::drawPreview(const BLOCK_WDATA /*block*/, TEXTURE &dest, int x, int y)
{
BlockRenderer::drawTextureAtlasEntry(*terrain_resized, terrain_atlas[12][5].resized, dest, x, y);
}
void WheatRenderer::tick(const BLOCK_WDATA block, int local_x, int local_y, int local_z, Chunk &c)
{
//If not irrigated, it's instantly withered (growth of 0)
if(!isIrrigated(local_x, local_y, local_z, c))
return c.setLocalBlock(local_x, local_y, local_z, getBLOCKWDATA(getBLOCK(block), 0));
const uint8_t growth = getBLOCKDATA(block);
if(growth == max_growth)
return;
//Grow only if lucky
if(rand() % 30 != 0)
return;
c.setLocalBlock(local_x, local_y, local_z, getBLOCKWDATA(getBLOCK(block), growth + 1));
}
void WheatRenderer::addedBlock(const BLOCK_WDATA /*block*/, int local_x, int local_y, int local_z, Chunk &c)
{
if(!isIrrigated(local_x, local_y, local_z, c))
return c.setLocalBlock(local_x, local_y, local_z, BLOCK_AIR);
}
PowerState WheatRenderer::powersSide(const BLOCK_WDATA /*block*/, BLOCK_SIDE /*side*/)
{
return PowerState::NotPowered;
}
const char *WheatRenderer::getName(const BLOCK_WDATA)
{
return "Wheat";
}
//To be irrigated, a block of water must be adjacent to the block underneath, which has to be dirt or grass
bool WheatRenderer::isIrrigated(const int local_x, const int local_y, const int local_z, const Chunk &c)
{
const BLOCK underneath = c.getGlobalBlockRelative(local_x, local_y - 1, local_z);
if(underneath != BLOCK_DIRT && underneath != BLOCK_GRASS)
return false;
return getBLOCK(c.getGlobalBlockRelative(local_x - 1, local_y - 1, local_z)) == BLOCK_WATER
|| getBLOCK(c.getGlobalBlockRelative(local_x + 1, local_y - 1, local_z)) == BLOCK_WATER
|| getBLOCK(c.getGlobalBlockRelative(local_x, local_y - 1, local_z - 1)) == BLOCK_WATER
|| getBLOCK(c.getGlobalBlockRelative(local_x, local_y - 1, local_z + 1)) == BLOCK_WATER;
}