Skip to content

Commit 05934f4

Browse files
committed
Add wheat
1 parent 0507b10 commit 05934f4

File tree

9 files changed

+107
-7
lines changed

9 files changed

+107
-7
lines changed

blockrenderer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "glassrenderer.h"
88
#include "leavesrenderer.h"
99
#include "torchrenderer.h"
10+
#include "wheatrenderer.h"
1011

1112
UniversalBlockRenderer global_block_renderer;
1213

@@ -143,6 +144,7 @@ UniversalBlockRenderer::UniversalBlockRenderer()
143144
map[BLOCK_TORCH] = std::make_shared<TorchRenderer>();
144145
map[BLOCK_WATER] = std::make_shared<FluidRenderer>(13, 12, "Water");
145146
map[BLOCK_LAVA] = std::make_shared<FluidRenderer>(13, 14, "Lava");
147+
map[BLOCK_WHEAT] = std::make_shared<WheatRenderer>();
146148

147149
auto flower_renderer = std::make_shared<BillboardRenderer>();
148150
flower_renderer->setEntry(0, 12, 0, "Red flower", BLOCK_SIZE, BLOCK_SIZE/2, BLOCK_SIZE);

chunk.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ void Chunk::render()
390390
}
391391
}
392392

393-
BLOCK_WDATA Chunk::getLocalBlock(const int x, const int y, const int z)
393+
BLOCK_WDATA Chunk::getLocalBlock(const int x, const int y, const int z) const
394394
{
395395
return blocks[x][y][z];
396396
}
@@ -433,7 +433,7 @@ void Chunk::changeLocalBlock(const int x, const int y, const int z, const BLOCK_
433433
global_block_renderer.addedBlock(block, x, y, z, *this);
434434
}
435435

