Skip to content
This repository has been archived by the owner on Feb 18, 2020. It is now read-only.

Commit

Permalink
Added trees and stone blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
kperdlich committed Sep 12, 2017
1 parent 0bf8d8c commit 6b0f292
Show file tree
Hide file tree
Showing 15 changed files with 155 additions and 14 deletions.
Binary file modified WoxelCraft.dol
Binary file not shown.
Binary file modified WoxelCraft.elf
Binary file not shown.
Binary file modified sprites/Dirt.tpl
Binary file not shown.
Binary file modified sprites/Grass.tpl
Binary file not shown.
Binary file modified sprites/Grass_Side.tpl
Binary file not shown.
Binary file added sprites/Leaf.tpl
Binary file not shown.
Binary file added sprites/Stone.tpl
Binary file not shown.
Binary file added sprites/Tree.tpl
Binary file not shown.
Binary file added sprites/Wood.tpl
Binary file not shown.
4 changes: 3 additions & 1 deletion src/entity/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,12 @@ void CPlayer::Update(float deltaSeconds)
Rotate( Vector3( 0, ROTATION_SPEED * deltaSeconds, 0 )); // left
}

float playerY = m_position.GetY();

Move(-(pad->GetNunchukAngleX()), -(pad->GetNunchukAngleY()), deltaSeconds);

// shity physics
Vector3 blockPositionUnderPlayer(m_position.GetX() + BLOCK_SIZE_HALF, 0.0f, m_position.GetZ() + BLOCK_SIZE_HALF);
Vector3 blockPositionUnderPlayer(m_position.GetX() + BLOCK_SIZE_HALF, playerY, m_position.GetZ() + BLOCK_SIZE_HALF);
Vector3 newPosition = m_pWorld->GetNewPlayerPosition(blockPositionUnderPlayer);
m_position.SetY(newPosition.GetY() + (2 * BLOCK_SIZE));

Expand Down
4 changes: 2 additions & 2 deletions src/renderer/BlockRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ void BlockRenderer::Draw()

