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);

View File

@@ -11,11 +11,33 @@ enum class GroundTileTypes
Air,
Dirt,
Grass,
Hard,
Stone,
Iron
Lava,
//Special
Hard,
//Ores
Bronze,
Gold,
Iron,
};
static std::map<GroundTileTypes, std::string> GroundTileTypeStrings {
{ GroundTileTypes::Air, "Air" },
{ GroundTileTypes::Dirt, "Dirt" },
{ GroundTileTypes::Grass, "Grass" },
{ GroundTileTypes::Stone, "Stone" },
{ GroundTileTypes::Lava, "Lava" },
{ GroundTileTypes::Hard, "Hard" },
{ GroundTileTypes::Bronze, "Bronze" },
{ GroundTileTypes::Gold, "Gold" },
{ GroundTileTypes::Iron, "Iron" },
};
GroundTileType * getRandomGroundTile();
class GroundTileType
{
public:
@@ -90,8 +112,20 @@ namespace Tiles
{
static GroundTileType* AIR = new GroundTileType("", GroundTileTypes::Air);
static GroundTileType* DIRT = new RandomGroundTile("tiles/dirt/dirt[0].png", GroundTileTypes::Dirt, 5);
static GroundTileType* IRON = new GroundTileType("tiles/ores/Ore_Ironium.png", GroundTileTypes::Iron);
namespace Hazards
{
static GroundTileType* STONE = new RandomGroundTile("tiles/ores/Ore_Stone_[0].png", GroundTileTypes::Stone, 3);
static GroundTileType* LAVA = new RandomGroundTile("tiles/ores/Ore_Lava_[0].png", GroundTileTypes::Lava, 3);
}
namespace Ores
{
static GroundTileType* IRON = new GroundTileType("tiles/ores/Ore_Ironium.png", GroundTileTypes::Iron);
static GroundTileType* BRONZE = new GroundTileType("tiles/ores/Ore_Bronzium.png", GroundTileTypes::Bronze);
static GroundTileType* GOLD = new GroundTileType("tiles/ores/Ore_Goldium.png", GroundTileTypes::Gold);
}
namespace Special
{
static GroundTileType* HARD_LEFT = new GroundTileType("tiles/dirt/special/hardLeft.png", GroundTileTypes::Hard);
@@ -102,6 +136,20 @@ namespace Tiles
}
}
static std::map<GroundTileType *, float> GroundTileWeights {
{ Tiles::AIR, 0.2f },
{ Tiles::DIRT, 0.5f },
{ Tiles::Special::GRASS, 0.0f },
{ Tiles::Hazards::STONE, 0.025f },
{ Tiles::Hazards::LAVA, 0.01f },
{ Tiles::Special::HARD_LEFT, 0.0f },
{ Tiles::Special::HARD_MIDDLE, 0.0f },
{ Tiles::Special::HARD_RIGHT, 0.0f },
{ Tiles::Ores::BRONZE, 0.05f },
{ Tiles::Ores::GOLD, 0.02f },
{ Tiles::Ores::IRON, 0.1f },
};
class WorldTile
{
public:
@@ -137,7 +185,7 @@ public:
private:
void DrawSide(const TileDirection& direction);
Vector2f m_Position;
GroundTileType* m_GroundTileType;
@@ -149,7 +197,7 @@ private:
surroundingTiles m_SurroundingTiles;
std::vector<Texture*> m_SideTextures { 8, nullptr };
std::vector<Texture *> m_SideTextures { 8, nullptr };
Texture* m_pAllTexture;
};