From 3771ea4a27ceb88cc9fbaecea895530604159301 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Sun, 19 Sep 2021 18:20:26 +0200 Subject: [PATCH] refactor: move tower place detection to World.cpp --- include/game/World.h | 7 ++++- include/game/client/ClientGame.h | 4 +-- src/game/World.cpp | 46 +++++++++++++++++++++++++++++++- src/game/client/ClientGame.cpp | 44 ------------------------------ src/render/WorldRenderer.cpp | 4 +-- 5 files changed, 54 insertions(+), 51 deletions(-) diff --git a/include/game/World.h b/include/game/World.h index d78503b..edc4b19 100644 --- a/include/game/World.h +++ b/include/game/World.h @@ -4,6 +4,8 @@ #include #include #include +#include + #include "Mobs.h" #include "Team.h" @@ -140,7 +142,7 @@ public: void spawnMobAt(MobID id, MobType type, std::uint8_t level, PlayerID sender, float x, float y, Direction dir); - TilePtr getTile(std::int32_t x, std::int32_t y); + TilePtr getTile(std::int32_t x, std::int32_t y) const; const TowerTileColorPalette& getTowerTileColorPalette() const { return m_TowerPlacePalette; } const Color& getWalkableTileColor() const { return m_WalkablePalette; } @@ -154,6 +156,9 @@ public: return m_TilePalette.at(index - 1); } + bool CanPlaceLittleTower(const glm::vec2& worldPos) const; + bool CanPlaceBigTower(const glm::vec2& worldPos) const; + const std::unordered_map& getChunks() const { return m_Chunks; } const Color& getSpawnColor(TeamColor color) const { return m_SpawnColorPalette[(std::size_t)color]; } diff --git a/include/game/client/ClientGame.h b/include/game/client/ClientGame.h index cffaa9e..97a972c 100644 --- a/include/game/client/ClientGame.h +++ b/include/game/client/ClientGame.h @@ -33,14 +33,12 @@ public: std::uint32_t getLobbyTime() const { return m_LobbyTime; } const game::Player* getPlayer() const { return m_Player; } + const WorldClient& getWorld() const { return m_WorldClient; } render::Renderer* getRenderer() const { return m_Renderer; } void PlaceTower(game::TowerType type, const glm::vec2& position); - bool CanPlaceLittleTower(const glm::vec2& worldPos); - bool CanPlaceBigTower(const glm::vec2& worldPos); - virtual void HandlePacket(protocol::ConnexionInfoPacket* packet); virtual void HandlePacket(protocol::PlayerJoinPacket* packet); virtual void HandlePacket(protocol::PlayerLeavePacket* packet); diff --git a/src/game/World.cpp b/src/game/World.cpp index 3938abe..a6bd570 100644 --- a/src/game/World.cpp +++ b/src/game/World.cpp @@ -20,7 +20,7 @@ World::World(Game* game) : m_Game(game) { #endif } -TilePtr World::getTile(std::int32_t x, std::int32_t y) { +TilePtr World::getTile(std::int32_t x, std::int32_t y) const { std::int16_t chunkX = x / Chunk::ChunkWidth; std::int16_t chunkY = y / Chunk::ChunkHeight; @@ -389,6 +389,50 @@ const Color* World::getTileColor(TilePtr tile) const { return nullptr; } +bool World::CanPlaceLittleTower(const glm::vec2& worldPos) const { + TilePtr tile = getTile(worldPos.x, worldPos.y); + + if (tile == nullptr) { + return false; + } + + if (tile->getType() == game::TileType::Tower) { + for (int x = -1; x < 2; x++) { + for (int y = -1; y < 2; y++) { + game::TilePtr adjacentTile = getTile(worldPos.x + x, worldPos.y + y); + if (adjacentTile == nullptr || adjacentTile->getType() != game::TileType::Tower) { + return false; + } + } + } + return true; + } + + return false; +} + +bool World::CanPlaceBigTower(const glm::vec2& worldPos) const { + TilePtr tile = getTile(worldPos.x, worldPos.y); + + if (tile == nullptr) { + return false; + } + + if (tile->getType() == game::TileType::Tower) { + for (int x = -2; x < 3; x++) { + for (int y = -2; y < 3; y++) { + game::TilePtr adjacentTile = getTile(worldPos.x + x, worldPos.y + y); + if (adjacentTile == nullptr || adjacentTile->getType() != game::TileType::Tower) { + return false; + } + } + } + return true; + } + + return false; +} + Team& World::getRedTeam() { return m_Game->getRedTeam(); } diff --git a/src/game/client/ClientGame.cpp b/src/game/client/ClientGame.cpp index d904e60..cb6ce39 100644 --- a/src/game/client/ClientGame.cpp +++ b/src/game/client/ClientGame.cpp @@ -116,49 +116,5 @@ void ClientGame::PlaceTower(game::TowerType type, const glm::vec2& position) { m_Client->getConnexion().sendPacket(&packet); } -bool ClientGame::CanPlaceLittleTower(const glm::vec2& worldPos) { - game::TilePtr tile = m_WorldClient.getTile(worldPos.x, worldPos.y); - - if (tile == nullptr) { - return false; - } - - if (tile->getType() == game::TileType::Tower) { - for (int x = -1; x < 2; x++) { - for (int y = -1; y < 2; y++) { - game::TilePtr adjacentTile = m_WorldClient.getTile(worldPos.x + x, worldPos.y + y); - if (adjacentTile == nullptr || adjacentTile->getType() != game::TileType::Tower) { - return false; - } - } - } - return true; - } - - return false; -} - -bool ClientGame::CanPlaceBigTower(const glm::vec2& worldPos) { - game::TilePtr tile = m_WorldClient.getTile(worldPos.x, worldPos.y); - - if (tile == nullptr) { - return false; - } - - if (tile->getType() == game::TileType::Tower) { - for (int x = -2; x < 3; x++) { - for (int y = -2; y < 3; y++) { - game::TilePtr adjacentTile = m_WorldClient.getTile(worldPos.x + x, worldPos.y + y); - if (adjacentTile == nullptr || adjacentTile->getType() != game::TileType::Tower) { - return false; - } - } - } - return true; - } - - return false; -} - } // namespace client } // namespace td diff --git a/src/render/WorldRenderer.cpp b/src/render/WorldRenderer.cpp index a1e1a72..059e983 100644 --- a/src/render/WorldRenderer.cpp +++ b/src/render/WorldRenderer.cpp @@ -111,7 +111,7 @@ void WorldRenderer::changeZoom(float zoomStep) { } void WorldRenderer::click() { - if (m_Client->CanPlaceLittleTower(getClickWorldPos())) { + if (m_Client->getWorld().CanPlaceLittleTower(getClickWorldPos())) { ImGui::OpenPopup("TowerPlace"); m_TowerPlacePopupOpened = true; } @@ -127,7 +127,7 @@ void WorldRenderer::renderPopups() const { for (int i = 0; i < (int)game::TowerType::TowerCount; i++) { game::TowerType towerType = game::TowerType(i); const game::TowerInfo& towerInfo = game::getTowerInfo(towerType); - if (!towerInfo.isBigTower() || (towerInfo.isBigTower() && m_Client->CanPlaceBigTower(getClickWorldPos()))) { + if (!towerInfo.isBigTower() || (towerInfo.isBigTower() && m_Client->getWorld().CanPlaceBigTower(getClickWorldPos()))) { if (ImGui::Button(game::getTowerInfo(towerType).getName().c_str())) { m_Client->PlaceTower(towerType, getClickWorldPos()); ImGui::CloseCurrentPopup();