feat: get_biome method for Chunk
Signed-off-by: szdytom <szdytom@qq.com>
This commit is contained in:
parent
5af0239ab1
commit
416eabd31f
@ -6,6 +6,7 @@ set(ISTD_TILEMAP_SRC
|
|||||||
src/tilemap.cpp
|
src/tilemap.cpp
|
||||||
src/noise.cpp
|
src/noise.cpp
|
||||||
src/biome.cpp
|
src/biome.cpp
|
||||||
|
src/chunk.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
# Create the tilemap library
|
# Create the tilemap library
|
||||||
|
@ -41,11 +41,15 @@ Each chunk contains 64×64 tiles and sub-chunk biome information.
|
|||||||
```cpp
|
```cpp
|
||||||
struct Chunk {
|
struct Chunk {
|
||||||
static constexpr uint8_t size = 64; // Tiles per side
|
static constexpr uint8_t size = 64; // Tiles per side
|
||||||
static constexpr uint8_t subchunk_size = /* certain value */; // Tiles per sub-chunk side
|
static constexpr uint8_t subchunk_size = 4; // Tiles per sub-chunk side
|
||||||
static constexpr uint8_t subchunk_count = size / subchunk_size; // Sub-chunks per side
|
static constexpr uint8_t subchunk_count = size / subchunk_size; // Sub-chunks per side
|
||||||
|
|
||||||
Tile tiles[size][size]; // 64x64 tile grid
|
Tile tiles[size][size]; // 64x64 tile grid
|
||||||
BiomeType biome[subchunk_count][subchunk_count];
|
BiomeType biome[subchunk_count][subchunk_count]; // Sub-chunk biomes
|
||||||
|
|
||||||
|
// Get biome for a specific sub-chunk position
|
||||||
|
BiomeType& get_biome(const SubChunkPos& pos);
|
||||||
|
const BiomeType& get_biome(const SubChunkPos& pos) const;
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -85,6 +89,23 @@ struct TilePos {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### SubChunkPos
|
||||||
|
|
||||||
|
Position within a chunk's sub-chunk grid.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
struct SubChunkPos {
|
||||||
|
std::uint8_t sub_x;
|
||||||
|
std::uint8_t sub_y;
|
||||||
|
|
||||||
|
constexpr SubChunkPos(std::uint8_t x, std::uint8_t y);
|
||||||
|
};
|
||||||
|
|
||||||
|
std::pair<std::uint8_t, std::uint8_t> subchunk_to_tile_start(
|
||||||
|
const SubChunkPos& pos
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
## Terrain Generation
|
## Terrain Generation
|
||||||
|
|
||||||
### GenerationConfig
|
### GenerationConfig
|
||||||
@ -223,21 +244,6 @@ const BiomeProperties& get_biome_properties(BiomeType biome);
|
|||||||
BiomeType determine_biome(double temperature, double humidity);
|
BiomeType determine_biome(double temperature, double humidity);
|
||||||
```
|
```
|
||||||
|
|
||||||
### SubChunkPos
|
|
||||||
|
|
||||||
Position within a chunk's sub-chunk grid.
|
|
||||||
|
|
||||||
```cpp
|
|
||||||
struct SubChunkPos {
|
|
||||||
std::uint8_t sub_x;
|
|
||||||
std::uint8_t sub_y;
|
|
||||||
};
|
|
||||||
|
|
||||||
constexpr std::pair<std::uint8_t, std::uint8_t> subchunk_to_tile_start(
|
|
||||||
const SubChunkPos& pos
|
|
||||||
);
|
|
||||||
```
|
|
||||||
|
|
||||||
## Usage Examples
|
## Usage Examples
|
||||||
|
|
||||||
### Basic Map Generation
|
### Basic Map Generation
|
||||||
@ -295,10 +301,18 @@ const auto& tile2 = chunk.tiles[32][32];
|
|||||||
### Working with Biomes
|
### Working with Biomes
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
// Get biome for a sub-chunk
|
// Method 1: Direct array access (traditional way)
|
||||||
const auto& chunk = tilemap.get_chunk(0, 0);
|
const auto& chunk = tilemap.get_chunk(0, 0);
|
||||||
istd::BiomeType biome = chunk.biome[1][1]; // Sub-chunk (1,1)
|
istd::BiomeType biome = chunk.biome[1][1]; // Sub-chunk (1,1)
|
||||||
|
|
||||||
|
// Method 2: Using SubChunkPos and get_biome method (recommended)
|
||||||
|
istd::SubChunkPos pos(1, 1); // Sub-chunk (1,1)
|
||||||
|
istd::BiomeType biome2 = chunk.get_biome(pos);
|
||||||
|
|
||||||
|
// Modify biome using the new method
|
||||||
|
auto& mutable_chunk = tilemap.get_chunk(0, 0);
|
||||||
|
mutable_chunk.get_biome(pos) = istd::BiomeType::Forest;
|
||||||
|
|
||||||
// Get biome properties
|
// Get biome properties
|
||||||
const auto& props = istd::get_biome_properties(biome);
|
const auto& props = istd::get_biome_properties(biome);
|
||||||
std::cout << "Biome: " << props.name << std::endl;
|
std::cout << "Biome: " << props.name << std::endl;
|
||||||
|
@ -41,10 +41,6 @@ struct BiomeProperties {
|
|||||||
double ice_ratio;
|
double ice_ratio;
|
||||||
double sand_ratio;
|
double sand_ratio;
|
||||||
double land_ratio;
|
double land_ratio;
|
||||||
|
|
||||||
// Noise parameters for base terrain
|
|
||||||
int base_octaves = 3;
|
|
||||||
double base_persistence = 0.5;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Get biome properties for terrain generation
|
// Get biome properties for terrain generation
|
||||||
@ -53,18 +49,6 @@ const BiomeProperties &get_biome_properties(BiomeType biome);
|
|||||||
// Determine biome type based on temperature and humidity
|
// Determine biome type based on temperature and humidity
|
||||||
BiomeType determine_biome(double temperature, double humidity);
|
BiomeType determine_biome(double temperature, double humidity);
|
||||||
|
|
||||||
struct SubChunkPos {
|
|
||||||
std::uint8_t sub_x;
|
|
||||||
std::uint8_t sub_y;
|
|
||||||
|
|
||||||
constexpr SubChunkPos(std::uint8_t x, std::uint8_t y): sub_x(x), sub_y(y) {}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Get the starting tile coordinates for a sub-chunk
|
|
||||||
std::pair<std::uint8_t, std::uint8_t> subchunk_to_tile_start(
|
|
||||||
const SubChunkPos &pos
|
|
||||||
);
|
|
||||||
|
|
||||||
} // namespace istd
|
} // namespace istd
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -8,6 +8,14 @@ namespace istd {
|
|||||||
// Forward declaration
|
// Forward declaration
|
||||||
enum class BiomeType : std::uint8_t;
|
enum class BiomeType : std::uint8_t;
|
||||||
|
|
||||||
|
// Position within a chunk's sub-chunk grid
|
||||||
|
struct SubChunkPos {
|
||||||
|
std::uint8_t sub_x;
|
||||||
|
std::uint8_t sub_y;
|
||||||
|
|
||||||
|
constexpr SubChunkPos(std::uint8_t x, std::uint8_t y): sub_x(x), sub_y(y) {}
|
||||||
|
};
|
||||||
|
|
||||||
// Represents the position of a tile in the map, using chunk and local
|
// Represents the position of a tile in the map, using chunk and local
|
||||||
// coordinates
|
// coordinates
|
||||||
struct TilePos {
|
struct TilePos {
|
||||||
@ -32,8 +40,23 @@ struct Chunk {
|
|||||||
|
|
||||||
// array of biomes for sub-chunks
|
// array of biomes for sub-chunks
|
||||||
BiomeType biome[subchunk_count][subchunk_count];
|
BiomeType biome[subchunk_count][subchunk_count];
|
||||||
|
|
||||||
|
// Get biome for a specific sub-chunk position
|
||||||
|
BiomeType &get_biome(const SubChunkPos &pos) {
|
||||||
|
return biome[pos.sub_x][pos.sub_y];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get biome for a specific sub-chunk position (const version)
|
||||||
|
const BiomeType &get_biome(const SubChunkPos &pos) const {
|
||||||
|
return biome[pos.sub_x][pos.sub_y];
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Get the starting tile coordinates for a sub-chunk
|
||||||
|
std::pair<std::uint8_t, std::uint8_t> subchunk_to_tile_start(
|
||||||
|
const SubChunkPos &pos
|
||||||
|
);
|
||||||
|
|
||||||
} // namespace istd
|
} // namespace istd
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -119,11 +119,4 @@ BiomeType determine_biome(double temperature, double humidity) {
|
|||||||
return static_cast<BiomeType>(index);
|
return static_cast<BiomeType>(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::pair<std::uint8_t, std::uint8_t> subchunk_to_tile_start(
|
|
||||||
const SubChunkPos &pos
|
|
||||||
) {
|
|
||||||
// Convert sub-chunk position to tile start coordinates
|
|
||||||
return {pos.sub_x * Chunk::subchunk_size, pos.sub_y * Chunk::subchunk_size};
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace istd
|
} // namespace istd
|
||||||
|
12
tilemap/src/chunk.cpp
Normal file
12
tilemap/src/chunk.cpp
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#include "chunk.h"
|
||||||
|
|
||||||
|
namespace istd {
|
||||||
|
|
||||||
|
std::pair<std::uint8_t, std::uint8_t> subchunk_to_tile_start(
|
||||||
|
const SubChunkPos &pos
|
||||||
|
) {
|
||||||
|
// Convert sub-chunk position to tile start coordinates
|
||||||
|
return {pos.sub_x * Chunk::subchunk_size, pos.sub_y * Chunk::subchunk_size};
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace istd
|
@ -74,7 +74,7 @@ void TerrainGenerator::generate_chunk(
|
|||||||
for (std::uint8_t sub_x = 0; sub_x < Chunk::subchunk_count; ++sub_x) {
|
for (std::uint8_t sub_x = 0; sub_x < Chunk::subchunk_count; ++sub_x) {
|
||||||
for (std::uint8_t sub_y = 0; sub_y < Chunk::subchunk_count; ++sub_y) {
|
for (std::uint8_t sub_y = 0; sub_y < Chunk::subchunk_count; ++sub_y) {
|
||||||
SubChunkPos sub_pos(sub_x, sub_y);
|
SubChunkPos sub_pos(sub_x, sub_y);
|
||||||
BiomeType biome = chunk.biome[sub_x][sub_y];
|
BiomeType biome = chunk.get_biome(sub_pos);
|
||||||
generate_subchunk(tilemap, chunk_x, chunk_y, sub_pos, biome);
|
generate_subchunk(tilemap, chunk_x, chunk_y, sub_pos, biome);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user