45 lines
1.4 KiB
C++
45 lines
1.4 KiB
C++
#include <chrono>
|
|
|
|
#include "world.h"
|
|
#include "bettola/game/chunk.h"
|
|
#include "bettola/noise/fast_noise_lite.h"
|
|
|
|
World::World(void) {
|
|
_noise.SetSeed(std::chrono::high_resolution_clock::now().time_since_epoch().count());
|
|
_noise.SetNoiseType(FastNoiseLite::NoiseType_Perlin);
|
|
_noise.SetFrequency(0.02f);
|
|
|
|
_noise.SetFractalType(FastNoiseLite::FractalType_FBm);
|
|
_noise.SetFractalOctaves(5);
|
|
_noise.SetFractalLacunarity(2.0f);
|
|
_noise.SetFractalGain(0.5);
|
|
}
|
|
|
|
BettolaLib::Game::Chunk& World::get_chunk(int x, int z) {
|
|
ChunkPos pos = {x, z};
|
|
auto it = _chunks.find(pos);
|
|
if(it != _chunks.end()) {
|
|
return it->second;
|
|
}
|
|
|
|
/* If chunk doesn't exist, generate it. */
|
|
BettolaLib::Game::Chunk& new_chunk = _chunks[pos];
|
|
_generate_chunk(new_chunk, x, z);
|
|
return new_chunk;
|
|
}
|
|
|
|
void World::_generate_chunk(BettolaLib::Game::Chunk& chunk, int chunk_x, int chunk_z) {
|
|
for(int z = 0; z < BettolaLib::Game::CHUNK_DATA_HEIGHT; ++z) {
|
|
for(int x = 0; x < BettolaLib::Game::CHUNK_DATA_WIDTH; ++x) {
|
|
/* Calculate world coordinates. */
|
|
float world_x = (float)(chunk_x * (BettolaLib::Game::CHUNK_WIDTH - 1)
|
|
+ (x - BettolaLib::Game::CHUNK_BORDER_SIZE));
|
|
float world_z = (float)(chunk_z * (BettolaLib::Game::CHUNK_HEIGHT - 1)
|
|
+ (z - BettolaLib::Game::CHUNK_BORDER_SIZE));
|
|
|
|
/* generate noise value. */
|
|
chunk.heightmap[z * BettolaLib::Game::CHUNK_DATA_WIDTH + x] = _noise.GetNoise(world_x, world_z);
|
|
}
|
|
}
|
|
}
|