Skip to content

Commit

Permalink
button update
Browse files Browse the repository at this point in the history
  • Loading branch information
ntut-Tu committed Mar 24, 2024
1 parent ae90c3b commit 229bfd4
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 6 deletions.
13 changes: 12 additions & 1 deletion include/GameObjectManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "Structure/PowerPlants.hpp"
#include "Structure/Structure.hpp"
#include "Structure/WarFactory.hpp"
#include "Unit/Avatar.hpp"
#include <unordered_map>
#include <utility>
class GameObjectManager {
Expand All @@ -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<Structure> newstruct) {
newstruct->Start();
m_BuiltStructure.push_back(newstruct);
}
void unitAppend(std::shared_ptr<Avatar> 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++) {
Expand All @@ -55,9 +64,11 @@ class GameObjectManager {
return totalPower;
}

std::vector<std::shared_ptr<Structure>> getStructureArray(){return m_BuiltStructure;}

private:
std::vector<std::shared_ptr<Structure>> m_BuiltStructure;
// std::vector<std::shared_ptr<Unit>> m_UnitArray;
std::vector<std::shared_ptr<Avatar>> m_UnitArray;
};

#endif // PRACTICALTOOLSFORSIMPLEDESIGN_GAMEOBJECTMANAGER_HPP
2 changes: 1 addition & 1 deletion include/Structure/Barracks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Barracks : public Structure, public IWayPointStructure {
std::vector<Line> 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){};
Expand Down
20 changes: 20 additions & 0 deletions include/UI/UI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ class UIClass {
bool
getIfAnythingCanSelectToBuild(); // 避免Scene收到空的getSelectedBuilding

std::shared_ptr<Avatar> getUnitFromUI();
bool getIfUnitReadyToSpawn(){return ButtonScript.getIfReadytoSpawn();}
void setIfUnitReadyToSpawn(bool b){ButtonScript.setIfReadytoSpawn(b);}
void importMap(std::shared_ptr<MapClass> m_Map){this->m_Map=m_Map;}

//check if building has built
void checkExistBuilding(std::vector<std::shared_ptr<Structure>> buildingList);
private:
UIScriptProcess ButtonScript;
void InitUnitQueue();
Expand Down Expand Up @@ -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<MapClass> m_Map;
};

#endif // PRACTICALTOOLSFORSIMPLEDESIGN_UI_HPP
22 changes: 22 additions & 0 deletions include/UI/UIScriptProcess.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,39 @@
#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 <chrono>
#include <future>
#include <queue>

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;
std::deque<unitType> buildQueue;
std::chrono::time_point<std::chrono::high_resolution_clock> m_StartTime;
std::chrono::time_point<std::chrono::high_resolution_clock>
m_CountDownCurrentTime;
// building end

// avatar
bool b_readytoSpawn = false;
float m_SpawnTime = 0.F;
unitType m_currentAvatar;
bool b_spawnInCD= false;
std::deque<unitType> m_spawnQueue;
std::chrono::time_point<std::chrono::high_resolution_clock> m_SpawnStartTime;
// avatar end

public:
UIScriptProcess(){};
Expand All @@ -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<Avatar> spawnAvatar();
bool getIfReadytoSpawn(){return b_readytoSpawn;}
void setIfReadytoSpawn(bool b){b_readytoSpawn=b;}

private:
std::shared_ptr<Structure> barracks = std::make_shared<Barracks>();
std::shared_ptr<Structure> oreRefinery = std::make_shared<OreRefinery>();
Expand Down
2 changes: 1 addition & 1 deletion include/Unit/Infantry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class Infantry:public Avatar{
public:
Infantry():Avatar(){
setHp(50);
//setHp(50);
setMovementSpeed(4);
}
private:
Expand Down
9 changes: 7 additions & 2 deletions src/Scene/DefaultScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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());
}
}
51 changes: 50 additions & 1 deletion src/UI/UI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -252,6 +256,7 @@ void UIClass::ShowDefTab() {
if (ImGui::BeginTabItem("Def")) {
if (getImageButtonBySpriteSheetIndex(m_StructureIconSpriteSheet, 18)) {
// sandbags

setUnitConstructCount(unitType::SANDBAGS, 1);
LOG_DEBUG("TEST");
}
Expand Down Expand Up @@ -432,3 +437,47 @@ bool UIClass::getIfAnythingCanSelectToBuild() {
ButtonScript.GetIfFinished(unitType::WAR_FACT) ||
ButtonScript.GetIfFinished(unitType::ADV_POWER_PLANT));
}

void UIClass::checkExistBuilding(std::vector<std::shared_ptr<Structure>> buildingList){
b_barackBuilt= false;
b_warfactoryBuilt= false;
b_orerefineryBuilt= false;
if(buildingList.size()==0){
return;
}
for(auto i:buildingList){
if(std::dynamic_pointer_cast<Barracks>(i)&&!b_barackBuilt){
m_barrackTargetCell=MapClass::GlobalCoordToCellCoord(std::dynamic_pointer_cast<Barracks>(i)->GetWayPointLocation());
b_barackBuilt= true;
m_barrackCell=MapClass::GlobalCoordToCellCoord(i->GetObjectLocation());
}
else if(std::dynamic_pointer_cast<WarFactory>(i)&&!b_warfactoryBuilt){
m_warfactoryTargetCell=MapClass::GlobalCoordToCellCoord(std::dynamic_pointer_cast<Barracks>(i)->GetWayPointLocation());
b_warfactoryBuilt= true;
m_warfactoryCell=MapClass::GlobalCoordToCellCoord(i->GetObjectLocation());
}
else if(std::dynamic_pointer_cast<OreRefinery>(i)&&!b_orerefineryBuilt){
m_orerefineryTargetCell=MapClass::GlobalCoordToCellCoord(std::dynamic_pointer_cast<Barracks>(i)->GetWayPointLocation());
b_orerefineryBuilt= true;
m_orerefineryCell=MapClass::GlobalCoordToCellCoord(i->GetObjectLocation());
}
}
}

std::shared_ptr<Avatar> UIClass::getUnitFromUI(){
printf("(UI)return to GOM\n");
auto Avatar = ButtonScript.spawnAvatar();
ButtonScript.setIfReadytoSpawn(false);
if(std::dynamic_pointer_cast<Infantry>(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;
}
45 changes: 45 additions & 0 deletions src/UI/UIScriptProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,37 @@ void UIScriptProcess::CountDown() {
m_CountDownCurrentTime = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed =
m_CountDownCurrentTime - m_StartTime;
std::chrono::duration<double> 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)) {
Expand All @@ -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();
}

Expand Down Expand Up @@ -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<Avatar> UIScriptProcess::spawnAvatar(){
printf("(UISC)spawnAvatar\n");
switch (m_currentAvatar) {
case unitType::INFANTRY:{
return std::make_unique<Infantry>();
}
}
}

0 comments on commit 229bfd4

Please sign in to comment.