Biome Classes

This commit is contained in:
Namonay
2025-06-01 18:36:48 +02:00
parent e1c4698a56
commit 0ec004b145
4 changed files with 133 additions and 0 deletions

64
Application/Biome.cpp git.filemode.normal_file
View File

@@ -0,0 +1,64 @@
#include <Biome.h>
#include "Block.h"
#include "Chunk.h"
#include "Maths/Vec3.h"
#include <utility>
Biome::Biome(std::uint32_t filler, std::uint32_t water_level, std::map<std::uint32_t, std::pair<BlockPlacementType, std::vector<BlockType>>> blocks): filler(filler), water_level(water_level), c_blockmap(blocks)
{
for (const auto& [height, BlockPlacement] : blocks)
{
const auto& [PlacementType, Blocks] = BlockPlacement;
if (PlacementType == BlockPlacementType::PseudoRandom && Blocks.size() < 2)
Scop::FatalError("A biome have a Multiple block placement but less than 2 blocks was given");
}
}
const std::array<std::uint32_t, CHUNK_SIZE.y> Biome::GetBiomeBlocks(const std::uint32_t height, Scop::Vec2i pos)
{
std::array<std::uint32_t, CHUNK_SIZE.y> data;
auto it = c_blockmap.lower_bound(height);
if (it == c_blockmap.end())
Scop::FatalError("Biome declaration does not have a value for a certain generated height");
for(std::uint32_t y = 0; y < std::min(height, CHUNK_SIZE.y); y++)
{
if(y > std::min(height, CHUNK_SIZE.y) - 2)
{
data[y] = static_cast<std::uint32_t>(BlockType::Dirt);
continue;
const auto& [placementType, blockTypes] = it->second;
switch (static_cast<std::uint8_t>(placementType))
{
case(static_cast<std::uint8_t>(BlockPlacementType::Simple)):
{
data[y] = static_cast<std::uint32_t>(blockTypes.front());
break;
}
case(static_cast<std::uint8_t>(BlockPlacementType::PseudoRandom)):
{
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>(blockTypes.at(0));
else
data[y] = static_cast<std::uint32_t>(blockTypes.at(1));
break;
}
default:
data[y] = static_cast<std::uint32_t>(BlockType::Stone);
}
}
else
{
data[y] = filler;
}
}
for(std::uint32_t y = 0; y < water_level; y++)
{
if(data[y] == static_cast<std::uint32_t>(BlockType::Air))
data[y] = static_cast<std::uint32_t>(BlockType::Water);
}
return data;
}

30
Application/Biome.h git.filemode.normal_file
View File

@@ -0,0 +1,30 @@
#ifndef BIOME_H
#define BIOME_H
#include <Block.h>
#include "Chunk.h"
#include "Maths/Vec2.h"
#include <map>
#include <utility>
#include <vector>
enum class BlockPlacementType : std::uint8_t
{
Simple = 0,
PseudoRandom,
};
class Biome
{
public:
Biome(std::uint32_t filler, std::uint32_t water_level, std::map<std::uint32_t, std::pair<BlockPlacementType, std::vector<BlockType>>> blocks);
~Biome() = default;
const std::array<std::uint32_t, CHUNK_SIZE.y> GetBiomeBlocks(const std::uint32_t height, Scop::Vec2i pos);
private:
const std::uint32_t filler;
const std::uint32_t water_level;
const std::map<std::uint32_t, std::pair<BlockPlacementType, std::vector<BlockType>>> c_blockmap;
};
#endif

6
Application/BiomeCollection.cpp git.filemode.normal_file
View File

@@ -0,0 +1,6 @@
#include <BiomeCollection.h>
const std::array<std::uint32_t, CHUNK_SIZE.y> BiomeCollection::GetBiomeBlocks(std::string biome, std::uint32_t height, Scop::Vec2i pos) const
{
return m_collection.at(biome)->GetBiomeBlocks(height, pos);
}

33
Application/BiomeCollection.h git.filemode.normal_file
View File

@@ -0,0 +1,33 @@
#ifndef BIOMECOLLECTION_H
#define BIOMECOLLECTION_H
#include <memory>
#include <unordered_map>
#include <string>
#include "Biome.h"
#include "Chunk.h"
#include "Maths/Vec2.h"
class BiomeCollection
{
public:
inline BiomeCollection()
{
m_collection.emplace("grassland", std::make_unique<Biome>(Biome(
static_cast<std::uint32_t>(BlockType::Stone),
0,
{
{20, {BlockPlacementType::Simple, {BlockType::Sand}}},
{255, {BlockPlacementType::Simple, {BlockType::Snow}}}
})
));
};
const std::array<std::uint32_t, CHUNK_SIZE.y> GetBiomeBlocks(std::string biome, std::uint32_t height, Scop::Vec2i pos) const;
~BiomeCollection() = default;
private:
std::unordered_map<std::string, std::unique_ptr<Biome>> m_collection;
};
#endif