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

@@ -58,6 +58,7 @@ void Game::ProcessKeyUpEvent(const SDL_KeyboardEvent& e) {
void Game::ProcessMouseMotionEvent(const SDL_MouseMotionEvent& e) {
m_MousePos = Vector2f { float(e.x), float(e.y) };
m_pCurrentLevel->MouseMove(Vector2f { float(e.x), float(e.y) });
}
@@ -83,6 +84,17 @@ void Game::ProcessMouseUpEvent(const SDL_MouseButtonEvent& e) {
// break;
//}
}
void Game::ProcessMouseWheelEvent(const SDL_MouseWheelEvent& e) {
Vector2f scroll = Vector2f { 0, 0 };
if (e.y > 0) {
scroll = Vector2f { 0, 1 };
}
else if (e.y < 0) {
scroll = Vector2f { 0, -1 };
}
//camera zoom
m_Camera.SetScale((m_Camera.GetScale() - e.preciseY * 0.1f));
}
void Game::ProcessImGui() {
m_pCurrentLevel->ProcessImGui();
}

View File

@@ -25,6 +25,7 @@ public:
void ProcessMouseMotionEvent(const SDL_MouseMotionEvent& e) override;
void ProcessMouseDownEvent(const SDL_MouseButtonEvent& e) override;
void ProcessMouseUpEvent(const SDL_MouseButtonEvent& e) override;
void ProcessMouseWheelEvent(const SDL_MouseWheelEvent& e) override;
void ProcessImGui() override;

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

View File

@@ -6,15 +6,19 @@ class Building
{
public:
Building(const std::string& filePath, const Vector2f& position, TextureManager* pTextureManager);
Building(const Building& other) = delete;
Building(Building&& other) = delete;
~Building();
void Draw() const;
void Update(float dt);
private:
Texture* m_Texture;
Vector2f m_Position;
Vector2f m_Size;
Rectf m_boundingBox;
};

View File

@@ -21,26 +21,12 @@ WorldLevel::WorldLevel(Camera* camera, Rectf viewport): Level(camera),
m_viewport(viewport),
m_screenManager(ScreenManager::GetInstance()) {
// The grid is 34 x 50 big, the top center is 0,0 in world coords
for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
for (size_t y { 0 }; y < WORLD_HEIGHT; ++y) {
for (int x { 0 }; x < WORLD_WIDTH; ++x) {
for (int y { 0 }; y < WORLD_HEIGHT; ++y) {
const int actualX = x - WORLD_WIDTH / 2;
Vector2f pos = Vector2f { float(actualX * TILE_WIDTH), -float(y * TILE_HEIGHT) - TILE_HEIGHT };
GroundTileType* type = Tiles::AIR;
switch (utils::randRange(0, 2)) {
case 0:
type = Tiles::DIRT;
break;
case 1:
type = Tiles::IRON;
break;
case 2:
//AIR
break;
default:
std::cout << "??" << '\n';
}
m_gridManager.SetTileAtIndex(x, y, new WorldTile { pos, Tiles::DIRT, TextureManager::GetInstance(), &m_gridManager });
GroundTileType* type = getRandomGroundTile();
m_gridManager.SetTileAtIndex(x, y, new WorldTile { pos, type, TextureManager::GetInstance(), &m_gridManager });
}
}
for (int x { 0 }; x < WORLD_WIDTH; ++x) {
@@ -257,9 +243,11 @@ void WorldLevel::ProcessImGui() {
if (m_ShowCameraWindow) {
ImGui::Begin("Camera", &m_ShowCameraWindow, ImGuiWindowFlags_AlwaysAutoResize);
ImGui::Text("Camera Position: (%f, %f)", m_pCamera->GetPosition().x, m_pCamera->GetPosition().y);
ImGui::Text("Camera Scale: %f", m_pCamera->GetScale());
ImGui::Text("Is Right Mouse Down: %s", utils::isMouseDown(0) ? "true" : "false");
if (ImGui::Button("Reset Camera")) {
m_pCamera->SetPosition(Vector2f { -m_viewport.width / 2, -m_viewport.height / 2 });
m_pCamera->SetScale(1.0f);
}
ImGui::End();
}

View File

@@ -22,6 +22,12 @@ Player::Player(const Vector2f& Position, TextureManager* manager) : m_Position(P
m_currentAnimation = m_walkAnimation;
}
Player::~Player() {
delete m_walkAnimation;
delete m_turnAnimation;
delete m_digAnimation;
}
Collision::CollisionRect Player::GetCollisionRect() const {
Collision::CollisionRect rect = { m_Position, m_Size, m_Vel };
return rect;

View File

@@ -30,7 +30,12 @@ enum class DigDirection
class Player
{
public:
Player(const Vector2f& Position, TextureManager* pTextureManager);
explicit Player(const Vector2f& Position, TextureManager* pTextureManager);
Player( const Player& other ) = default;
Player(Player&& other) = default;
~Player();
Collision::CollisionRect GetCollisionRect() const;
void Update(float elapsedTime, WorldLevel& level);
void Draw() const;

View File

@@ -2,7 +2,6 @@
#include <ctime>
#include "Game.h"
#define DEBUG
void StartHeapControl();
void DumpMemoryLeaks();