for(auto it = m_positions->begin(); it != m_positions->end(); ++it)
{
Vector3 blockPosition = *(*it)->pBlockPosition;
BlockFaceVisibiltyVO blockRenderVO = *(*it)->pFaceVO;
Vector3& blockPosition = *(*it)->pBlockPosition;
BlockFaceVisibiltyVO& blockRenderVO = *(*it)->pFaceVO;

// see http://www.matrix44.net/cms/wp-content/uploads/2011/03/ogl_coord_object_space_cube.png
guVector vertices[8] =
Expand Down
65 changes: 65 additions & 0 deletions src/world/blocks/BlockManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,29 @@
#include "Dirt_tpl.h"
#include "Grass_tpl.h"
#include "Grass_Side_tpl.h"
#include "Stone_tpl.h"
#include "Wood_tpl.h"
#include "Leaf_tpl.h"
#include "Tree_tpl.h"

void BlockManager::LoadBlocks()
{
Texture* pDirtTexture = m_pTextureHandler->CreateTexture(Dirt_tpl, Dirt_tpl_size, BLOCK_TEXTURE_DIRT );
Texture* pGrassTexture = m_pTextureHandler->CreateTexture(Grass_tpl, Grass_tpl_size, BLOCK_TEXTURE_GRASS );
Texture* pGrassSideTexture = m_pTextureHandler->CreateTexture(Grass_Side_tpl, Grass_Side_tpl_size, BLOCK_TEXTURE_GRASS_SIDE );
Texture* pStoneTexture = m_pTextureHandler->CreateTexture(Stone_tpl, Stone_tpl_size, BLOCK_TEXTURE_STONE );
Texture* pWoodTexture = m_pTextureHandler->CreateTexture(Wood_tpl, Wood_tpl_size, BLOCK_TEXTURE_WOOD );
Texture* pLeafTexture = m_pTextureHandler->CreateTexture(Leaf_tpl, Leaf_tpl_size, BLOCK_TEXTURE_LEAF );
Texture* pTreeTexture = m_pTextureHandler->CreateTexture(Tree_tpl, Tree_tpl_size, BLOCK_TEXTURE_TREE );

// todo dirty workaround - have to fix texture handler
pDirtTexture->SetVisible(false);
pGrassTexture->SetVisible(false);
pGrassSideTexture->SetVisible(false);
pStoneTexture->SetVisible(false);
pWoodTexture->SetVisible(false);
pLeafTexture->SetVisible(false);
pTreeTexture->SetVisible(false);

std::map<const Texture*, std::vector<EBlockFaces>> dirtTextureMap =
{
Expand All @@ -53,6 +65,36 @@ void BlockManager::LoadBlocks()
}
};

std::map<const Texture*, std::vector<EBlockFaces>> stoneTextureMap =
{
{
pStoneTexture,
{
EBlockFaces::Left,
EBlockFaces::Right,
EBlockFaces::Front,
EBlockFaces::Back,
EBlockFaces::Top,
EBlockFaces::Bottom,
}
}
};

std::map<const Texture*, std::vector<EBlockFaces>> leafTextureMap =
{
{
pLeafTexture,
{
EBlockFaces::Left,
EBlockFaces::Right,
EBlockFaces::Front,
EBlockFaces::Back,
EBlockFaces::Top,
EBlockFaces::Bottom,
}
}
};

std::map<const Texture*, std::vector<EBlockFaces>> grassTextureMap =
{
{
Expand All @@ -78,9 +120,32 @@ void BlockManager::LoadBlocks()
}
};

std::map<const Texture*, std::vector<EBlockFaces>> woodTextureMap =
{
{
pTreeTexture,
{
EBlockFaces::Top,
EBlockFaces::Bottom
}
},
{
pWoodTexture,
{
EBlockFaces::Left,
EBlockFaces::Right,
EBlockFaces::Front,
EBlockFaces::Back
}
}
};

m_blocks.insert(std::pair< BlockType, Block* >( BlockType::AIR, new Block( BLOCK_SIZE_HALF, {}))); // no texture for air!
m_blocks.insert(std::pair< BlockType, Block* >( BlockType::DIRT, new Block( BLOCK_SIZE_HALF, dirtTextureMap )));
m_blocks.insert(std::pair< BlockType, Block* >( BlockType::GRASS, new Block( BLOCK_SIZE_HALF, grassTextureMap)));
m_blocks.insert(std::pair< BlockType, Block* >( BlockType::STONE, new Block( BLOCK_SIZE_HALF, stoneTextureMap)));
m_blocks.insert(std::pair< BlockType, Block* >( BlockType::WOOD, new Block( BLOCK_SIZE_HALF, woodTextureMap)));
m_blocks.insert(std::pair< BlockType, Block* >( BlockType::LEAF, new Block( BLOCK_SIZE_HALF, leafTextureMap)));
}

void BlockManager::UnloadBlocks()
Expand Down
9 changes: 8 additions & 1 deletion src/world/blocks/BlockManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,19 @@
#define BLOCK_TEXTURE_DIRT "BLOCK_TEXTURE_DIRT"
#define BLOCK_TEXTURE_GRASS "BLOCK_TEXTURE_GRASS"
#define BLOCK_TEXTURE_GRASS_SIDE "BLOCK_TEXTURE_GRASS_SIDE"
#define BLOCK_TEXTURE_STONE "BLOCK_TEXTURE_STONE"
#define BLOCK_TEXTURE_WOOD "BLOCK_TEXTURE_WOOD"
#define BLOCK_TEXTURE_LEAF "BLOCK_TEXTURE_LEAF"
#define BLOCK_TEXTURE_TREE "BLOCK_TEXTURE_TREE"

enum BlockType {

AIR,
DIRT,
GRASS
GRASS,
STONE,
WOOD,
LEAF
};

class BlockManager {
Expand Down
80 changes: 71 additions & 9 deletions src/world/chunk/Chunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,73 @@ void Chunk::Init(Vector3& position)

for (uint32_t y = 0; y < height; y++)
{
if ( y == height - 1 )
if ( y == height - 1 )
{
m_pBlocks[x][y][z] = BlockType::GRASS;
}
else
{
m_pBlocks[x][y][z] = BlockType::DIRT;
}
m_pBlocks[x][y][z] = BlockType::GRASS;
}
else if (y <= STONE_LEVEL )
{
m_pBlocks[x][y][z] = BlockType::STONE;
}
else
{
m_pBlocks[x][y][z] = BlockType::DIRT;
}
}
}
}

