mirror of
https://github.com/HowestDAE/dae16-VerhulstBram.git
synced 2025-12-16 20:41:47 +01:00
Add more ores
Add weighted random distribution
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user