436-
BLOCK_WDATA Chunk::getGlobalBlockRelative(int x, int y, int z)
436+
BLOCK_WDATA Chunk::getGlobalBlockRelative(const int x, const int y, const int z) const
437437
{
438438
if(inBounds(x, y, z))
439439
return getLocalBlock(x, y, z);

chunk.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ class Chunk
2828
void logic();
2929
void render();
3030
void setDirty() { render_dirty = true; }
31-
BLOCK_WDATA getLocalBlock(const int x, const int y, const int z);
31+
BLOCK_WDATA getLocalBlock(const int x, const int y, const int z) const;
3232
void setLocalBlock(const int x, const int y, const int z, const BLOCK_WDATA block);
3333
void changeLocalBlock(const int x, const int y, const int z, const BLOCK_WDATA block); //Calls removeBlock and addBlock
34-
BLOCK_WDATA getGlobalBlockRelative(int x, int y, int z);
34+
BLOCK_WDATA getGlobalBlockRelative(const int x, const int y, const int z) const;
3535
void setGlobalBlockRelative(const int x, const int y, const int z, const BLOCK_WDATA block);
3636
AABB &getAABB() { return aabb; }
3737
bool intersects(AABB &other);

main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ static const BLOCK_WDATA user_selectable[] = {
6565
getBLOCKWDATA(BLOCK_FLOWER, 1),
6666
getBLOCKWDATA(BLOCK_MUSHROOM, 0),
6767
getBLOCKWDATA(BLOCK_MUSHROOM, 1),
68+
getBLOCKWDATA(BLOCK_WHEAT, 0),
6869
BLOCK_SPIDERWEB,
6970
BLOCK_TORCH,
7071
BLOCK_CAKE

terrain.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ constexpr BLOCK BLOCK_MUSHROOM = 131; //Data: Mushroom type
5252
constexpr BLOCK BLOCK_DOOR = 132; //Data: (top: 1<<7) | BLOCK_SIDE
5353
constexpr BLOCK BLOCK_WATER = 133; //Data: range
5454
constexpr BLOCK BLOCK_LAVA = 134; //Data: range
55-
constexpr BLOCK BLOCK_SPECIAL_LAST = BLOCK_MUSHROOM;
55+
constexpr BLOCK BLOCK_WHEAT = 135; //Data: growth
56+
constexpr BLOCK BLOCK_SPECIAL_LAST = BLOCK_WHEAT;
5657

5758
constexpr uint8_t RANGE_WATER = 5;
5859
constexpr uint8_t RANGE_LAVA = 3;

wheatrenderer.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <cstdlib>
2+
3+
#include "wheatrenderer.h"
4+
5+
void WheatRenderer::renderSpecialBlock(const BLOCK_WDATA block, GLFix x, GLFix y, GLFix z, Chunk &c)
6+
{
7+
BlockRenderer::renderBillboard((x - c.absX()) / BLOCK_SIZE, (y - c.absY()) / BLOCK_SIZE, (z - c.absZ()) / BLOCK_SIZE, terrain_atlas[8 + getBLOCKDATA(block)][5].current, c);
8+
}
9+
10+
AABB WheatRenderer::getAABB(const BLOCK_WDATA block, GLFix x, GLFix y, GLFix z)
11+
{
12+
const GLFix height = BLOCK_SIZE / max_growth * getBLOCKDATA(block);
13+
14+
return {x, y, z, x + BLOCK_SIZE, y + height, z + BLOCK_SIZE};
15+
}
16+
17+
void WheatRenderer::drawPreview(const BLOCK_WDATA /*block*/, TEXTURE &dest, int x, int y)
18+
{
19+
BlockRenderer::drawTextureAtlasEntry(*terrain_resized, terrain_atlas[12][5].resized, true, dest, x, y);
20+
}
21+
22+
void WheatRenderer::tick(const BLOCK_WDATA block, int local_x, int local_y, int local_z, Chunk &c)
23+
{
24+
//If not irrigated, it's instantly withered (growth of 0)
25+
if(!isIrrigated(local_x, local_y, local_z, c))
26+
return c.setLocalBlock(local_x, local_y, local_z, getBLOCKWDATA(getBLOCK(block), 0));
27+
28+
//Grow only if lucky
29+
if(rand() % 30 != 0)
30+
return;
31+
32+
const uint8_t growth = getBLOCKDATA(block);
33+
if(growth == max_growth)
34+
return;
35+
36+
c.setLocalBlock(local_x, local_y, local_z, getBLOCKWDATA(getBLOCK(block), growth + 1));
37+
}
38+
39+
void WheatRenderer::addedBlock(const BLOCK_WDATA /*block*/, int local_x, int local_y, int local_z, Chunk &c)
40+
{
41+
if(!isIrrigated(local_x, local_y, local_z, c))
42+
return c.setLocalBlock(local_x, local_y, local_z, BLOCK_AIR);
43+
}
44+
45+
const char *WheatRenderer::getName(const BLOCK_WDATA)
46+
{
47+
return "Wheat";
48+
}
49+
50+
//To be irrigated, a block of water must be adjacent to the block underneath, which has to be dirt or grass
51+
bool WheatRenderer::isIrrigated(const int local_x, const int local_y, const int local_z, const Chunk &c)
52+
{
53+
const BLOCK underneath = c.getGlobalBlockRelative(local_x, local_y - 1, local_z);
54+
if(underneath != BLOCK_DIRT && underneath != BLOCK_GRASS)
55+
return false;
56+
57+
return getBLOCK(c.getGlobalBlockRelative(local_x - 1, local_y - 1, local_z)) == BLOCK_WATER
58+
|| getBLOCK(c.getGlobalBlockRelative(local_x + 1, local_y - 1, local_z)) == BLOCK_WATER
59+
|| getBLOCK(c.getGlobalBlockRelative(local_x, local_y - 1, local_z - 1)) == BLOCK_WATER
60+
|| getBLOCK(c.getGlobalBlockRelative(local_x, local_y - 1, local_z + 1)) == BLOCK_WATER;
61+
}

wheatrenderer.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef WHEATRENDERER_H
2+
#define WHEATRENDERER_H
3+
4+
#include "blockrenderer.h"
5+
6+
class WheatRenderer : public BlockRenderer
7+
{
8+
public:
9+
virtual void renderSpecialBlock(const BLOCK_WDATA block, GLFix x, GLFix y, GLFix z, Chunk &c) override;
10+
virtual void geometryNormalBlock(const BLOCK_WDATA, const int /*local_x*/, const int /*local_y*/, const int /*local_z*/, const BLOCK_SIDE /*side*/, Chunk &/*c*/) override {};
11+
virtual bool isOpaque(const BLOCK_WDATA /*block*/) override { return false; }
12+
virtual bool isObstacle(const BLOCK_WDATA /*block*/) override { return false; }
13+
virtual bool isOriented(const BLOCK_WDATA /*block*/) override { return false; }
14+
virtual bool isFullyOriented(const BLOCK_WDATA /*block*/) override { return false; }
15+
16+
virtual bool isBlockShaped(const BLOCK_WDATA /*block*/) override { return false; }
17+
virtual AABB getAABB(const BLOCK_WDATA, GLFix x, GLFix y, GLFix z) override;
18+
19+
virtual void drawPreview(const BLOCK_WDATA, TEXTURE &dest, int x, int y) override;
20+
21+
virtual bool action(const BLOCK_WDATA /*block*/, const int /*local_x*/, const int /*local_y*/, const int /*local_z*/, Chunk &/*c*/) override { return false; };
22+
virtual void tick(const BLOCK_WDATA /*block*/, int /*local_x*/, int /*local_y*/, int /*local_z*/, Chunk &/*c*/) override;
23+
virtual void updateBlock(const BLOCK_WDATA /*block*/, int /*local_x*/, int /*local_y*/, int /*local_z*/, Chunk &/*c*/) override {}
24+
virtual void addedBlock(const BLOCK_WDATA block, int local_x, int local_y, int local_z, Chunk &c) override;
25+
virtual void removedBlock(const BLOCK_WDATA /*block*/, int /*local_x*/, int /*local_y*/, int /*local_z*/, Chunk &/*c*/) override {}
26+
27+
virtual const char* getName(const BLOCK_WDATA) override;
28+
29+
bool isIrrigated(const int local_x, const int local_y, const int local_z, const Chunk &c);
30+
31+
protected:
32+
constexpr static int max_growth = 7;
33+
};
34+
35+
#endif // WHEATRENDERER_H

world.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ constexpr int getChunk(const int global)
3838
return global >> 3;
3939
}
4040

41-
BLOCK_WDATA World::getBlock(int x, int y, int z) const
41+
BLOCK_WDATA World::getBlock(const int x, const int y, const int z) const
4242
{
4343
int chunk_x = getChunk(x), chunk_y = getChunk(y), chunk_z = getChunk(z);
4444

world.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class World
1919
World();
2020
~World();
2121
void generateSeed();
22-
BLOCK_WDATA getBlock(int x, int y, int z) const;
22+
BLOCK_WDATA getBlock(const int x, const int y, const int z) const;
2323
void setBlock(const int x, const int y, const int z, const BLOCK_WDATA block);
2424
void changeBlock(const int x, const int y, const int z, const BLOCK_WDATA block);
2525
void setPosition(int x, int y, int z);

0 commit comments

Comments
 (0)