From 680735450526ec564b2963aaa8876631b73e0ef2 Mon Sep 17 00:00:00 2001 From: git Date: Fri, 15 Mar 2024 07:52:38 +0800 Subject: [PATCH 1/3] button update --- include/Scene/DefaultScene.hpp | 1 + include/Structure/Structure.hpp | 4 ++- include/UI/UI.hpp | 3 ++ include/UI/UIScriptProcess.h | 13 +++------ src/Scene/DefaultScene.cpp | 50 ++++++++++++++++++--------------- src/UI/UI.cpp | 35 ++++++++++++++++------- src/UI/UIScriptProcess.cpp | 23 +++++++++++++-- 7 files changed, 83 insertions(+), 46 deletions(-) diff --git a/include/Scene/DefaultScene.hpp b/include/Scene/DefaultScene.hpp index 5ce740d1..a48f38de 100644 --- a/include/Scene/DefaultScene.hpp +++ b/include/Scene/DefaultScene.hpp @@ -50,6 +50,7 @@ class DefaultScene { std::vector m_OgMap; std::shared_ptr m_TileSetSpriteSheet = std::make_shared(); + std::vector> m_BuiltStructure; }; #endif // PRACTICALTOOLSFORSIMPLEDESIGN_DEFAULTSCENE_HPP diff --git a/include/Structure/Structure.hpp b/include/Structure/Structure.hpp index 26fbaf9a..9704fccb 100644 --- a/include/Structure/Structure.hpp +++ b/include/Structure/Structure.hpp @@ -28,6 +28,7 @@ class Structure : public Util::GameObject { m_CurrentState = updateMode::Invisidable; }; + Structure(float electricPower, float buildingTime, float buildingCost, float buildingHp, GameObjectID id) : electricPower(electricPower), @@ -38,6 +39,7 @@ class Structure : public Util::GameObject { m_Transform.scale = {1, 1}; // this->SetZIndex(DEFAULT_ZINDEX); }; + ~Structure(){}; void Update() override; @@ -58,7 +60,7 @@ class Structure : public Util::GameObject { virtual void onSelected(bool selected); virtual void attachmentUpdate(); // this function now will update // attachment's location and draw as well - + bool getStarted(){ if(m_CurrentState==updateMode::Fixed){return true;}else{return false;}} /* void SetElectricPower(float electricPower); void SetBuildingTime(float buildingTime); diff --git a/include/UI/UI.hpp b/include/UI/UI.hpp index 84f20d72..12fbfec3 100644 --- a/include/UI/UI.hpp +++ b/include/UI/UI.hpp @@ -25,6 +25,9 @@ class UIClass { static void setUnitConstructCount(unitType type, int value) { s_unitConstructCount[type] += value; } + static void resetUnitConstructCount(unitType type) { + s_unitConstructCount[type] =0; + } private: void InitUnitQueue(); diff --git a/include/UI/UIScriptProcess.h b/include/UI/UIScriptProcess.h index 83d43db7..c1ccefc9 100644 --- a/include/UI/UIScriptProcess.h +++ b/include/UI/UIScriptProcess.h @@ -10,6 +10,8 @@ #include "Structure/PowerPlants.hpp" #include "Structure/WarFactory.hpp" #include "Structure/AdvencePowerPlants.hpp" +#include "UI.hpp" +#include "GameObjectID.hpp" #include #include #include @@ -23,10 +25,12 @@ class UIScriptProcess{ bool b_STALL=false; std::queue> buildQueue; std::shared_ptr temp_PTR; + std::chrono::time_point m_StartTime; public: UIScriptProcess(){}; // bool GetIfFinished(std::shared_ptr structure); + bool GetIfFinished(unitType type); void SetFinished(std::shared_ptr structure); //Event void buttonEvent(std::shared_ptr(m_Structure)); @@ -36,14 +40,5 @@ class UIScriptProcess{ //CountDown void SetCoolDown(float time); void CountDown(); - //Delay?? - std::future asyncUpdate() { - return std::async(std::launch::async, [this] { - while (b_STALL) { - Counter+=0.1F; - std::this_thread::sleep_for(std::chrono::milliseconds(100)); // 模拟每秒调用一次update - } - }); - } }; #endif // PRACTICALTOOLSFORSIMPLEDESIGN_UISCRIPTPROCESS_H diff --git a/src/Scene/DefaultScene.cpp b/src/Scene/DefaultScene.cpp index bdba8209..636ea8fe 100644 --- a/src/Scene/DefaultScene.cpp +++ b/src/Scene/DefaultScene.cpp @@ -8,30 +8,7 @@ void DefaultScene::Start() { // image.SetImage("../assets/sprites/Shapes/B_Box.png"); OccupiedID::InitID(); LOG_TRACE("Start"); - /* - m_GameObjectList[0]->SetDrawable( - std::make_unique("../assets/sprites/Raccoon3.jpg")); - m_GameObjectList[0]->SetZIndex(10); - m_GameObjectList[1]->SetDrawable( - std::make_unique("../assets/sprites/capybara.png")); - - m_Structure->SetDrawable( - std::make_unique("../assets/sprites/barracks.png")); - m_Structure->SetZIndex(0); - m_Structure->SetObjectLocation({0, 0}); - m_Structure->Start(); - m_Structure->SetCurrentUpdateMode(Structure::updateMode::Moveable); - m_Renderer.AddChild(m_Structure); - - m_Inf->Start(); - - for (auto i : m_GameObjectList) { - i->Start(); - } - - m_Renderer.AddChild(m_Structure); - */ // init map m_TileSetSpriteSheet->Start("../assets/sprites/TILESET_Field.png", 24, 24, 20, 0); @@ -53,6 +30,8 @@ void DefaultScene::Start() { m_testdraw.Start(std::vector({glm::vec2(0.F, 0.F)}), DrawOverlays::OverlayShapes::R_CROSS); // m_GameObjectManager.Start(); + + } void DefaultScene::Update() { @@ -77,4 +56,29 @@ void DefaultScene::Update() { m_testdraw.DrawUsingCamera(trans2, 1); // m_GameObjectManager.Update(); + + if(UIClass::getUnitConstructCount(unitType::BARRACKS)>0){ + m_BuiltStructure.push_back(std::make_unique()); + m_BuiltStructure.back()->Start(); + } + if(UIClass::getUnitConstructCount(unitType::POWER_PLANT) > 0) { + m_BuiltStructure.push_back(std::make_unique()); + m_BuiltStructure.back()->Start(); + } + + if(UIClass::getUnitConstructCount(unitType::ORE_REF) > 0) { + m_BuiltStructure.push_back(std::make_unique()); + m_BuiltStructure.back()->Start(); + } + + if(UIClass::getUnitConstructCount(unitType::WAR_FACT) > 0) { + m_BuiltStructure.push_back(std::make_unique()); + m_BuiltStructure.back()->Start(); + } + + if(UIClass::getUnitConstructCount(unitType::ADV_POWER_PLANT) > 0) { + m_BuiltStructure.push_back(std::make_unique()); + m_BuiltStructure.back()->Start(); + } + } diff --git a/src/UI/UI.cpp b/src/UI/UI.cpp index bbfeebdc..7be81450 100644 --- a/src/UI/UI.cpp +++ b/src/UI/UI.cpp @@ -82,25 +82,34 @@ void UIClass::ShowPlayerConstructionMenu() { if (getImageButtonBySpriteSheetIndex(m_StructureIconSpriteSheet, 7)) { // power plants + if(ButtonScript.GetIfFinished(unitType::POWER_PLANT)){ + setUnitConstructCount(unitType::POWER_PLANT, 1); + }else{ + ButtonScript.buttonEvent(powerPlant); + } - setUnitConstructCount(unitType::POWER_PLANT, 1); - ButtonScript.buttonEvent(powerPlant); LOG_DEBUG("TEST"); } ImGui::SameLine(); if (getImageButtonBySpriteSheetIndex(m_StructureIconSpriteSheet, 22)) { // barracks - setUnitConstructCount(unitType::BARRACKS, 1); - ButtonScript.buttonEvent(barracks); + if(ButtonScript.GetIfFinished(unitType::BARRACKS)) { + setUnitConstructCount(unitType::BARRACKS, 1); + }else { + ButtonScript.buttonEvent(barracks); + } LOG_DEBUG("TEST"); } ImGui::SameLine(); if (getImageButtonBySpriteSheetIndex(m_StructureIconSpriteSheet, 8)) { // ore - setUnitConstructCount(unitType::ORE_REF, 1); - ButtonScript.buttonEvent(oreRefinery); + if(ButtonScript.GetIfFinished(unitType::ORE_REF)) { + setUnitConstructCount(unitType::ORE_REF, 1); + }else { + ButtonScript.buttonEvent(oreRefinery); + } LOG_DEBUG("TEST"); } @@ -108,16 +117,22 @@ void UIClass::ShowPlayerConstructionMenu() { if (getImageButtonBySpriteSheetIndex(m_StructureIconSpriteSheet, 20)) { // war factory - setUnitConstructCount(unitType::WAR_FACT, 1); - ButtonScript.buttonEvent(warFactory); + if(ButtonScript.GetIfFinished(unitType::WAR_FACT)) { + setUnitConstructCount(unitType::WAR_FACT, 1); + }else { + ButtonScript.buttonEvent(warFactory); + } LOG_DEBUG("TEST"); } ImGui::SameLine(); if (getImageButtonBySpriteSheetIndex(m_StructureIconSpriteSheet, 1)) { // advance power - setUnitConstructCount(unitType::ADV_POWER_PLANT, 1); - ButtonScript.buttonEvent(advPowerPlant); + if(ButtonScript.GetIfFinished(unitType::ADV_POWER_PLANT)) { + setUnitConstructCount(unitType::ADV_POWER_PLANT, 1); + }else { + ButtonScript.buttonEvent(advPowerPlant); + } LOG_DEBUG("TEST"); } if (ImGui::Button("Radar Dome")) { diff --git a/src/UI/UIScriptProcess.cpp b/src/UI/UIScriptProcess.cpp index 5f73daa7..13fc0396 100644 --- a/src/UI/UIScriptProcess.cpp +++ b/src/UI/UIScriptProcess.cpp @@ -16,6 +16,20 @@ bool UIScriptProcess::GetIfFinished(std::shared_ptr structure) { return b_ADVPowerPlant; } } + +bool UIScriptProcess::GetIfFinished(unitType type) { + if (type==unitType::BARRACKS) { + return b_Baracks; + } else if (type==unitType::ORE_REF) { + return b_OreRefinery; + } else if (type==unitType::POWER_PLANT) { + return b_PowerPlants; + } else if (type==unitType::WAR_FACT) { + return b_WarFactory; + } else if (type==unitType::ADV_POWER_PLANT) { + return b_ADVPowerPlant; + } +} void UIScriptProcess::SetFinished(std::shared_ptr structure) { if (std::dynamic_pointer_cast(structure)) { b_Baracks = true; @@ -30,7 +44,9 @@ void UIScriptProcess::SetFinished(std::shared_ptr structure) { } } void UIScriptProcess::CountDown() { - if (true /*Counter>=TargetTime*/) { + auto current = std::chrono::high_resolution_clock::now(); + std::chrono::duration elapsed = current - m_StartTime; + if (elapsed.count()>=TargetTime) { SetFinished(temp_PTR); b_STALL = false; return; @@ -39,12 +55,12 @@ void UIScriptProcess::CountDown() { } void UIScriptProcess::SetCoolDown(float time) { Counter = 0.F; - TargetTime = time * 60; + TargetTime = time; + m_StartTime = std::chrono::high_resolution_clock::now(); } void UIScriptProcess::buttonEvent(std::shared_ptr m_Structure) { if (GetIfFinished(m_Structure)) { - m_Structure->Start(); return; } buildQueue.push(m_Structure); @@ -60,3 +76,4 @@ void UIScriptProcess::Update() { } CountDown(); } + From 0bbe3e9057211ced96f5b5717414ea862243b7de Mon Sep 17 00:00:00 2001 From: git Date: Fri, 15 Mar 2024 09:21:20 +0800 Subject: [PATCH 2/3] button update --- CMakeLists.txt | 8 +-- include/GameObjectID.hpp | 1 + include/Structure/Structure.hpp | 2 +- include/UI/UI.hpp | 6 +-- ...{UIScriptProcess.h => UIScriptProcess.hpp} | 8 +-- include/Unit/PathfindingUnit.hpp | 51 +++++++++++++++++++ src/Structure/Structure.cpp | 1 + src/UI/UIScriptProcess.cpp | 2 +- 8 files changed, 67 insertions(+), 12 deletions(-) rename include/UI/{UIScriptProcess.h => UIScriptProcess.hpp} (84%) create mode 100644 include/Unit/PathfindingUnit.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 59f2e1db..552a9cb2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -76,13 +76,14 @@ set(SRC_FILES ${SRC_DIR}/Structure/HighLight.cpp ${SRC_DIR}/UI/UIScriptProcess.cpp - # ${SRC_DIR}/Structure/Buildings.cpp ${SRC_DIR}/Structure/Barracks.cpp ${SRC_DIR}/Structure/OreRefinery.cpp ${SRC_DIR}/Structure/PowerPlants.cpp ${SRC_DIR}/Structure/WarFactory.cpp #${SRC_DIR}/Structure/ADVPowerPlants.cpp + #${SRC_DIR}/Unit/PathfindingUnit.cpp + ${SRC_DIR}/Scene/DefaultScene.cpp ${SRC_DIR}/Camera.cpp ${SRC_DIR}/SpriteSheet.cpp @@ -147,17 +148,18 @@ set(INCLUDE_FILES ${INCLUDE_DIR}/Structure/MousOverlapTool.h ${INCLUDE_DIR}/Structure/IWayPointStructure.hpp ${INCLUDE_DIR}/HighLight.h - ${INCLUDE_DIR}/UI/UIScriptProcess.h + ${INCLUDE_DIR}/UI/UIScriptProcess.hpp ${INCLUDE_DIR}/DrawOverlays.hpp ${INCLUDE_DIR}/UI/UI.hpp - #${INCLUDE_DIR}/Structure/Buildings.hpp ${INCLUDE_DIR}/Structure/Barracks.hpp ${INCLUDE_DIR}/Structure/OreRefinery.hpp ${INCLUDE_DIR}/Structure/PowerPlants.hpp ${INCLUDE_DIR}/Structure/WarFactory.hpp ${INCLUDE_DIR}/Structure/ADVPowerPlants.hpp + ${INCLUDE_DIR}/Unit/PathfindingUnit.hpp + ${INCLUDE_DIR}/imgui/imconfig.h ${INCLUDE_DIR}/imgui/imgui.h ${INCLUDE_DIR}/imgui/imgui_impl_sdl2.h diff --git a/include/GameObjectID.hpp b/include/GameObjectID.hpp index d3093954..5c2cca22 100644 --- a/include/GameObjectID.hpp +++ b/include/GameObjectID.hpp @@ -5,6 +5,7 @@ #ifndef PRACTICALTOOLSFORSIMPLEDESIGN_GAMEOBJECTID_HPP #define PRACTICALTOOLSFORSIMPLEDESIGN_GAMEOBJECTID_HPP #include +#include enum class unitType { // buildings POWER_PLANT, diff --git a/include/Structure/Structure.hpp b/include/Structure/Structure.hpp index 9704fccb..f21abc64 100644 --- a/include/Structure/Structure.hpp +++ b/include/Structure/Structure.hpp @@ -60,7 +60,7 @@ class Structure : public Util::GameObject { virtual void onSelected(bool selected); virtual void attachmentUpdate(); // this function now will update // attachment's location and draw as well - bool getStarted(){ if(m_CurrentState==updateMode::Fixed){return true;}else{return false;}} + bool getBuilt(){ if(m_CurrentState==updateMode::Fixed){return true;}else{return false;}} /* void SetElectricPower(float electricPower); void SetBuildingTime(float buildingTime); diff --git a/include/UI/UI.hpp b/include/UI/UI.hpp index 12fbfec3..295cb0db 100644 --- a/include/UI/UI.hpp +++ b/include/UI/UI.hpp @@ -7,7 +7,7 @@ #include "Camera.hpp" #include "GameObjectID.hpp" #include "SpriteSheet.hpp" -#include "UIScriptProcess.h" +#include "UI/UIScriptProcess.hpp" #include "imgui/imgui.h" #include "imgui/imgui_impl_opengl3.h" #include "imgui/imgui_impl_sdl2.h" @@ -30,6 +30,7 @@ class UIClass { } private: + UIScriptProcess ButtonScript; void InitUnitQueue(); void ShowCursorSelectionRegion(ImVec2 *start_pos, ImVec2 *end_pos, ImGuiMouseButton mouse_button); @@ -42,7 +43,6 @@ class UIClass { getImageButtonBySpriteSheetIndex(std::shared_ptr spritesheet, int index); -private: std::shared_ptr m_StructureIconSpriteSheet = std::make_shared(); std::shared_ptr m_InfantryIconSpriteSheet = @@ -53,7 +53,7 @@ class UIClass { static std::unordered_map s_unitConstructCount; Grid m_Grid; glm::vec2 m_GridSize = {100, 100}; - UIScriptProcess ButtonScript; + std::shared_ptr barracks = std::make_shared(); std::shared_ptr oreRefinery = std::make_shared(); diff --git a/include/UI/UIScriptProcess.h b/include/UI/UIScriptProcess.hpp similarity index 84% rename from include/UI/UIScriptProcess.h rename to include/UI/UIScriptProcess.hpp index c1ccefc9..939e2fe5 100644 --- a/include/UI/UIScriptProcess.h +++ b/include/UI/UIScriptProcess.hpp @@ -2,15 +2,14 @@ // Created by nudle on 2024/3/8. // -#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_UISCRIPTPROCESS_H -#define PRACTICALTOOLSFORSIMPLEDESIGN_UISCRIPTPROCESS_H +#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_UISCRIPTPROCESS_HPP +#define PRACTICALTOOLSFORSIMPLEDESIGN_UISCRIPTPROCESS_HPP #include "Structure/Structure.hpp" #include "Structure/Barracks.hpp" #include "Structure/OreRefinery.hpp" #include "Structure/PowerPlants.hpp" #include "Structure/WarFactory.hpp" #include "Structure/AdvencePowerPlants.hpp" -#include "UI.hpp" #include "GameObjectID.hpp" #include #include @@ -28,6 +27,7 @@ class UIScriptProcess{ std::chrono::time_point m_StartTime; public: UIScriptProcess(){}; + ~UIScriptProcess(){}; // bool GetIfFinished(std::shared_ptr structure); bool GetIfFinished(unitType type); @@ -41,4 +41,4 @@ class UIScriptProcess{ void SetCoolDown(float time); void CountDown(); }; -#endif // PRACTICALTOOLSFORSIMPLEDESIGN_UISCRIPTPROCESS_H +#endif // PRACTICALTOOLSFORSIMPLEDESIGN_UISCRIPTPROCESS_HPP diff --git a/include/Unit/PathfindingUnit.hpp b/include/Unit/PathfindingUnit.hpp new file mode 100644 index 00000000..8c9f0380 --- /dev/null +++ b/include/Unit/PathfindingUnit.hpp @@ -0,0 +1,51 @@ +// +// Created by nudle on 2024/3/15. +// + +#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_PATHFINDINGUNIT_HPP +#define PRACTICALTOOLSFORSIMPLEDESIGN_PATHFINDINGUNIT_HPP + +#include "Tile.hpp" +#include "Util/GameObject.hpp" +#include "Map.hpp" +class PathfindingUnit:public Util::GameObject{ +private: + enum class UnitMode{ + Attacking,Moving,Stop + }; + enum class MoveDirection{ + Up,TopRight,TopLeft,Right,Left,DownRight,DownLeft,Down,Wait + }; + + glm::vec2 m_targetCell; + glm::vec2 m_nextCell; + glm::vec2 m_currentCell; + glm::vec2 m_currentLocation; + + MoveDirection m_currentDir=MoveDirection::Wait; + + float m_Hp=100.F; + float m_MoveSpeed=1.F; + +public: + PathfindingUnit(){}; + ~PathfindingUnit(){}; + + void setTargetCell(glm::vec2 target){this->m_targetCell=target;} + glm::vec2 getTargetCell(){return m_targetCell;} + + void findNextCell() { + if (m_targetCell.x - m_currentCell.x >m_targetCell.y - m_currentCell.y &&m_targetCell.x - m_currentCell.x > 0) { + m_currentDir = MoveDirection::Right; + } else if (m_targetCell.x - m_currentCell.x >m_targetCell.y - m_currentCell.y &&m_targetCell.x - m_currentCell.x < 0) { + m_currentDir = MoveDirection::Up; + } else if (m_targetCell.x - m_currentCell.x 0) { + m_currentDir = MoveDirection::Down; + }else if(m_targetCell.x-m_currentCell.xSetObjectLocation(location); this->SetCurrentUpdateMode(updateMode::Fixed); + //在這裡增加設置Tile屬性 } } void Structure::updateInvinsible() { diff --git a/src/UI/UIScriptProcess.cpp b/src/UI/UIScriptProcess.cpp index 13fc0396..9a617ef8 100644 --- a/src/UI/UIScriptProcess.cpp +++ b/src/UI/UIScriptProcess.cpp @@ -1,7 +1,7 @@ // // Created by nudle on 2024/3/8. // -#include "UI/UIScriptProcess.h" +#include "UI/UIScriptProcess.hpp" bool UIScriptProcess::GetIfFinished(std::shared_ptr structure) { if (std::dynamic_pointer_cast(structure)) { From bfefaa6358058fa11e4e2f0f942b88bacf23fe8f Mon Sep 17 00:00:00 2001 From: git Date: Fri, 15 Mar 2024 09:26:26 +0800 Subject: [PATCH 3/3] button update --- src/Scene/DefaultScene.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Scene/DefaultScene.cpp b/src/Scene/DefaultScene.cpp index 636ea8fe..8c6b695c 100644 --- a/src/Scene/DefaultScene.cpp +++ b/src/Scene/DefaultScene.cpp @@ -58,27 +58,35 @@ void DefaultScene::Update() { // m_GameObjectManager.Update(); if(UIClass::getUnitConstructCount(unitType::BARRACKS)>0){ + UIClass::setUnitConstructCount(unitType::BARRACKS,-1); m_BuiltStructure.push_back(std::make_unique()); m_BuiltStructure.back()->Start(); } if(UIClass::getUnitConstructCount(unitType::POWER_PLANT) > 0) { + UIClass::setUnitConstructCount(unitType::POWER_PLANT,-1); m_BuiltStructure.push_back(std::make_unique()); m_BuiltStructure.back()->Start(); } if(UIClass::getUnitConstructCount(unitType::ORE_REF) > 0) { + UIClass::setUnitConstructCount(unitType::ORE_REF,-1); m_BuiltStructure.push_back(std::make_unique()); m_BuiltStructure.back()->Start(); } if(UIClass::getUnitConstructCount(unitType::WAR_FACT) > 0) { + UIClass::setUnitConstructCount(unitType::WAR_FACT,-1); m_BuiltStructure.push_back(std::make_unique()); m_BuiltStructure.back()->Start(); } if(UIClass::getUnitConstructCount(unitType::ADV_POWER_PLANT) > 0) { + UIClass::setUnitConstructCount(unitType::ADV_POWER_PLANT,-1); m_BuiltStructure.push_back(std::make_unique()); m_BuiltStructure.back()->Start(); } + for(auto i :m_BuiltStructure){ + i->Update(); + } }