Skip to content

Commit

Permalink
water finally kinda works
Browse files Browse the repository at this point in the history
  • Loading branch information
Kade-github committed Apr 21, 2024
1 parent 4b873a9 commit e96d96f
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 47 deletions.
16 changes: 8 additions & 8 deletions src/Game/Data/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ bool Data::Region::doesBlockExistInRange(int x, int y, int z, int type, int rang
{
for (int _y = 0; _y < CHUNK_HEIGHT; _y++)
{
if (c.blocks[_x][_z][_y] == type)
if (c.bChunk.blocks[_x][_z][_y] == type)
{
if (abs(c.x + _x - x) < range && abs(c.z + _z - z) < range && abs(_y - y) < range)
return true;
Expand Down Expand Up @@ -240,7 +240,7 @@ Data::Chunk Data::Region::generateChunk(int x, int z)
{
for (int _y = 0; _y < CHUNK_HEIGHT; _y++)
{
chunk.blocks[_x][_z][_y] = 0;
chunk.bChunk.blocks[_x][_z][_y] = 0;
}
}
}
Expand All @@ -267,11 +267,11 @@ Data::Chunk Data::Region::generateChunk(int x, int z)
for (int _y = rY; _y > -1; _y--)
{
if (_y == rY) // grass
chunk.blocks[_x][_z][_y] = GRASS;
chunk.bChunk.blocks[_x][_z][_y] = GRASS;
else if (_y > rY - 5) // dirt
chunk.blocks[_x][_z][_y] = DIRT;
chunk.bChunk.blocks[_x][_z][_y] = DIRT;
else // stone
chunk.blocks[_x][_z][_y] = STONE;
chunk.bChunk.blocks[_x][_z][_y] = STONE;
}
}
}
Expand Down Expand Up @@ -300,19 +300,19 @@ void Data::Region::generateStructures()
{
for (int _y = CHUNK_HEIGHT - 1; _y > -1; _y--)
{
if (c.blocks[_x][_z][_y] <= 0)
if (c.bChunk.blocks[_x][_z][_y] <= 0)
continue;

// trees

if (c.blocks[_x][_z][_y] == GRASS)
if (c.bChunk.blocks[_x][_z][_y] == GRASS)
{
int _rx = c.x + _x;
int _rz = c.z + _z;

if (rand() % 100 < 2)
{
struct_tree.Create(_rx,_rz,_y, c, this);
struct_tree.Create(_rx,_rz,_y - 1, c, this);
}
}
}
Expand Down
18 changes: 13 additions & 5 deletions src/Game/Data/World.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,29 @@ namespace Data
MSGPACK_DEFINE_ARRAY(blocks);
};

struct Chunk
struct BlockChunk
{
bool isGenerated = false;
uint8_t blocks[CHUNK_SIZE][CHUNK_SIZE][CHUNK_HEIGHT];

MSGPACK_DEFINE_ARRAY(blocks);
};

class Chunk
{
public:
bool isGenerated = false;
BlockChunk bChunk;
int32_t x, z;
DataChunk data;

void placeBlock(int x, int y, int z, uint8_t block)
{
blocks[x][z][y] = block;
bChunk.blocks[x][z][y] = block;
}

void removeBlock(int x, int y, int z)
{
blocks[x][z][y] = 0;
bChunk.blocks[x][z][y] = 0;
}

BlockData getBlockData(int x, int y, int z)
Expand Down Expand Up @@ -142,7 +150,7 @@ namespace Data
}


MSGPACK_DEFINE_ARRAY(blocks, x, z, data);
MSGPACK_DEFINE_ARRAY(bChunk, x, z, data);
};

struct Region {
Expand Down
15 changes: 12 additions & 3 deletions src/Game/Objects/Base/Blocks/Water.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,28 @@ bool Water::DoesBlockExist(glm::vec3 _pos)
Chunk* c = WorldManager::instance->GetChunk(_pos.x, _pos.z);

if (c == nullptr)
return false;
return true;

int type = c->GetBlock(_pos.x, _pos.y, _pos.z);
return type > 0;
return type > 0 && type != WATER;
}

void Water::PlaceWater(glm::vec3 _pos, int _strength)
{
if (_pos.y < position.y)
_strength += 2;

if (_strength <= 0)
return;

Chunk* c = WorldManager::instance->GetChunk(_pos.x, _pos.z);

if (c == nullptr)
return;

if (c->DoesBlockExist(_pos.x, _pos.y, _pos.z))
return;

Data::BlockData d = data;
d.tags.clear();

Expand All @@ -109,7 +118,7 @@ void Water::PlaceWater(glm::vec3 _pos, int _strength)

void Water::Update(int tick) // water functionality
{
if (strength == 0 || tick % 10 != 0 || currentChunk == nullptr)
if (strength == 0 || tick % 8 != 0 || currentChunk == nullptr)
return;

std::vector<glm::vec3> freeSpaces = GetFreeSpaces(position);
Expand Down
56 changes: 25 additions & 31 deletions src/Game/Objects/Base/Chunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ int Chunk::GetBlock(float x, float y, float z)
if (y < 0 || y > CHUNK_HEIGHT - 1)
return 0;

return myData.blocks[_x][_z][(int)y];
return myData.bChunk.blocks[_x][_z][(int)y];
}

int Chunk::GetHighestBlock(float x, float z)
Expand Down Expand Up @@ -105,7 +105,7 @@ int Chunk::GetHighestBlock(float x, float z)

for (int y = CHUNK_HEIGHT - 1; y > -1; y--)
{
if (myData.blocks[_x][_z][y] > 0)
if (myData.bChunk.blocks[_x][_z][y] > 0)
return y;
}

Expand Down Expand Up @@ -242,16 +242,16 @@ int Chunk::GetBlockNoCheck(float x, float y, float z)
if (y < 0 || y > CHUNK_HEIGHT - 1)
return 0;

return myData.blocks[_x][_z][(int)y];
return myData.bChunk.blocks[_x][_z][(int)y];
}