CreateTrees();
}

void Chunk::CreateTrees()
{
srand (time(NULL));
uint32_t x = 2 + (rand() % (CHUNK_SIZE_X - 4)); // value range 2 - 14
srand (time(NULL));
uint32_t z = 2 + (rand() % (CHUNK_SIZE_Z - 4));

double xWorld = (m_pCenterPosition->GetX() - (CHUNK_BLOCK_SIZE_X / 2) + (x * BLOCK_SIZE));
double zWorld = (m_pCenterPosition->GetZ() - (CHUNK_BLOCK_SIZE_Z / 2) + (z * BLOCK_SIZE));

double noise = m_pWorldManager->GetNoise().GetHeight(xWorld, zWorld);

uint32_t y = (uint32_t) MathHelper::Clamp(CHUNK_SIZE_Y * noise, 1.0, CHUNK_SIZE_Y);


if ( y < CHUNK_SIZE_Y - TREE_HIGHT)
{
if ( m_pBlocks[x][y-1][z] == BlockType::GRASS)
{

for (int8_t i = -2; i <= 2; i++)
{
for (int8_t j = -2; j <= 2; j++)
{
m_pBlocks[x+i][y+3][z+j] = BlockType::LEAF;
m_pBlocks[x+i][y+4][z+j] = BlockType::LEAF;
}
}

for (int8_t i = -1; i <= 1; i++)
{
for (int8_t j = -1; j <= 1; j++)
{
m_pBlocks[x+i][y+5][z+j] = BlockType::LEAF;
m_pBlocks[x+i][y+6][z+j] = ((i % 2) == 0) || ((j % 2) == 0) ? (BlockType::LEAF) : (BlockType::AIR);
}
}


for (uint32_t i = 0; i < 3; i++)
{
m_pBlocks[x][y+i][z] = BlockType::WOOD;
}
}
}
}


void Chunk::UpdateChunkNeighbors()
{
Vector3 leftChunkPos(m_pCenterPosition->GetX() - CHUNK_BLOCK_SIZE_X, m_pCenterPosition->GetY(), m_pCenterPosition->GetZ());
Expand Down Expand Up @@ -458,13 +512,21 @@ BlockType Chunk::GetBlockTypeByWorldPosition(const Vector3& worldPosition) const

Vector3 Chunk::ValidatePhysicalPosition(const Vector3& position) const
{
Vector3 pos = GetBlockPositionByWorldPosition(position);
Vector3 pos = GetBlockPositionByWorldPosition(position);
float currentPos = CHUNK_BLOCK_SIZE_Y - (BLOCK_SIZE);
bool bValidated = false;
do
{
pos.SetY(currentPos);
bValidated = (GetBlockTypeByWorldPosition(pos) != BlockType::AIR) || (currentPos == 0);
bValidated = (GetBlockTypeByWorldPosition(pos) != BlockType::AIR) || (currentPos == 0);
/* quick physics fix for the tree update
* Player shouldn't jump higher than blocksize
*/
if (bValidated)
{
double distance = position.GetY() - currentPos;
bValidated = distance > (-BLOCK_SIZE);
}
currentPos -= BLOCK_SIZE;

} while(!bValidated);
Expand Down
7 changes: 6 additions & 1 deletion src/world/chunk/Chunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
#define CHUNK_BLOCK_SIZE_Y (BLOCK_SIZE * CHUNK_SIZE_Y)
#define CHUNK_BLOCK_SIZE_Z (BLOCK_SIZE * CHUNK_SIZE_Z)

#define STONE_LEVEL 2
#define TREE_HIGHT 6

struct Vec3i {
uint32_t m_x, m_y, m_z;
};
Expand Down Expand Up @@ -74,8 +77,10 @@ class Chunk {
void BuildBlockRenderList();
bool IsBlockVisible(uint32_t iX, uint32_t iY, uint32_t iZ, BlockFaceVisibiltyVO* &pFaceVO );
Vec3i GetLocalBlockPositionByWorldPosition(const Vector3& blockWorldPosition) const;

Vector3 LocalPositionToGlobalPosition(const Vec3i& localPosition) const;
void CreateTrees();



private:
bool m_isDirty;
Expand Down

0 comments on commit 6b0f292

Please sign in to comment.