Skip to content

Commit 019de55

Browse files
Create WorldGenerator.cpp
1 parent 67d7dc8 commit 019de55

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

src/World/WorldGenerator.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#include "WorldGenerator.hpp"
2+
#include <random>
3+
#include <chrono>
4+
5+
WorldGenerator::WorldGenerator(int width, int height)
6+
: m_Width(width)
7+
, m_Height(height)
8+
, m_Progress(0.0f)
9+
, m_IsGenerating(false)
10+
{
11+
m_Tiles.resize(height, std::vector<Tile>(width));
12+
}
13+
14+
WorldGenerator::~WorldGenerator() {}
15+
16+
void WorldGenerator::Generate() {
17+
m_IsGenerating = true;
18+
m_Progress = 0.0f;
19+
20+
GenerateHeightMap();
21+
m_Progress = 0.5f;
22+
23+
GenerateBiomes();
24+
m_Progress = 1.0f;
25+
26+
m_IsGenerating = false;
27+
}
28+
29+
void WorldGenerator::GenerateHeightMap() {
30+
std::random_device rd;
31+
std::mt19937 gen(rd());
32+
std::uniform_real_distribution<float> dis(0.0f, 1.0f);
33+
34+
for (int y = 0; y < m_Height; ++y) {
35+
for (int x = 0; x < m_Width; ++x) {
36+
float height = dis(gen);
37+
38+
if (height < 0.3f) {
39+
m_Tiles[y][x].type = TileType::Water;
40+
}
41+
else if (height < 0.7f) {
42+
m_Tiles[y][x].type = TileType::Grass;
43+
}
44+
else {
45+
m_Tiles[y][x].type = TileType::Mountain;
46+
}
47+
48+
m_Tiles[y][x].color = GetTileColor(m_Tiles[y][x].type);
49+
}
50+
}
51+
}
52+
53+
void WorldGenerator::GenerateBiomes() {
54+
// В этой версии мы просто используем цвета для разных типов тайлов
55+
for (int y = 0; y < m_Height; ++y) {
56+
for (int x = 0; x < m_Width; ++x) {
57+
m_Tiles[y][x].color = GetTileColor(m_Tiles[y][x].type);
58+
}
59+
}
60+
}
61+
62+
Color WorldGenerator::GetTileColor(TileType type) const {
63+
switch (type) {
64+
case TileType::Water:
65+
return Color(0, 0, 255); // Синий
66+
case TileType::Grass:
67+
return Color(0, 255, 0); // Зелёный
68+
case TileType::Mountain:
69+
return Color(128, 128, 128); // Серый
70+
default:
71+
return Color(0, 0, 0); // Чёрный
72+
}
73+
}
74+
75+
void WorldGenerator::Render(IRenderer* renderer) {
76+
const int tileSize = 6; // Размер каждого тайла в пикселях
77+
const int offsetX = 10; // Отступ от левого края
78+
const int offsetY = 3; // Отступ от верхнего края
79+
80+
for (int y = 0; y < m_Height; ++y) {
81+
for (int x = 0; x < m_Width; ++x) {
82+
const Tile& tile = m_Tiles[y][x];
83+
renderer->FillRect(
84+
offsetX + x * tileSize,
85+
offsetY + y * tileSize,
86+
tileSize - 1,
87+
tileSize - 1,
88+
tile.color
89+
);
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)