int Chunk::GetBlockRaw(float x, float y, float z)
{
int _x = x;
int _z = z;

return myData.blocks[_x][_z][(int)y];
return myData.bChunk.blocks[_x][_z][(int)y];

}

bool Chunk::DoesBlockExist(float x, float y, float z)
Expand All @@ -264,46 +264,37 @@ bool Chunk::DoesBlockExist(float x, float y, float z)

void Chunk::CreateOtherSubchunks(float x, float y, float z, glm::vec3 w)
{
Gameplay* gp = (Gameplay*)Game::instance->currentScene;

if (w.x == 0)
{
Chunk* c = left;

if (c != nullptr)
{
gp->QueueLoadBlocks(c);
}
c->modified = true;
}

if (w.x == CHUNK_SIZE - 1)
{
Chunk* c = right;

if (c != nullptr)
{
gp->QueueLoadBlocks(c);
}
c->modified = true;
}

if (w.z == 0)
{
Chunk* c = back;

if (c != nullptr)
{
gp->QueueLoadBlocks(c);
}
c->modified = true;
}

if (w.z == CHUNK_SIZE - 1)
{
Chunk* c = front;

if (c != nullptr)
{
gp->QueueLoadBlocks(c);
}
c->modified = true;
}
}

Expand Down Expand Up @@ -332,12 +323,9 @@ void Chunk::ModifyBlock(float x, float y, float z, int id)
else
myData.placeBlock(w.x, w.y, w.z, id);

gp->QueueLoadBlocks(this);
modified = true;

CreateOtherSubchunks(x, y, z, w);

LightingManager::GetInstance()->RefreshShadows();

}

void Chunk::PlaceBlock(float x, float y, float z, Block* b)
Expand Down Expand Up @@ -365,13 +353,9 @@ void Chunk::PlaceBlock(float x, float y, float z, Block* b)

delete b;

// check if we need to update other chunks

gp->QueueLoadBlocks(this);
modified = true;

CreateOtherSubchunks(x, y, z, w);

LightingManager::GetInstance()->RefreshShadows();
}

// this is made confusingly. I'm sorry.
Expand Down Expand Up @@ -564,7 +548,7 @@ Data::Chunk Chunk::GetChunkData()
for (int x = 0; x < CHUNK_SIZE; x++)
{
for (int z = 0; z < CHUNK_SIZE; z++)
c.blocks[x][z][y] = myData.blocks[x][z][y];
c.bChunk.blocks[x][z][y] = myData.bChunk.blocks[x][z][y];
}
continue;
}
Expand All @@ -574,9 +558,9 @@ Data::Chunk Chunk::GetChunkData()
for (int z = 0; z < CHUNK_SIZE; z++)
{
if (sbc->blocks[x][z] != nullptr)
c.blocks[x][z][y] = sbc->blocks[x][z]->type;
c.bChunk.blocks[x][z][y] = sbc->blocks[x][z]->type;
else
c.blocks[x][z][y] = 0;
c.bChunk.blocks[x][z][y] = 0;
}
}
}
Expand Down Expand Up @@ -677,7 +661,7 @@ subChunk* Chunk::CreateSubChunk(int y)
{
for (int z = 0; z < CHUNK_SIZE; z++)
{
int id = myData.blocks[x][z][y];
int id = myData.bChunk.blocks[x][z][y];

if (id <= 0)
continue;
Expand Down Expand Up @@ -1021,6 +1005,16 @@ void Chunk::DrawShadows()

void Chunk::UpdateChunk(int tick)
{
if (modified)
{
Gameplay* gp = (Gameplay*)Game::instance->currentScene;

gp->QueueLoadBlocks(this);

LightingManager::GetInstance()->RefreshShadows();
modified = false;
}

for (int i = 0; i < subChunks.size(); i++)
{
subChunk* sbc = subChunks[i];
Expand Down
2 changes: 2 additions & 0 deletions src/Game/Objects/Base/Chunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class Chunk : public GameObject
bool isBeingLoaded = false;
bool isShadowLoaded = false;

bool modified = false;

bool pleaseRender = false;

bool isLoaded = false;
Expand Down

0 comments on commit e96d96f

Please sign in to comment.