Add more ores

Add weighted random distribution
This commit is contained in:
Bram Verhulst
2024-04-22 22:34:29 +02:00
parent e578a77d84
commit e75b80eea8
25 changed files with 198 additions and 94 deletions

View File

@@ -10,6 +10,29 @@
#include "WorldGridManager.h"
GroundTileType* getRandomGroundTile() {
// Generate a random weight between 0 and the sum of weights
float sumWeights = 0.0f;
for (const auto& pair : GroundTileWeights) {
sumWeights += pair.second;
}
float randomWeight = static_cast<float>(rand()) / RAND_MAX * sumWeights;
// Find the corresponding ground tile type
float cumulativeWeight = 0.0f;
for (const auto& pair : GroundTileWeights) {
cumulativeWeight += pair.second;
if (randomWeight <= cumulativeWeight) {
return pair.first;
}
}
// This should never be reached if weights sum to 1.0
return Tiles::AIR; // Default value
}
WorldTile::WorldTile(const Vector2f& position, GroundTileType* groundTileType, TextureManager* pTextureManager, WorldGridManager* pGridManager) : m_Position { position }, m_GroundTileType { groundTileType }, m_pGridManager { pGridManager } {
// const std::string dirtPath = + "tiles/dirt/dirt" + std::to_string(utils::randRange(1, 5)) + ".png";
// m_pTexture = new Texture(dirtPath);