mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-09 02:07:04 +08:00
refactor: biome build (#5)
* refactor: rename Biome to BiomeType * feat: add biome builder
This commit is contained in:
18
src/gameplay/builders/biome_builder.cpp
Normal file
18
src/gameplay/builders/biome_builder.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#include "Cubed/gameplay/builders/biome_builder.hpp"
|
||||
|
||||
#include "Cubed/gameplay/chunk.hpp"
|
||||
#include "Cubed/gameplay/chunk_generator.hpp"
|
||||
namespace Cubed {
|
||||
void BiomeBuilder::build_bottom() {
|
||||
ChunkGenerator& chunk_generator = get_chunk_generator();
|
||||
Chunk& chunk = chunk_generator.chunk();
|
||||
auto& m_blocks = chunk.blocks();
|
||||
for (int x = 0; x < CHUCK_SIZE; x++) {
|
||||
for (int y = 0; y < 5; y++) {
|
||||
for (int z = 0; z < CHUCK_SIZE; z++) {
|
||||
m_blocks[Chunk::get_index(x, y, z)] = 3;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace Cubed
|
||||
55
src/gameplay/builders/desert_builder.cpp
Normal file
55
src/gameplay/builders/desert_builder.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#include "Cubed/gameplay/builders/desert_builder.hpp"
|
||||
|
||||
#include "Cubed/gameplay/chunk.hpp"
|
||||
#include "Cubed/gameplay/chunk_generator.hpp"
|
||||
namespace Cubed {
|
||||
DesertBuilder::DesertBuilder(ChunkGenerator& chunk_generator)
|
||||
: m_chunk_generator(chunk_generator) {}
|
||||
|
||||
void DesertBuilder::build_biome() {
|
||||
BiomeBuilder::build_bottom();
|
||||
build_blocks();
|
||||
};
|
||||
|
||||
void DesertBuilder::build_blocks() {
|
||||
auto& m_chunk = m_chunk_generator.chunk();
|
||||
auto& m_blocks = m_chunk.blocks();
|
||||
auto& m_heightmap = m_chunk.heightmap();
|
||||
for (int x = 0; x < CHUCK_SIZE; x++) {
|
||||
for (int z = 0; z < CHUCK_SIZE; z++) {
|
||||
int height = static_cast<int>(m_heightmap[x][z]);
|
||||
for (int y = 5; y < height - 5; y++) {
|
||||
m_blocks[Chunk::get_index(x, y, z)] = 3;
|
||||
}
|
||||
|
||||
for (int y = height - 5; y <= height; y++) {
|
||||
m_blocks[Chunk::get_index(x, y, z)] = 4;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DesertBuilder::build_vegetation() {
|
||||
auto& m_chunk = m_chunk_generator.chunk();
|
||||
auto& m_blocks = m_chunk.blocks();
|
||||
auto& m_heightmap = m_chunk.heightmap();
|
||||
if (m_chunk_generator.neighbor_river()) {
|
||||
for (int x = 0; x < SIZE_X; x++) {
|
||||
for (int z = 0; z < SIZE_Z; z++) {
|
||||
int height = static_cast<int>(m_heightmap[x][z]);
|
||||
if (height >= SEA_LEVEL) {
|
||||
continue;
|
||||
}
|
||||
for (int y = height + 1; y < SEA_LEVEL; y++) {
|
||||
m_blocks[Chunk::get_index(x, y, z)] = 7;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ChunkGenerator& DesertBuilder::get_chunk_generator() {
|
||||
return m_chunk_generator;
|
||||
};
|
||||
|
||||
} // namespace Cubed
|
||||
72
src/gameplay/builders/forest_builder.cpp
Normal file
72
src/gameplay/builders/forest_builder.cpp
Normal file
@@ -0,0 +1,72 @@
|
||||
#include "Cubed/gameplay/builders/forest_builder.hpp"
|
||||
|
||||
#include "Cubed/gameplay/chunk.hpp"
|
||||
#include "Cubed/gameplay/chunk_generator.hpp"
|
||||
#include "Cubed/gameplay/tree.hpp"
|
||||
namespace Cubed {
|
||||
ForestBuilder::ForestBuilder(ChunkGenerator& chunk_generator)
|
||||
: m_chunk_generator(chunk_generator) {}
|
||||
|
||||
void ForestBuilder::build_biome() {
|
||||
BiomeBuilder::build_bottom();
|
||||
build_blocks();
|
||||
};
|
||||
|
||||
void ForestBuilder::build_blocks() {
|
||||
auto& m_chunk = m_chunk_generator.chunk();
|
||||
auto& m_blocks = m_chunk.blocks();
|
||||
auto& m_heightmap = m_chunk.heightmap();
|
||||
for (int x = 0; x < CHUCK_SIZE; x++) {
|
||||
for (int z = 0; z < CHUCK_SIZE; z++) {
|
||||
int height = static_cast<int>(m_heightmap[x][z]);
|
||||
for (int y = 5; y < height - 5; y++) {
|
||||
m_blocks[Chunk::get_index(x, y, z)] = 3;
|
||||
}
|
||||
for (int y = height - 5; y < height; y++) {
|
||||
m_blocks[Chunk::get_index(x, y, z)] = 2;
|
||||
}
|
||||
m_blocks[Chunk::get_index(x, height, z)] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ForestBuilder::build_vegetation() {
|
||||
auto& m_chunk = m_chunk_generator.chunk();
|
||||
auto& m_heightmap = m_chunk.heightmap();
|
||||
auto& m_random = m_chunk_generator.random();
|
||||
std::array<int, SIZE_X> x_arr;
|
||||
std::iota(x_arr.begin(), x_arr.end(), 0);
|
||||
std::shuffle(x_arr.begin(), x_arr.end(), m_random.engine());
|
||||
std::array<int, SIZE_Z> z_arr;
|
||||
std::iota(z_arr.begin(), z_arr.end(), 0);
|
||||
std::shuffle(z_arr.begin(), z_arr.end(), m_random.engine());
|
||||
for (auto x : x_arr) {
|
||||
for (auto z : z_arr) {
|
||||
int y = static_cast<int>(m_heightmap[x][z]);
|
||||
if (m_random.random_bool(forest_params().tree_frequency) &&
|
||||
y >= SEA_LEVEL) {
|
||||
build_tree(m_chunk, {x, y, z});
|
||||
}
|
||||
}
|
||||
}
|
||||
auto& m_blocks = m_chunk.blocks();
|
||||
if (m_chunk_generator.neighbor_river()) {
|
||||
for (int x = 0; x < SIZE_X; x++) {
|
||||
for (int z = 0; z < SIZE_Z; z++) {
|
||||
int height = static_cast<int>(m_heightmap[x][z]);
|
||||
if (height >= SEA_LEVEL) {
|
||||
continue;
|
||||
}
|
||||
for (int y = height + 1; y < SEA_LEVEL; y++) {
|
||||
m_blocks[Chunk::get_index(x, y, z)] = 7;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ChunkGenerator& ForestBuilder::get_chunk_generator() {
|
||||
return m_chunk_generator;
|
||||
};
|
||||
|
||||
} // namespace Cubed
|
||||
63
src/gameplay/builders/mountain_builder.cpp
Normal file
63
src/gameplay/builders/mountain_builder.cpp
Normal file
@@ -0,0 +1,63 @@
|
||||
#include "Cubed/gameplay/builders/mountain_builder.hpp"
|
||||
|
||||
#include "Cubed/gameplay/chunk.hpp"
|
||||
#include "Cubed/gameplay/chunk_generator.hpp"
|
||||
namespace Cubed {
|
||||
MountainBuilder::MountainBuilder(ChunkGenerator& chunk_generator)
|
||||
: m_chunk_generator(chunk_generator) {}
|
||||
|
||||
void MountainBuilder::build_biome() {
|
||||
BiomeBuilder::build_bottom();
|
||||
build_blocks();
|
||||
};
|
||||
|
||||
void MountainBuilder::build_blocks() {
|
||||
auto& m_chunk = m_chunk_generator.chunk();
|
||||
auto& m_blocks = m_chunk.blocks();
|
||||
auto& m_heightmap = m_chunk.heightmap();
|
||||
for (int x = 0; x < CHUCK_SIZE; x++) {
|
||||
for (int z = 0; z < CHUCK_SIZE; z++) {
|
||||
int height = static_cast<int>(m_heightmap[x][z]);
|
||||
for (int y = 5; y < height - 5; y++) {
|
||||
m_blocks[Chunk::get_index(x, y, z)] = 3;
|
||||
}
|
||||
for (int y = height - 5; y <= height - 1; y++) {
|
||||
if (y > 110) {
|
||||
m_blocks[Chunk::get_index(x, y, z)] = 3;
|
||||
} else {
|
||||
m_blocks[Chunk::get_index(x, y, z)] = 2;
|
||||
}
|
||||
}
|
||||
if (height > 110) {
|
||||
m_blocks[Chunk::get_index(x, height - 1, z)] = 3;
|
||||
} else {
|
||||
m_blocks[Chunk::get_index(x, height - 1, z)] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MountainBuilder::build_vegetation() {
|
||||
auto& m_chunk = m_chunk_generator.chunk();
|
||||
auto& m_blocks = m_chunk.blocks();
|
||||
auto& m_heightmap = m_chunk.heightmap();
|
||||
if (m_chunk_generator.neighbor_river()) {
|
||||
for (int x = 0; x < SIZE_X; x++) {
|
||||
for (int z = 0; z < SIZE_Z; z++) {
|
||||
int height = static_cast<int>(m_heightmap[x][z]);
|
||||
if (height >= SEA_LEVEL) {
|
||||
continue;
|
||||
}
|
||||
for (int y = height + 1; y < SEA_LEVEL; y++) {
|
||||
m_blocks[Chunk::get_index(x, y, z)] = 7;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ChunkGenerator& MountainBuilder::get_chunk_generator() {
|
||||
return m_chunk_generator;
|
||||
};
|
||||
|
||||
} // namespace Cubed
|
||||
55
src/gameplay/builders/plain_builder.cpp
Normal file
55
src/gameplay/builders/plain_builder.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#include "Cubed/gameplay/builders/plain_builder.hpp"
|
||||
|
||||
#include "Cubed/gameplay/chunk.hpp"
|
||||
#include "Cubed/gameplay/chunk_generator.hpp"
|
||||
namespace Cubed {
|
||||
PlainBuilder::PlainBuilder(ChunkGenerator& chunk_generator)
|
||||
: m_chunk_generator(chunk_generator) {}
|
||||
|
||||
void PlainBuilder::build_biome() {
|
||||
BiomeBuilder::build_bottom();
|
||||
build_blocks();
|
||||
};
|
||||
|
||||
void PlainBuilder::build_blocks() {
|
||||
auto& m_chunk = m_chunk_generator.chunk();
|
||||
auto& m_blocks = m_chunk.blocks();
|
||||
auto& m_heightmap = m_chunk.heightmap();
|
||||
for (int x = 0; x < CHUCK_SIZE; x++) {
|
||||
for (int z = 0; z < CHUCK_SIZE; z++) {
|
||||
int height = static_cast<int>(m_heightmap[x][z]);
|
||||
for (int y = 5; y < height - 5; y++) {
|
||||
m_blocks[Chunk::get_index(x, y, z)] = 3;
|
||||
}
|
||||
for (int y = height - 5; y < height; y++) {
|
||||
m_blocks[Chunk::get_index(x, y, z)] = 2;
|
||||
}
|
||||
m_blocks[Chunk::get_index(x, height, z)] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PlainBuilder::build_vegetation() {
|
||||
auto& m_chunk = m_chunk_generator.chunk();
|
||||
auto& m_blocks = m_chunk.blocks();
|
||||
auto& m_heightmap = m_chunk.heightmap();
|
||||
if (m_chunk_generator.neighbor_river()) {
|
||||
for (int x = 0; x < SIZE_X; x++) {
|
||||
for (int z = 0; z < SIZE_Z; z++) {
|
||||
int height = static_cast<int>(m_heightmap[x][z]);
|
||||
if (height >= SEA_LEVEL) {
|
||||
continue;
|
||||
}
|
||||
for (int y = height + 1; y < SEA_LEVEL; y++) {
|
||||
m_blocks[Chunk::get_index(x, y, z)] = 7;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ChunkGenerator& PlainBuilder::get_chunk_generator() {
|
||||
return m_chunk_generator;
|
||||
};
|
||||
|
||||
} // namespace Cubed
|
||||
59
src/gameplay/builders/river_builder.cpp
Normal file
59
src/gameplay/builders/river_builder.cpp
Normal file
@@ -0,0 +1,59 @@
|
||||
#include "Cubed/gameplay/builders/river_builder.hpp"
|
||||
|
||||
#include "Cubed/gameplay/chunk.hpp"
|
||||
#include "Cubed/gameplay/chunk_generator.hpp"
|
||||
namespace Cubed {
|
||||
RiverBuilder::RiverBuilder(ChunkGenerator& chunk_generator)
|
||||
: m_chunk_generator(chunk_generator) {}
|
||||
|
||||
void RiverBuilder::build_biome() {
|
||||
BiomeBuilder::build_bottom();
|
||||
build_blocks();
|
||||
};
|
||||
|
||||
void RiverBuilder::build_blocks() {
|
||||
auto& m_chunk = m_chunk_generator.chunk();
|
||||
auto& m_blocks = m_chunk.blocks();
|
||||
auto& m_heightmap = m_chunk.heightmap();
|
||||
for (int x = 0; x < CHUCK_SIZE; x++) {
|
||||
for (int z = 0; z < CHUCK_SIZE; z++) {
|
||||
int height = static_cast<int>(m_heightmap[x][z]);
|
||||
for (int y = 5; y < height - 5; y++) {
|
||||
m_blocks[Chunk::get_index(x, y, z)] = 3;
|
||||
}
|
||||
for (int y = height - 5; y <= height - 1; y++) {
|
||||
m_blocks[Chunk::get_index(x, y, z)] = 2;
|
||||
}
|
||||
for (int y = height; y <= height; y++) {
|
||||
if (y >= SEA_LEVEL - 1) {
|
||||
m_blocks[Chunk::get_index(x, y, z)] = 1;
|
||||
} else {
|
||||
m_blocks[Chunk::get_index(x, y, z)] = 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void RiverBuilder::build_vegetation() {
|
||||
auto& m_chunk = m_chunk_generator.chunk();
|
||||
auto& m_blocks = m_chunk.blocks();
|
||||
auto& m_heightmap = m_chunk.heightmap();
|
||||
for (int x = 0; x < SIZE_X; x++) {
|
||||
for (int z = 0; z < SIZE_Z; z++) {
|
||||
int height = static_cast<int>(m_heightmap[x][z]);
|
||||
if (height >= SEA_LEVEL) {
|
||||
continue;
|
||||
}
|
||||
for (int y = height + 1; y < SEA_LEVEL; y++) {
|
||||
m_blocks[Chunk::get_index(x, y, z)] = 7;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ChunkGenerator& RiverBuilder::get_chunk_generator() {
|
||||
return m_chunk_generator;
|
||||
};
|
||||
|
||||
} // namespace Cubed
|
||||
Reference in New Issue
Block a user