From 229bfd4969cd9fbc16baca1726d4c83e2dc41e97 Mon Sep 17 00:00:00 2001 From: git Date: Sun, 24 Mar 2024 16:58:47 +0800 Subject: [PATCH] button update --- include/GameObjectManager.hpp | 13 ++++++++- include/Structure/Barracks.hpp | 2 +- include/UI/UI.hpp | 20 +++++++++++++ include/UI/UIScriptProcess.hpp | 22 +++++++++++++++ include/Unit/Infantry.hpp | 2 +- src/Scene/DefaultScene.cpp | 9 ++++-- src/UI/UI.cpp | 51 +++++++++++++++++++++++++++++++++- src/UI/UIScriptProcess.cpp | 45 ++++++++++++++++++++++++++++++ 8 files changed, 158 insertions(+), 6 deletions(-) diff --git a/include/GameObjectManager.hpp b/include/GameObjectManager.hpp index 1112143c..d4710f8e 100644 --- a/include/GameObjectManager.hpp +++ b/include/GameObjectManager.hpp @@ -11,6 +11,7 @@ #include "Structure/PowerPlants.hpp" #include "Structure/Structure.hpp" #include "Structure/WarFactory.hpp" +#include "Unit/Avatar.hpp" #include #include class GameObjectManager { @@ -30,12 +31,20 @@ class GameObjectManager { for (auto pair : m_BuiltStructure) { pair->Update(); } + for (auto unit : m_UnitArray){ + unit->Update(); + printf("(GOM) update back success\n"); + } } void Append(std::shared_ptr newstruct) { newstruct->Start(); m_BuiltStructure.push_back(newstruct); } + void unitAppend(std::shared_ptr newUnit) { + m_UnitArray.push_back(newUnit); + printf("(GOM) push back success\n"); + } void RemoveStructByID(const GameObjectID id) { for (int i = 0; i < m_BuiltStructure.size(); i++) { @@ -55,9 +64,11 @@ class GameObjectManager { return totalPower; } + std::vector> getStructureArray(){return m_BuiltStructure;} + private: std::vector> m_BuiltStructure; - // std::vector> m_UnitArray; + std::vector> m_UnitArray; }; #endif // PRACTICALTOOLSFORSIMPLEDESIGN_GAMEOBJECTMANAGER_HPP diff --git a/include/Structure/Barracks.hpp b/include/Structure/Barracks.hpp index bebe8798..9dfe2478 100644 --- a/include/Structure/Barracks.hpp +++ b/include/Structure/Barracks.hpp @@ -22,7 +22,7 @@ class Barracks : public Structure, public IWayPointStructure { std::vector m_lineVector; public: - Barracks(float electricPower = -20.F, float buildingTime = 15.F, + Barracks(float electricPower = -20.F, float buildingTime = 1.f/*15.F*/, float buildingCost = 300.F, float buildingHp = 800.F) : Structure(electricPower, buildingTime, buildingCost, buildingHp, unitType::BARRACKS){}; diff --git a/include/UI/UI.hpp b/include/UI/UI.hpp index 258771d7..ace4358d 100644 --- a/include/UI/UI.hpp +++ b/include/UI/UI.hpp @@ -33,6 +33,13 @@ class UIClass { bool getIfAnythingCanSelectToBuild(); // 避免Scene收到空的getSelectedBuilding + std::shared_ptr getUnitFromUI(); + bool getIfUnitReadyToSpawn(){return ButtonScript.getIfReadytoSpawn();} + void setIfUnitReadyToSpawn(bool b){ButtonScript.setIfReadytoSpawn(b);} + void importMap(std::shared_ptr m_Map){this->m_Map=m_Map;} + + //check if building has built + void checkExistBuilding(std::vector> buildingList); private: UIScriptProcess ButtonScript; void InitUnitQueue(); @@ -86,6 +93,19 @@ class UIClass { bool b_WarFactory = false; bool b_ADVPowerPlant = false; bool b_SelectToBuild = false; + + bool b_barackBuilt = false; + bool b_warfactoryBuilt = false; + bool b_orerefineryBuilt = false; + + glm::vec2 m_barrackCell; + glm::vec2 m_barrackTargetCell; + glm::vec2 m_warfactoryCell; + glm::vec2 m_warfactoryTargetCell; + glm::vec2 m_orerefineryCell; + glm::vec2 m_orerefineryTargetCell; + + std::shared_ptr m_Map; }; #endif // PRACTICALTOOLSFORSIMPLEDESIGN_UI_HPP diff --git a/include/UI/UIScriptProcess.hpp b/include/UI/UIScriptProcess.hpp index d2fd211e..2b295b6b 100644 --- a/include/UI/UIScriptProcess.hpp +++ b/include/UI/UIScriptProcess.hpp @@ -11,6 +11,8 @@ #include "Structure/PowerPlants.hpp" #include "Structure/Structure.hpp" #include "Structure/WarFactory.hpp" +#include "Unit/Avatar.hpp" +#include "Unit/Infantry.hpp" #include "Util/GameObject.hpp" #include #include @@ -18,11 +20,13 @@ class UIScriptProcess { private: + // building bool b_Baracks = false; bool b_OreRefinery = false; bool b_PowerPlants = false; bool b_WarFactory = false; bool b_ADVPowerPlant = false; + float TargetTime = 0.F; unitType m_currentStructure; bool b_STALL = false; @@ -30,6 +34,16 @@ class UIScriptProcess { std::chrono::time_point m_StartTime; std::chrono::time_point m_CountDownCurrentTime; + // building end + + // avatar + bool b_readytoSpawn = false; + float m_SpawnTime = 0.F; + unitType m_currentAvatar; + bool b_spawnInCD= false; + std::deque m_spawnQueue; + std::chrono::time_point m_SpawnStartTime; + // avatar end public: UIScriptProcess(){}; @@ -47,16 +61,24 @@ class UIScriptProcess { std::string GetFormattedCD(); // Event void AddToBuildQueue(unitType type); + void AddToSpawnQueue(unitType type); void Update(bool queueContinue); // CountDown void SetCountDown(float time); + void SetSpawnCountDown(float time); void CountDown(); // transform to ptr float GetStructureTime(unitType type); + float GetSpawnTime(unitType type); unitType GetCurrentStructure() { return m_currentStructure; }; + // spawn unit + std::shared_ptr spawnAvatar(); + bool getIfReadytoSpawn(){return b_readytoSpawn;} + void setIfReadytoSpawn(bool b){b_readytoSpawn=b;} + private: std::shared_ptr barracks = std::make_shared(); std::shared_ptr oreRefinery = std::make_shared(); diff --git a/include/Unit/Infantry.hpp b/include/Unit/Infantry.hpp index a5ad37d2..1c41da66 100644 --- a/include/Unit/Infantry.hpp +++ b/include/Unit/Infantry.hpp @@ -9,7 +9,7 @@ class Infantry:public Avatar{ public: Infantry():Avatar(){ - setHp(50); + //setHp(50); setMovementSpeed(4); } private: diff --git a/src/Scene/DefaultScene.cpp b/src/Scene/DefaultScene.cpp index 39910db2..778c0fdc 100644 --- a/src/Scene/DefaultScene.cpp +++ b/src/Scene/DefaultScene.cpp @@ -25,11 +25,11 @@ void DefaultScene::Start() { DrawOverlays::OverlayShapes::R_CROSS); // m_GameObjectManager.Start(); - m_dummy.Start({5, 5}, m_Map); + //m_dummy.Start({5, 5}, m_Map); } void DefaultScene::Update() { - m_dummy.Update(); + //m_dummy.Update(); m_Manager.Update(); @@ -58,4 +58,9 @@ void DefaultScene::Update() { if (m_UI.getIfAnythingCanSelectToBuild()) { m_Manager.Append(m_UI.getSelectedBuilding()); } + m_UI.checkExistBuilding(m_Manager.getStructureArray()); + m_UI.importMap(m_Map); + if(m_UI.getIfUnitReadyToSpawn()){ + m_Manager.unitAppend(m_UI.getUnitFromUI()); + } } diff --git a/src/UI/UI.cpp b/src/UI/UI.cpp index d41a0fd3..4829ea2a 100644 --- a/src/UI/UI.cpp +++ b/src/UI/UI.cpp @@ -218,7 +218,11 @@ void UIClass::ShowInfantryTab() { if (ImGui::BeginTabItem("Inf")) { if (getImageButtonBySpriteSheetIndex(m_InfantryIconSpriteSheet, 0)) { // rifle - setUnitConstructCount(unitType::INFANTRY, 1); + if(b_barackBuilt){ + ButtonScript.AddToSpawnQueue(unitType::INFANTRY); + setUnitConstructCount(unitType::INFANTRY, 1); + } + LOG_DEBUG("TEST"); } ImGui::SameLine(); @@ -252,6 +256,7 @@ void UIClass::ShowDefTab() { if (ImGui::BeginTabItem("Def")) { if (getImageButtonBySpriteSheetIndex(m_StructureIconSpriteSheet, 18)) { // sandbags + setUnitConstructCount(unitType::SANDBAGS, 1); LOG_DEBUG("TEST"); } @@ -432,3 +437,47 @@ bool UIClass::getIfAnythingCanSelectToBuild() { ButtonScript.GetIfFinished(unitType::WAR_FACT) || ButtonScript.GetIfFinished(unitType::ADV_POWER_PLANT)); } + +void UIClass::checkExistBuilding(std::vector> buildingList){ + b_barackBuilt= false; + b_warfactoryBuilt= false; + b_orerefineryBuilt= false; + if(buildingList.size()==0){ + return; + } + for(auto i:buildingList){ + if(std::dynamic_pointer_cast(i)&&!b_barackBuilt){ + m_barrackTargetCell=MapClass::GlobalCoordToCellCoord(std::dynamic_pointer_cast(i)->GetWayPointLocation()); + b_barackBuilt= true; + m_barrackCell=MapClass::GlobalCoordToCellCoord(i->GetObjectLocation()); + } + else if(std::dynamic_pointer_cast(i)&&!b_warfactoryBuilt){ + m_warfactoryTargetCell=MapClass::GlobalCoordToCellCoord(std::dynamic_pointer_cast(i)->GetWayPointLocation()); + b_warfactoryBuilt= true; + m_warfactoryCell=MapClass::GlobalCoordToCellCoord(i->GetObjectLocation()); + } + else if(std::dynamic_pointer_cast(i)&&!b_orerefineryBuilt){ + m_orerefineryTargetCell=MapClass::GlobalCoordToCellCoord(std::dynamic_pointer_cast(i)->GetWayPointLocation()); + b_orerefineryBuilt= true; + m_orerefineryCell=MapClass::GlobalCoordToCellCoord(i->GetObjectLocation()); + } + } +} + +std::shared_ptr UIClass::getUnitFromUI(){ + printf("(UI)return to GOM\n"); + auto Avatar = ButtonScript.spawnAvatar(); + ButtonScript.setIfReadytoSpawn(false); + if(std::dynamic_pointer_cast(Avatar)){ + Avatar->Start(m_barrackCell,m_Map); + Avatar->setNewDestination(m_barrackTargetCell); + } /*else if(std::dynamic_pointer_cast< >(Avatar)){ + Avatar->Start(m_warfactoryCell,m_Map); + Avatar->setNewDestination(m_warfactoryTargetCell); + } else if(std::dynamic_pointer_cast< >(Avatar)){ + Avatar->Start(m_orerefineryCell,m_Map); + Avatar->setNewDestination(m_orerefineryTargetCell); + }*/ + printf("(UI)return to GOM success\n"); + return Avatar; +} \ No newline at end of file diff --git a/src/UI/UIScriptProcess.cpp b/src/UI/UIScriptProcess.cpp index 1062fa1c..d629b8de 100644 --- a/src/UI/UIScriptProcess.cpp +++ b/src/UI/UIScriptProcess.cpp @@ -52,21 +52,37 @@ void UIScriptProcess::CountDown() { m_CountDownCurrentTime = std::chrono::high_resolution_clock::now(); std::chrono::duration elapsed = m_CountDownCurrentTime - m_StartTime; + std::chrono::duration unitElapsed = + m_CountDownCurrentTime - m_SpawnStartTime; if (b_STALL) { // printf("(Button) CD: %.2f,%s\n", TargetTime - elapsed.count(), // elapsed.count() >= TargetTime ? "True" : "False"); } + if (b_spawnInCD) { + printf("(UISC) CD: %.2f,%s\n", unitElapsed.count(), + elapsed.count() >= m_SpawnTime ? "True" : "False"); + } if (elapsed.count() >= TargetTime && b_STALL) { // printf("(Button) Construction Finished\n"); SetIfFinished(m_currentStructure, true); b_STALL = false; return; } + if (unitElapsed.count() >= m_SpawnTime && b_spawnInCD) { + b_readytoSpawn=true; + b_spawnInCD = false; + printf("(UISC)Unit Ready\n"); + return; + } } void UIScriptProcess::SetCountDown(float time) { TargetTime = time; m_StartTime = std::chrono::high_resolution_clock::now(); } +void UIScriptProcess::SetSpawnCountDown(float time) { + m_SpawnTime = time; + m_SpawnStartTime = std::chrono::high_resolution_clock::now(); +} void UIScriptProcess::AddToBuildQueue(unitType type) { if (GetIfFinished(type)) { @@ -85,6 +101,12 @@ void UIScriptProcess::Update(bool queueContinue) { b_STALL = true; SetCountDown(GetStructureTime(m_currentStructure)); } + if (m_spawnQueue.size() !=0 && !b_spawnInCD) { + m_currentAvatar = m_spawnQueue.front(); + m_spawnQueue.pop_front(); + b_spawnInCD = true; + SetSpawnCountDown(GetSpawnTime(m_currentAvatar)); + } CountDown(); } @@ -129,3 +151,26 @@ void UIScriptProcess::SetIfFinished(unitType type, bool value) { break; } } + +void UIScriptProcess::AddToSpawnQueue(unitType type){ + printf("(UISC)Add Spawn Queue\n"); + m_spawnQueue.push_back(type); + return; + +} +float UIScriptProcess::GetSpawnTime(unitType type){ + switch (type) { + case unitType::INFANTRY:{ + return 5.F; + } + } +} + +std::shared_ptr UIScriptProcess::spawnAvatar(){ + printf("(UISC)spawnAvatar\n"); + switch (m_currentAvatar) { + case unitType::INFANTRY:{ + return std::make_unique(); + } + } +} \ No newline at end of file