Refactor logic from Noise to NoiseCollection

This commit is contained in:
Namonay
2025-05-31 20:07:52 +02:00
parent 6ebd51b76e
commit f8ff6be0e6
5 changed files with 58 additions and 35 deletions

View File

@@ -1,4 +1,6 @@
#include "Block.h"
#include "Chunk.h"
#include "Maths/Constants.h"
#include "Maths/Vec2.h"
#include <NoiseCollection.h>
#include <Noise.h>
@@ -16,5 +18,46 @@ Noise* NoiseCollection::GetNoise(const std::string& key) const
const std::array<std::uint32_t, CHUNK_SIZE.y> NoiseCollection::GetBlocks(Scop::Vec2i pos)
{
return m_collection["terrain"]->GetHeight(pos);
const std::uint32_t height = m_collection["terrain"]->Perlin2D(pos.x, pos.y) + ARTIFICIAL_ELEVATION;
std::array<std::uint32_t, CHUNK_SIZE.y> data;
std::memset(data.data(), static_cast<std::uint32_t>(BlockType::Air), data.size() * sizeof(std::uint32_t));
for(std::uint32_t y = 0; y < std::min(height, CHUNK_SIZE.y); y++)
{
// const std::uint32_t value = Perlin3D(pos.x, y, pos.y);
if(y > std::min(height, CHUNK_SIZE.y) - 2)
{
if(height <= 23 + ARTIFICIAL_ELEVATION)
data[y] = static_cast<std::uint32_t>(BlockType::Sand);
else if(height < 125 + ARTIFICIAL_ELEVATION)
data[y] = static_cast<std::uint32_t>(BlockType::Grass);
else if (height < 132 + ARTIFICIAL_ELEVATION)
{
float weight = sin(2 * (pos.x * pos.y)) + sin(Scop::Pi<float>() * (pos.x * pos.y));
if (weight > 0.0f)
data[y] = static_cast<std::uint32_t>(BlockType::Grass);
else
data[y] = static_cast<std::uint32_t>(BlockType::SnowyGrass);
}
else if (height < 140 + ARTIFICIAL_ELEVATION)
{
float weight = sin(2 * (pos.x * pos.y)) + sin(Scop::Pi<float>() * (pos.x * pos.y));
if (weight > 0.0f)
data[y] = static_cast<std::uint32_t>(BlockType::Snow);
else
data[y] = static_cast<std::uint32_t>(BlockType::SnowyGrass);
}
else
data[y] = static_cast<std::uint32_t>(BlockType::Snow);
}
else
data[y] = static_cast<std::uint32_t>(BlockType::Stone);
}
for(std::uint32_t y = 0; y < ARTIFICIAL_ELEVATION + WATER_LEVEL; y++)
{
if(data[y] == static_cast<std::uint32_t>(BlockType::Air))
data[y] = static_cast<std::uint32_t>(BlockType::Water);
}
return data;
}