Add basic controller Support

Fix more memory leaks (seeing a trend here)
This commit is contained in:
Bram Verhulst
2024-04-24 21:38:14 +02:00
parent 1b90f222a4
commit f5d352239c
14 changed files with 2267 additions and 166 deletions

View File

@@ -5,6 +5,7 @@
#include "Camera.h"
#include "colors.h"
#include "GroundTileTypeManager.h"
#include "../TextureManager.h"
#include "utils.h"
#include "WorldGridManager.h"
@@ -12,7 +13,7 @@
GroundTileType* getRandomGroundTile() {
GroundTileType * getRandomGroundTile() {
// Generate a random weight between 0 and the sum of weights
float sumWeights = 0.0f;
for (const auto& pair : GroundTileWeights) {
@@ -31,13 +32,41 @@ GroundTileType* getRandomGroundTile() {
}
// This should never be reached if weights sum to 1.0
return Tiles::AIR; // Default value
return GroundTileTypeManager::GetInstance()->AIR; // Default value
}
// void InitializeGroundTiles() {
//
// Tiles::AIR = new GroundTileType("", GroundTileTypes::Air);
// }
WorldTile::WorldTile(const Vector2f& position, GroundTileType* groundTileType, TextureManager* pTextureManager, WorldGridManager* pGridManager) : m_Position { position }, m_GroundTileType { groundTileType }, m_pGridManager { pGridManager } {
void InitializeGroundTiles() {
Tiles tiles {};
GroundTileTypeManager::GetInstance()->AIR = new GroundTileType("", GroundTileTypes::Air);
GroundTileTypeManager::GetInstance()->DIRT = new RandomGroundTile("tiles/dirt/dirt[0].png", GroundTileTypes::Dirt, 5);
GroundTileTypeManager::GetInstance()->STONE = new RandomGroundTile("tiles/ores/Ore_Stone_[0].png", GroundTileTypes::Stone, 3);
GroundTileTypeManager::GetInstance()->LAVA = new RandomGroundTile("tiles/ores/Ore_Lava_[0].png", GroundTileTypes::Lava, 3);
GroundTileTypeManager::GetInstance()->IRON = new GroundTileType("tiles/ores/Ore_Ironium.png", GroundTileTypes::Iron);
GroundTileTypeManager::GetInstance()->BRONZE = new GroundTileType("tiles/ores/Ore_Bronzium.png", GroundTileTypes::Bronze);
GroundTileTypeManager::GetInstance()->GOLD = new GroundTileType("tiles/ores/Ore_Goldium.png", GroundTileTypes::Gold);
GroundTileTypeManager::GetInstance()->GRASS = new RandomGroundTile("tiles/dirt/special/grass[0].png", GroundTileTypes::Grass, 2);
GroundTileTypeManager::GetInstance()->HARD_LEFT = new GroundTileType("tiles/dirt/special/hardLeft.png", GroundTileTypes::Hard);
GroundTileTypeManager::GetInstance()->HARD_RIGHT = new GroundTileType("tiles/dirt/special/hardRight.png", GroundTileTypes::Hard);
GroundTileTypeManager::GetInstance()->HARD_MIDDLE = new GroundTileType("tiles/dirt/special/hardMiddle.png", GroundTileTypes::Hard);
GroundTileWeights = {
{ GroundTileTypeManager::GetInstance()->AIR, 0.2f },
{ GroundTileTypeManager::GetInstance()->DIRT, 0.5f },
{ GroundTileTypeManager::GetInstance()->GRASS, 0.0f },
{ GroundTileTypeManager::GetInstance()->STONE, 0.025f },
{ GroundTileTypeManager::GetInstance()->LAVA, 0.01f },
{ GroundTileTypeManager::GetInstance()->HARD_LEFT, 0.0f },
{ GroundTileTypeManager::GetInstance()->HARD_MIDDLE, 0.0f },
{ GroundTileTypeManager::GetInstance()->HARD_RIGHT, 0.0f },
{ GroundTileTypeManager::GetInstance()->BRONZE, 0.05f },
{ GroundTileTypeManager::GetInstance()->GOLD, 0.02f },
{ GroundTileTypeManager::GetInstance()->IRON, 0.1f },
};
}
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);
m_pTexture = pTextureManager->GetTexture(groundTileType->getPath());
@@ -51,7 +80,7 @@ WorldTile::WorldTile(const Vector2f& position, GroundTileType* groundTileType, T
m_SideTextures[TileDirection::BottomMiddle] = pTextureManager->GetTexture("tiles/dirt/sidepieces/middleBottom.png");
m_SideTextures[TileDirection::MiddleLeft] = pTextureManager->GetTexture("tiles/dirt/sidepieces/middleLeft.png");
m_SideTextures[TileDirection::MiddleRight] = pTextureManager->GetTexture("tiles/dirt/sidepieces/middleRight.png");
m_pAllTexture = pTextureManager->GetTexture("tiles/dirt/sidepieces/all.png");
}
@@ -64,21 +93,22 @@ void WorldTile::Draw() {
case GroundTileTypes::Air: {
//check if it's all around dirt
bool allDirt = true;
for (int i = 0; i < 8; i++) {
const WorldTile* tile = m_SurroundingTiles.GetTile(static_cast<TileDirection>(i));
if(tile != nullptr) { //Tile exists
TileDirection allDirtDirections[] { TileDirection::BottomMiddle, TileDirection::MiddleLeft, TileDirection::MiddleRight, TileDirection::TopMiddle };
for (int i = 0; i < 3; i++) {
const WorldTile* tile = m_SurroundingTiles.GetTile(allDirtDirections[i]);
if (tile != nullptr) { //Tile exists
const GroundTileTypes type = tile->GetTileType()->getType();
if(type != Tiles::DIRT->getType()) {
if (type != GroundTileTypeManager::GetInstance()->DIRT->getType()) {
allDirt = false;
break;
}
}
}
if(allDirt) {
if (allDirt) {
m_pAllTexture->Draw(m_Position);
return;
}
if(*m_GroundTileType == Tiles::AIR) {
else {
this->DrawSide(TileDirection::TopLeft);
this->DrawSide(TileDirection::TopRight);
this->DrawSide(TileDirection::BottomLeft);
@@ -88,9 +118,8 @@ void WorldTile::Draw() {
this->DrawSide(TileDirection::BottomMiddle);
this->DrawSide(TileDirection::MiddleLeft);
this->DrawSide(TileDirection::MiddleRight);
break;
}
break;
}
case GroundTileTypes::Dirt:
case GroundTileTypes::Hard:
@@ -101,9 +130,9 @@ void WorldTile::Draw() {
default:
break;
}
if (*m_GroundTileType != Tiles::AIR) {
if (*m_GroundTileType != GroundTileTypeManager::GetInstance()->AIR) {
m_pTexture->Draw(m_Position);
if (m_Hightlight) {
utils::SetColor(Colors::GREEN);
@@ -111,16 +140,14 @@ void WorldTile::Draw() {
}
}
}
void WorldTile::Update(const Camera* camera) {
m_pGridManager->GetIndexFromPosition(m_Position);
m_SurroundingTiles = m_pGridManager->GetSurroundingTiles(this);
const Vector2f mouse_pos = camera->TransformMouse(Vector2f{utils::GetMousePos().x, 500 - utils::GetMousePos().y});
m_Hightlight = utils::IsPointInRect(mouse_pos, Rectf{GetCollisionRect().pos, GetCollisionRect().size});
const Vector2f mouse_pos = camera->TransformMouse(Vector2f { utils::GetMousePos().x, 500 - utils::GetMousePos().y });
m_Hightlight = utils::IsPointInRect(mouse_pos, Rectf { GetCollisionRect().pos, GetCollisionRect().size });
}
Collision::TileCollisionRect WorldTile::GetCollisionRect() {
return Collision::TileCollisionRect { m_Position, GetSize(), ( this ) };
@@ -128,18 +155,19 @@ Collision::TileCollisionRect WorldTile::GetCollisionRect() {
void WorldTile::DrawSide(const TileDirection& direction) {
const WorldTile* tile = m_SurroundingTiles.GetTile(direction);
if(tile != nullptr) {
if (tile != nullptr) {
const GroundTileTypes type = tile->GetTileType()->getType();
if(direction == TileDirection::BottomMiddle || direction == TileDirection::BottomLeft || direction == TileDirection::BottomRight) {
if(type == Tiles::Special::GRASS->getType() || type == Tiles::Special::HARD_LEFT->getType() || type == Tiles::Special::HARD_MIDDLE->getType() || type == Tiles::Special::HARD_RIGHT->getType()) {
if (direction == TileDirection::BottomMiddle || direction == TileDirection::BottomLeft || direction == TileDirection::BottomRight) {
if (type == GroundTileTypeManager::GetInstance()->GRASS->getType() || type == GroundTileTypeManager::GetInstance()->HARD_LEFT->getType() || type ==
GroundTileTypeManager::GetInstance()->HARD_MIDDLE->getType() || type == GroundTileTypeManager::GetInstance()->HARD_RIGHT->getType()) {
//Fix for edges being renderd on grass / hard tiles
//TODO: Possible fix if i have time
return;
}
}
if(type != Tiles::AIR->getType()) {
if (type != GroundTileTypeManager::GetInstance()->AIR->getType()) {
m_SideTextures[direction]->Draw(m_Position);
}
}
}
}