From 2e7db79fdf38af9f35ffaa3c2fab5628beacd7b7 Mon Sep 17 00:00:00 2001 From: jonylu7 Date: Thu, 25 Apr 2024 10:46:10 +0800 Subject: [PATCH] fix player --- CMakeLists.txt | 12 +++++-- include/Mechanics/CursorSelection.hpp | 41 +--------------------- include/Mechanics/GameObjectManager.hpp | 26 +++++--------- include/Mechanics/Player.hpp | 22 ++++++++---- src/Mechanics/CursorSelection.cpp | 46 +++++++++++++++++++++++++ src/Scene/DefaultScene.cpp | 2 +- src/UI/UI.cpp | 5 +-- src/UI/UIScriptProcess.cpp | 12 +++---- 8 files changed, 91 insertions(+), 75 deletions(-) create mode 100644 src/Mechanics/CursorSelection.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 82ec17cb..5555a49a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -100,17 +100,21 @@ set(SRC_FILES ${SRC_DIR}/Unit/Hunter.cpp ${SRC_DIR}/Unit/PathUtility.cpp ${SRC_DIR}/Unit/PathFindingUnit.cpp - ${SRC_DIR}/Mechanics/FindValidPathToDest.cpp + ${SRC_DIR}/Scene/DefaultScene.cpp ${SRC_DIR}/Camera.cpp ${SRC_DIR}/SpriteSheet.cpp ${SRC_DIR}/UI/UI.cpp - ${SRC_DIR}/Mechanics/GameObjectID.cpp + ${SRC_DIR}/Map/YAMLReader.cpp ${SRC_DIR}/Map/TerrainConfig.cpp ${SRC_DIR}/Map/MapbinReader.cpp ${SRC_DIR}/Map/Map.cpp + ${SRC_DIR}/Mechanics/FindValidPathToDest.cpp + ${SRC_DIR}/Mechanics/GameObjectID.cpp + ${SRC_DIR}/Mechanics/CursorSelection.cpp + ) set(INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include) set(INCLUDE_FILES @@ -159,9 +163,11 @@ set(INCLUDE_FILES ${INCLUDE_DIR}/Scene/MapScene.hpp - ${INCLUDE_DIR}/App.hpp ${INCLUDE_DIR}/Mechanics/GameObjectManager.hpp ${INCLUDE_DIR}/Mechanics/GameObjectID.hpp + ${INCLUDE_DIR}/Mechanics/CursorSelection.hpp + + ${INCLUDE_DIR}/App.hpp ${INCLUDE_DIR}/Tile.hpp ${INCLUDE_DIR}/config.hpp ${INCLUDE_DIR}/DrawOverlays.hpp diff --git a/include/Mechanics/CursorSelection.hpp b/include/Mechanics/CursorSelection.hpp index 719d9bf7..217f6c06 100644 --- a/include/Mechanics/CursorSelection.hpp +++ b/include/Mechanics/CursorSelection.hpp @@ -13,46 +13,7 @@ class CursorSelection { CursorSelection() {} virtual ~CursorSelection() {} void CursorSelect(std::shared_ptr m_Map, glm::vec2 *start, - glm::vec2 *end) { - - if (Util::Input::IsKeyDown(Util::Keycode::MOUSE_LB)) { - *start = Util::Input::GetCursorPosition(); - } - if (Util::Input::IsKeyPressed(Util::Keycode::MOUSE_LB)) { - // clear up last selected - for (auto i : lastSeletctedObjects) { - i->setSelected(false); - } - lastSeletctedObjects.clear(); - - // get cursor position - *end = Util::Input::GetCursorPosition(); - - // select objects - auto startcell = MapUtil::GlobalCoordToCellCoord( - MapUtil::ScreenToGlobalCoord(*start)); - auto endcell = MapUtil::GlobalCoordToCellCoord( - MapUtil::ScreenToGlobalCoord(*end)); - - int max_x_cell = std::max(startcell.x, endcell.x); - int max_y_cell = std::max(startcell.y, endcell.y); - int min_x_cell = std::min(startcell.x, endcell.x); - int min_y_cell = std::min(startcell.y, endcell.y); - - for (int i = min_y_cell; i <= max_y_cell; i++) { - for (int j = min_x_cell; j <= max_x_cell; j++) { - auto objects = m_Map->getTileByCellPosition(glm::vec2(j, i)) - ->getSelectableObjects(); - for (auto i : objects) { - if (i->getSelected() == false) { - i->setSelected(true); - lastSeletctedObjects.push_back(i); - } - } - } - } - } - } + glm::vec2 *end); protected: std::vector> lastSeletctedObjects; diff --git a/include/Mechanics/GameObjectManager.hpp b/include/Mechanics/GameObjectManager.hpp index 992f514d..5053dcc7 100644 --- a/include/Mechanics/GameObjectManager.hpp +++ b/include/Mechanics/GameObjectManager.hpp @@ -9,8 +9,8 @@ #include "GameObjectID.hpp" #include "Mechanics/Constructing.hpp" #include "Mechanics/CursorSelection.hpp" +#include "Mechanics/Player.hpp" #include "Mechanics/StructureArray.hpp" -#include "Player.hpp" #include "Structure/AdvencePowerPlants.hpp" #include "Structure/Barracks.hpp" #include "Structure/OreRefinery.hpp" @@ -22,15 +22,14 @@ #include #include -class GameObjectManager : public Constructing, public CursorSelection { +class GameObjectManager : public Constructing, + public CursorSelection, + public Player { public: GameObjectManager() {} ~GameObjectManager() {} - void Start(std::shared_ptr map, std::shared_ptr player, - std::shared_ptr cursor) { + void Start(std::shared_ptr map) { m_Map = map; - m_Player = player; - m_Cursor = cursor; for (auto pair : m_BuiltStructure) { pair->Start(); @@ -68,15 +67,6 @@ class GameObjectManager : public Constructing, public CursorSelection { void RemoveStructByID(const GameObjectID id) {} - int GetTotalPower() { - int totalPower = 0; - for (int i = 0; i < m_BuiltStructure.size(); i++) { - totalPower += m_BuiltStructure[i]->GetElectricPower(); - } - return totalPower; - } - - float GetTotalCurrency() { return m_Player->getTotalCurrency(); } std::vector> getStructureArray() { return m_BuiltStructure; } @@ -93,12 +83,14 @@ class GameObjectManager : public Constructing, public CursorSelection { } } + int getTotalPower() { + return Player::getTotalPower(this->m_BuiltStructure); + } + private: std::vector> m_UnitArray; FindValidPathToDest m_wayPointUnit; std::shared_ptr m_Map = std::make_shared(); - std::shared_ptr m_Player; - std::shared_ptr m_Cursor; std::chrono::high_resolution_clock::time_point m_StartTime; double m_lastElapsed = 0.F; }; diff --git a/include/Mechanics/Player.hpp b/include/Mechanics/Player.hpp index 0c0b4efc..75a4efe4 100644 --- a/include/Mechanics/Player.hpp +++ b/include/Mechanics/Player.hpp @@ -4,6 +4,7 @@ #ifndef PRACTICALTOOLSFORSIMPLEDESIGN_PLAYER_HPP #define PRACTICALTOOLSFORSIMPLEDESIGN_PLAYER_HPP +#include "Structure/Structure.hpp" class Player { public: Player() {} @@ -12,16 +13,25 @@ class Player { void setTotalCurrency(int value) { m_totalCurrency = value; }; void addCurrency(int value) { m_totalCurrency += value; }; - void setTotalPower(int value){m_totalPower=value;} - void addPower(int value){m_totalPower+=value;} + void setTotalPower(int value) { m_totalPower = value; } + void addPower(int value) { m_totalPower += value; } - int getTotalPower() {return m_totalPower;} int getTotalCurrency() { return m_totalCurrency; } - int getMaxTroopSize(){return m_maxTroopSize;} + int getMaxTroopSize() { return m_maxTroopSize; } -private: +protected: + int + getTotalPower(std::vector> m_BuiltStructure) { + int totalPower = 0; + for (int i = 0; i < m_BuiltStructure.size(); i++) { + totalPower += m_BuiltStructure[i]->GetElectricPower(); + } + return totalPower; + } + +protected: int m_maxTroopSize = 200; - int m_totalPower = 0 ; + int m_totalPower = 0; int m_totalCurrency = 0; }; #endif // PRACTICALTOOLSFORSIMPLEDESIGN_PLAYER_HPP diff --git a/src/Mechanics/CursorSelection.cpp b/src/Mechanics/CursorSelection.cpp new file mode 100644 index 00000000..9a6f85b2 --- /dev/null +++ b/src/Mechanics/CursorSelection.cpp @@ -0,0 +1,46 @@ +// +// Created by 盧威任 on 4/25/24. +// +#include "Mechanics/CursorSelection.hpp" + +void CursorSelection::CursorSelect(std::shared_ptr m_Map, + glm::vec2 *start, glm::vec2 *end) { + + if (Util::Input::IsKeyDown(Util::Keycode::MOUSE_LB)) { + *start = Util::Input::GetCursorPosition(); + } + if (Util::Input::IsKeyPressed(Util::Keycode::MOUSE_LB)) { + // clear up last selected + for (auto i : lastSeletctedObjects) { + i->setSelected(false); + } + lastSeletctedObjects.clear(); + + // get cursor position + *end = Util::Input::GetCursorPosition(); + + // select objects + auto startcell = MapUtil::GlobalCoordToCellCoord( + MapUtil::ScreenToGlobalCoord(*start)); + auto endcell = + MapUtil::GlobalCoordToCellCoord(MapUtil::ScreenToGlobalCoord(*end)); + + int max_x_cell = std::max(startcell.x, endcell.x); + int max_y_cell = std::max(startcell.y, endcell.y); + int min_x_cell = std::min(startcell.x, endcell.x); + int min_y_cell = std::min(startcell.y, endcell.y); + + for (int i = min_y_cell; i <= max_y_cell; i++) { + for (int j = min_x_cell; j <= max_x_cell; j++) { + auto objects = m_Map->getTileByCellPosition(glm::vec2(j, i)) + ->getSelectableObjects(); + for (auto i : objects) { + if (i->getSelected() == false) { + i->setSelected(true); + lastSeletctedObjects.push_back(i); + } + } + } + } + } +} diff --git a/src/Scene/DefaultScene.cpp b/src/Scene/DefaultScene.cpp index ff2abe4b..e7eebff8 100644 --- a/src/Scene/DefaultScene.cpp +++ b/src/Scene/DefaultScene.cpp @@ -18,7 +18,7 @@ void DefaultScene::Start() { // m_GameObjectManager.Start(); // m_dummy.Start({5, 5}, m_Map); - m_GameObjectManager->Start(m_Map, m_Player, m_Cursor); + m_GameObjectManager->Start(m_Map); // m_Cursor.Start(m_Map); m_UI.Start(m_Map, m_Player, m_GameObjectManager); m_Player->setTotalCurrency(5000); diff --git a/src/UI/UI.cpp b/src/UI/UI.cpp index 48160bd9..aa0f2fdd 100644 --- a/src/UI/UI.cpp +++ b/src/UI/UI.cpp @@ -39,9 +39,10 @@ void UIClass::ShowPlayerStatus() { .c_str()); // ImGui::Text(fmt::format("Zoom: {}", // m_SceneCamera.getCameraZoom()).c_str()); - ImGui::Text(fmt::format("$ {}", m_Player->getTotalCurrency()).c_str()); ImGui::Text( - fmt::format("Power {}", m_gameObjectManager->GetTotalPower()).c_str()); + fmt::format("$ {}", m_gameObjectManager->getTotalCurrency()).c_str()); + ImGui::Text( + fmt::format("Power {}", m_gameObjectManager->getTotalPower()).c_str()); ImGui::PushFont(sacker_med); } diff --git a/src/UI/UIScriptProcess.cpp b/src/UI/UIScriptProcess.cpp index 673e0869..74c64148 100644 --- a/src/UI/UIScriptProcess.cpp +++ b/src/UI/UIScriptProcess.cpp @@ -56,9 +56,9 @@ void UIScriptProcess::CountDown() { // Structure Building std::chrono::duration buildElapsed = m_currentCountDownTime - m_buildStartTime; - if (m_gameObjectManager->GetTotalCurrency() <= 0 && + if (m_gameObjectManager->getTotalCurrency() <= 0 && b_isBuildingInCoolDown && buildElapsed.count() < buildCoolDownTime) { - if (m_gameObjectManager->GetTotalPower() <= 0) { + if (m_gameObjectManager->getTotalPower() <= 0) { SetBuildCountDown(buildCoolDownTime / 2 - buildElapsed.count()); } else { SetBuildCountDown(buildCoolDownTime - buildElapsed.count()); @@ -90,9 +90,9 @@ void UIScriptProcess::CountDown() { // Unit Spawning std::chrono::duration spawnElapsed = m_currentCountDownTime - m_SpawnStartTime; - if (m_gameObjectManager->GetTotalCurrency() <= 0 && + if (m_gameObjectManager->getTotalCurrency() <= 0 && b_isSpawningInCooldown && spawnElapsed.count() < spawnCoolDownTime) { - if (m_gameObjectManager->GetTotalPower() <= 0) { + if (m_gameObjectManager->getTotalPower() <= 0) { SetSpawnCountDown(spawnCoolDownTime / 2 - spawnElapsed.count()); } else { SetSpawnCountDown(spawnCoolDownTime - spawnElapsed.count()); @@ -235,14 +235,14 @@ std::shared_ptr UIScriptProcess::spawnAvatar() { } float UIScriptProcess::GetBuildCountDownTime() { - if (m_gameObjectManager->GetTotalPower() <= 0) { + if (m_gameObjectManager->getTotalPower() <= 0) { return m_offPowerBuildCoolDownTime; } else { return m_buildCoolDownTime; } } float UIScriptProcess::GetSpawnCountDownTime() { - if (m_gameObjectManager->GetTotalPower() <= 0) { + if (m_gameObjectManager->getTotalPower() <= 0) { return m_offPowerSpawnCoolDownTime; } else { return m_spawnCoolDownTime;