Skip to content

Commit

Permalink
chunk updates are way simpler than I thought
Browse files Browse the repository at this point in the history
  • Loading branch information
Kade-github committed Apr 21, 2024
1 parent a1d6fda commit 4b873a9
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 183 deletions.
7 changes: 7 additions & 0 deletions src/Engine/Logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class Logging
std::chrono::time_point<std::chrono::system_clock> _start;
std::ofstream _file;
public:
std::vector<std::string> logs;

Logging(std::string path)
{
_file.open(path);
Expand Down Expand Up @@ -48,6 +50,11 @@ class Logging
_file << log << std::endl;

_file.flush();

logs.push_back(log);

if (logs.size() > 10)
logs.erase(logs.begin());
}

};
Expand Down
11 changes: 11 additions & 0 deletions src/Engine/Objects/2DCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ void Camera2D::MouseMove(float x, float y)

void Camera2D::KeyPress(int key)
{
if (key == GLFW_KEY_KP_0)
debug = !debug;

for (int i = 0; i < objects.size(); i++)
{
GameObject2D* object = objects[i];
Expand Down Expand Up @@ -297,6 +300,14 @@ void Camera2D::Draw()

DrawDebugText("FPS: " + format, glm::vec2(4, _h - 28), 24);

if (debug)
{
for(int i = 0; i < Game::instance->log->logs.size(); i++)
{
DrawDebugText(Game::instance->log->logs[i], glm::vec2(4, _h - 52 - (i * 24)), 24);
}
}

UpdateFramebuffer();

glViewport(0, 0, _rW, _rH);
Expand Down
2 changes: 2 additions & 0 deletions src/Engine/Objects/2DCamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class Camera2D : public GameObject
int _lastId = 0;
bool setProject = false;
public:
bool debug = false;

float _w, _h;
float _rW, _rH;
unsigned int s_vao, s_vbo;
Expand Down
78 changes: 42 additions & 36 deletions src/Game/Objects/Base/Blocks/Water.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "Water.h"

#include <Game.h>
#include "../Chunk.h"
#include "../../../WorldManager.h"

Expand All @@ -11,19 +11,13 @@ Water::Water(glm::vec3 _position, int strength, bool isSource) : Block(_position

transparent = true;

strength = strength;
this->strength = strength;
source = isSource;

data.tags.clear();

Data::DataTag source = Data::DataTag();
source.Assemble("source", "false");

Data::DataTag str = Data::DataTag();
str.Assemble("strength", std::to_string(strength));

data.tags.push_back(str);
data.tags.push_back(source);
data.AddTag("strength", std::to_string(strength));
data.AddTag("source", isSource ? "true" : "false");

currentChunk = WorldManager::instance->GetChunk(position.x, position.z); // cache this
}
Expand All @@ -32,35 +26,56 @@ std::vector<glm::vec3> Water::GetFreeSpaces(glm::vec3 _pos)
{
std::vector<glm::vec3> freeSpaces = {};

// Get our current chunk
if (source)
{
if (!DoesBlockExist(_pos + glm::vec3(0, 0, 1)))
freeSpaces.push_back(_pos + glm::vec3(0, 0, 1));

if (!DoesBlockExist(_pos - glm::vec3(0, 1, 0)) && !source)
return {};
if (!DoesBlockExist(_pos + glm::vec3(0, 0, -1)))
freeSpaces.push_back(_pos + glm::vec3(0, 0, -1));

if (!DoesBlockExist(_pos + glm::vec3(0, 0, 1)))
freeSpaces.push_back(_pos + glm::vec3(0, 0, 1));
if (!DoesBlockExist(_pos + glm::vec3(1, 0, 0)))
freeSpaces.push_back(_pos + glm::vec3(1, 0, 0));

if (!DoesBlockExist(_pos + glm::vec3(0, 0, -1)))
freeSpaces.push_back(_pos + glm::vec3(0, 0, -1));
if (!DoesBlockExist(_pos + glm::vec3(-1, 0, 0)))
freeSpaces.push_back(_pos + glm::vec3(-1, 0, 0));

if (!DoesBlockExist(_pos + glm::vec3(1, 0, 0)))
freeSpaces.push_back(_pos + glm::vec3(1, 0, 0));
if (!DoesBlockExist(_pos + glm::vec3(0, -1, 0)))
freeSpaces.push_back(_pos + glm::vec3(0, -1, 0));
}
else
{
if (!DoesBlockExist(_pos + glm::vec3(0, -1, 0)))
{
freeSpaces.push_back(_pos + glm::vec3(0, -1, 0));
return freeSpaces;
}

if (!DoesBlockExist(_pos + glm::vec3(-1, 0, 0)))
freeSpaces.push_back(_pos + glm::vec3(-1, 0, 0));
if (!DoesBlockExist(_pos + glm::vec3(0, 0, 1)))
freeSpaces.push_back(_pos + glm::vec3(0, 0, 1));

if (!DoesBlockExist(_pos + glm::vec3(0, 0, -1)))
freeSpaces.push_back(_pos + glm::vec3(0, 0, -1));

if (!DoesBlockExist(_pos + glm::vec3(1, 0, 0)))
freeSpaces.push_back(_pos + glm::vec3(1, 0, 0));

if (!DoesBlockExist(_pos + glm::vec3(-1, 0, 0)))
freeSpaces.push_back(_pos + glm::vec3(-1, 0, 0));
}

return freeSpaces;
}

bool Water::DoesBlockExist(glm::vec3 _pos)
{
if (currentChunk == nullptr)
return false;

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

if (c == nullptr)
return false;

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

void Water::PlaceWater(glm::vec3 _pos, int _strength)
Expand All @@ -85,6 +100,8 @@ void Water::PlaceWater(glm::vec3 _pos, int _strength)

b->data = d;

Game::instance->log->Write((source ? "[Source Block] [" : " [") + std::to_string(strength) + "] Placed water at " + std::to_string(_pos.x) + ", " + std::to_string(_pos.y) + ", " + std::to_string(_pos.z));

c->PlaceBlock(_pos.x, _pos.y, _pos.z, b);

changedBlocks = true;
Expand All @@ -97,17 +114,6 @@ void Water::Update(int tick) // water functionality

std::vector<glm::vec3> freeSpaces = GetFreeSpaces(position);

if (freeSpaces.size() == 0)
{
// go down
if (currentChunk->DoesBlockExist(position.x, position.y - 1, position.z))
return;

PlaceWater(glm::vec3(position.x, position.y - 1, position.z), strength - 1);

return;
}

for (glm::vec3 freeSpace : freeSpaces)
{
Chunk* c = WorldManager::instance->GetChunk(freeSpace.x, freeSpace.z);
Expand Down
Loading

0 comments on commit 4b873a9

Please sign in to comment.