diff --git a/Game/Game.vcxproj b/Game/Game.vcxproj index 893b3a5..a30b7d4 100644 --- a/Game/Game.vcxproj +++ b/Game/Game.vcxproj @@ -149,6 +149,7 @@ xcopy "$(SolutionDir)Resources\*.*" "$(TargetDir)" /y /d /s + MultiThreadedDebugDll EnableFastChecks @@ -306,6 +307,7 @@ xcopy "$(SolutionDir)Resources\*.*" "$(TargetDir)" /y /d /s + diff --git a/Game/Player.cpp b/Game/Player.cpp index 17ff31a..eb1cc86 100644 --- a/Game/Player.cpp +++ b/Game/Player.cpp @@ -40,6 +40,11 @@ void Player::Update(float elapsedTime, WorldLevel& level) { if(m_Grounded) { if(m_ContactMap[Collision::CollisionDirection::Bottom] != nullptr) { m_ContactMap[Collision::CollisionDirection::Bottom]->SetTileType(GroundTileTypes::Air); + WorldTile* tile = m_ContactMap[Collision::CollisionDirection::Bottom]; + //center of tile + Point2f tileCenter = tile->GetCollisionRect().getCollisionRect().pos + tile->GetCollisionRect().getCollisionRect().size / 2; + m_Position = Point2f{tileCenter.x - m_Size.x / 2, tileCenter.y - m_Size.y / 2}; + } } @@ -56,12 +61,15 @@ void Player::Update(float elapsedTime, WorldLevel& level) { Point2f intersectionPoint, normal; std::vector> contactTimes{}; + + WorldGridManager& gridManager = level.GetGridManager(); - for (size_t x { 0 }; x < level.WORLD_WIDTH; ++x) { - for(size_t y { 0 }; y < level.WORLD_HEIGHT; ++y) { - if(level.GetTileAt(Point2f{(float)x,(float)y})->GetTileType() == GroundTileTypes::Dirt) { - if(Collision::DynamicRectVsRect(this->GetCollisionRect(), elapsedTime, level.GetTileAt(Point2f{(float)x, (float)y})->GetCollisionRect().getCollisionRect(), intersectionPoint, normal, t)) { - contactTimes.push_back(std::pair{x + y * level.WORLD_WIDTH, t}); + for (size_t x { 0 }; x < WORLD_WIDTH; ++x) { + for(size_t y { 0 }; y < WORLD_HEIGHT; ++y) { + if(gridManager.GetTileAtIndex(x,y)->GetTileType() == GroundTileTypes::Dirt) { + gridManager.GetTileAtIndex(x,y)->m_Hightlight = false; + if(Collision::DynamicRectVsRect(this->GetCollisionRect(), elapsedTime, gridManager.GetTileAtIndex(x, y)->GetCollisionRect().getCollisionRect(), intersectionPoint, normal, t)) { + contactTimes.push_back(std::pair{x + y * WORLD_WIDTH, t}); } } } @@ -72,19 +80,43 @@ void Player::Update(float elapsedTime, WorldLevel& level) { }); for (std::pair contact_time : contactTimes) { - int x = contact_time.first % WorldLevel::WORLD_WIDTH; - int y = contact_time.first / WorldLevel::WORLD_WIDTH; - WorldTile* world_tile = level.GetTileAt(Point2f{(float) x, (float) y}); - //check if tile is next to player + int x = contact_time.first % WORLD_WIDTH; + int y = contact_time.first / WORLD_WIDTH; + WorldTile* world_tile = gridManager.GetTileAtIndex(x,y); + world_tile->m_Hightlight = true; - //check if the collision happend beneeth the player - if(world_tile->GetCollisionRect().pos.y < m_Position.y) { - m_Grounded = true; - m_ContactMap[Collision::CollisionDirection::Bottom] = world_tile; + bool isDiggingCandidate = true; + Collision::CollisionRect tileRect = world_tile->GetCollisionRect().getCollisionRect(); + //check if the tile is above the player + if(tileRect.pos.y > m_Position.y + m_Size.y) { + isDiggingCandidate = false; } + //check if tile is left of player + if(tileRect.pos.x > m_Position.x + m_Size.x) { + isDiggingCandidate = false; + } + //check if tile is right of player + if(tileRect.pos.x + tileRect.size.x < m_Position.x) { + isDiggingCandidate = false; + } + //check if the collision happend under the player + if(tileRect.pos.y < m_Position.y - m_Size.y) { + //Tile is under player + //check if the center of the player is between the centers of the tiles left and right + if(m_Position.x + m_Size.x / 2 > tileRect.pos.x && m_Position.x + m_Size.x / 2 < tileRect.pos.x + tileRect.size.x) { + if(isDiggingCandidate) { + //Dig the tile + m_ContactMap[Collision::CollisionDirection::Bottom] = world_tile; + m_Grounded = true; + } + } + } + //m_ContactMap[Collision::CollisionDirection::Bottom] = world_tile; + //m_Grounded = true; + Collision::CollisionRect rect = world_tile->GetCollisionRect().getCollisionRect(); //TODO: fix this mess Collision::ResolvePlayerVsRect(*this, elapsedTime, &rect); } diff --git a/Game/Player.h b/Game/Player.h index 5cc5f4c..d9d081e 100644 --- a/Game/Player.h +++ b/Game/Player.h @@ -20,6 +20,8 @@ public: auto GetContactMap(){ return m_ContactMap; } void SetContactMap(Collision::CollisionDirection dir, WorldTile* tile) { m_ContactMap[dir] = tile; } + void ProcessImGui(); + private: Point2f m_Position; Point2f m_Size; diff --git a/Game/WorldGridManager.cpp b/Game/WorldGridManager.cpp new file mode 100644 index 0000000..61ddfa1 --- /dev/null +++ b/Game/WorldGridManager.cpp @@ -0,0 +1,29 @@ +#include "WorldGridManager.h" +WorldGridManager::WorldGridManager() { + for (size_t x { 0 }; x < WORLD_WIDTH; ++x) { + for(size_t y { 0 }; y < WORLD_HEIGHT; ++y) { + m_worldTiles[x][y] = nullptr; + } + } + +} +WorldGridManager::~WorldGridManager() { + +} +WorldTile* WorldGridManager::GetTileAtIndex(const int x, const int y) const { + if (x < 0 || x >= WORLD_WIDTH || y < 0 || y >= WORLD_HEIGHT) { + return nullptr; + } + return m_worldTiles[x][y]; +} +WorldTile* WorldGridManager::GetTileAtWorldPos(const Point2f& pos) const { + int x = int(pos.x / TILE_WIDTH + WORLD_WIDTH / 2); + int y = int(-pos.y / TILE_HEIGHT); + if (x < 0 || x >= WORLD_WIDTH || y < 0 || y >= WORLD_HEIGHT) { + return nullptr; + } + return m_worldTiles[x][y]; +} +void WorldGridManager::SetTileAtIndex(const int x, const int y, WorldTile* tile) { + m_worldTiles[x][y] = tile; +} diff --git a/Game/WorldGridManager.h b/Game/WorldGridManager.h new file mode 100644 index 0000000..ad01747 --- /dev/null +++ b/Game/WorldGridManager.h @@ -0,0 +1,30 @@ +#pragma once +#include + +#include "structs.h" + +static const int WORLD_WIDTH = 34; +static const int WORLD_HEIGHT = 34; + +static const int TILE_WIDTH = 50; +static const int TILE_HEIGHT = 50; + +class WorldTile; + +class WorldGridManager +{ +public: + WorldGridManager(); + ~WorldGridManager(); + + WorldTile* GetTileAtIndex(const int x, const int y) const; + WorldTile* GetTileAtWorldPos(const Point2f& pos) const; + + void SetTileAtIndex(const int x, const int y, WorldTile* tile); + +private: + + std::array, WORLD_HEIGHT> m_worldTiles; + + +}; diff --git a/Game/WorldLevel.cpp b/Game/WorldLevel.cpp index f6ceec9..9bf2d10 100644 --- a/Game/WorldLevel.cpp +++ b/Game/WorldLevel.cpp @@ -15,14 +15,15 @@ WorldLevel::WorldLevel(Camera* camera, Rectf viewport): Level(camera), m_player(Player { Point2f { 0, 100 } }), m_mousePos { 0, 0 }, - m_viewport(viewport) + m_viewport(viewport), + m_gridManager(WorldGridManager()) { // 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) { int actualX = x - WORLD_WIDTH / 2; Point2f pos = Point2f{ float(actualX * TILE_WIDTH), -float(y * TILE_HEIGHT) - TILE_HEIGHT}; - m_worldTiles[x][y] = new WorldTile{ pos, GroundTileTypes::Dirt, TextureManager::GetInstance() }; + m_gridManager.SetTileAtIndex(x,y, new WorldTile{ pos, GroundTileTypes::Dirt, TextureManager::GetInstance() }); } } @@ -43,8 +44,8 @@ void WorldLevel::Update(float elapsedSec) { for (size_t x { 0 }; x < WORLD_WIDTH; ++x) { for (size_t y { 0 }; y < WORLD_HEIGHT; ++y) { - if(m_worldTiles[x][y]->GetCollisionRect().Contains(m_mousePos)) { - selectedTile = m_worldTiles[x][y]; + if(m_gridManager.GetTileAtIndex(x,y)->GetCollisionRect().Contains(m_mousePos)) { + selectedTile = m_gridManager.GetTileAtIndex(x,y); } } } @@ -56,6 +57,11 @@ void WorldLevel::Update(float elapsedSec) { m_player.Update(elapsedSec, *this); + WorldTile* t = m_gridManager.GetTileAtWorldPos(m_mousePos); + if(t != nullptr) { + t->SetTileType(GroundTileTypes::Air); + } + //Point2f playerPos = m_player.GetPosition(); //Point2f newCameraPos = playerPos; //m_pCamera->SetPosition(newCameraPos); @@ -81,15 +87,15 @@ void WorldLevel::Draw() const { for (size_t x { 0 }; x < WORLD_WIDTH; ++x) { for (size_t y { 0 }; y < WORLD_HEIGHT; ++y) { - m_worldTiles[x][y]->Draw(); + m_gridManager.GetTileAtIndex(x,y)->Draw(); } } //loop over worldtiles for (int x { 0 }; x < WORLD_WIDTH; ++x) { for (int y { 0 }; y < WORLD_HEIGHT; ++y) { - if(m_worldTiles[x][y]->GetTileType() == GroundTileTypes::Dirt) { - Collision::CollisionRect rect = m_worldTiles[x][y]->GetCollisionRect().getCollisionRect(); + if(m_gridManager.GetTileAtIndex(x,y)->GetTileType() == GroundTileTypes::Dirt) { + Collision::CollisionRect rect = m_gridManager.GetTileAtIndex(x,y)->GetCollisionRect().getCollisionRect(); utils::SetColor(Colors::GREEN); utils::DrawRect(rect.pos, rect.size.x, rect.size.y); } @@ -153,16 +159,7 @@ void WorldLevel::ProcessImGui() { // ImGui::Text("Size: (%f, %f)", contact.second->size.x, contact.second->size.y); // } ImGui::End(); + + } } -WorldTile* WorldLevel::GetTileAt(const Point2f& pos) const { - return m_worldTiles[pos.x][pos.y]; -} -void WorldLevel::SetTileAt(const Point2f& pos, WorldTile* tile) { -} -std::array, WorldLevel::WORLD_HEIGHT> WorldLevel::GetAllTiles() const { - return m_worldTiles; -} - - - diff --git a/Game/WorldLevel.h b/Game/WorldLevel.h index 7be3832..5a5d1d3 100644 --- a/Game/WorldLevel.h +++ b/Game/WorldLevel.h @@ -8,15 +8,11 @@ #include "Collision.h" #include "Player.h" #include "utils.h" +#include "WorldGridManager.h" class WorldLevel : public Level { public: - static const int WORLD_WIDTH = 34; - static const int WORLD_HEIGHT = 34; - - static const int TILE_WIDTH = 50; - static const int TILE_HEIGHT = 50; WorldLevel(Camera* camera, Rectf viewport); ~WorldLevel() override; @@ -27,22 +23,14 @@ public: void MouseMove(const Point2f& mousePos) override; void ProcessImGui() override; - WorldTile* GetTileAt(const Point2f& pos) const; - void SetTileAt(const Point2f& pos, WorldTile* tile); - - std::array, WORLD_HEIGHT> GetAllTiles() const; + WorldGridManager& GetGridManager() { return m_gridManager; } std::vector m_Rects; - std::array, WORLD_HEIGHT> GetWorldTiles() const { return m_worldTiles; } - - - private: - std::array, WORLD_HEIGHT> m_worldTiles; - + WorldGridManager m_gridManager{}; Player m_player; Point2f m_mousePos{}; diff --git a/Game/WorldTile.cpp b/Game/WorldTile.cpp index 2c53afc..a2eada6 100644 --- a/Game/WorldTile.cpp +++ b/Game/WorldTile.cpp @@ -1,6 +1,7 @@ #include "pch.h" #include "WorldTile.h" +#include "colors.h" #include "TextureManager.h" #include "utils.h" @@ -20,6 +21,11 @@ void WorldTile::Draw() const { m_pTexture->Draw(m_Position); //utils::SetColor(Color4f{ 0.5f, 0.5f, 0.5f, 1.0f}); //utils::FillRect(m_Position.x, m_Position.y, 50, 50); + + if(m_Hightlight) { + utils::SetColor(Colors::GREEN); + utils::FillRect(m_Position, 50, 50); + } } } Collision::TileCollisionRect WorldTile::GetCollisionRect() { diff --git a/Game/WorldTile.h b/Game/WorldTile.h index eb4f351..99d1081 100644 --- a/Game/WorldTile.h +++ b/Game/WorldTile.h @@ -26,6 +26,8 @@ public: void SetTileType(GroundTileTypes type) { m_GroundTileType = type; } Collision::TileCollisionRect GetCollisionRect(); + + bool m_Hightlight{ false }; private: