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 25, 2024
1 parent 229bfd4 commit 7af60dd
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 56 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ set(INCLUDE_FILES
${INCLUDE_DIR}/UI/UIScriptProcess.hpp
${INCLUDE_DIR}/DrawOverlays.hpp
${INCLUDE_DIR}/UI/UI.hpp
${INCLUDE_DIR}/Player.hpp

${INCLUDE_DIR}/Structure/Barracks.hpp
${INCLUDE_DIR}/Structure/OreRefinery.hpp
Expand Down
11 changes: 9 additions & 2 deletions include/Player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@ class Player {
Player() {}
~Player() {}

void setCurrency(int value) { m_totalCurrency += value; };
void setTotalCurrency(int value) { m_totalCurrency = value; };
void addCurrency(int value) { m_totalCurrency += value; };

int getTotalPower() {}
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;}

private:
int m_maxTroopSize = 200;
int m_totalPower = 0 ;
int m_totalCurrency = 0;
};
#endif // PRACTICALTOOLSFORSIMPLEDESIGN_PLAYER_HPP
1 change: 1 addition & 0 deletions include/Scene/DefaultScene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class DefaultScene {
Util::Renderer m_Renderer;
UIClass m_UI;
std::shared_ptr<MapClass> m_Map = std::make_shared<MapClass>();
std::shared_ptr<Player> m_Player=std::make_shared<Player>();
GameObjectManager m_Manager;

Avatar m_dummy;
Expand Down
8 changes: 7 additions & 1 deletion include/UI/UI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define PRACTICALTOOLSFORSIMPLEDESIGN_UI_HPP
#include "Camera.hpp"
#include "GameObjectID.hpp"
#include "Player.hpp"
#include "SpriteSheet.hpp"
#include "UI/UIScriptProcess.hpp"
#include "imgui/imgui.h"
Expand All @@ -31,12 +32,15 @@ class UIClass {

std::unique_ptr<Structure> getSelectedBuilding();
bool
getIfAnythingCanSelectToBuild(); // 避免Scene收到空的getSelectedBuilding
getIfAnyBuildingReadyToBuild(); // 避免Scene收到空的getSelectedBuilding

std::shared_ptr<Avatar> getUnitFromUI();
bool getIfUnitReadyToSpawn(){return ButtonScript.getIfReadytoSpawn();}
void setIfUnitReadyToSpawn(bool b){ButtonScript.setIfReadytoSpawn(b);}

//import from scene
void importMap(std::shared_ptr<MapClass> m_Map){this->m_Map=m_Map;}
void importPlayer(std::shared_ptr<Player> m_Player){this->m_Player=m_Player;}

//check if building has built
void checkExistBuilding(std::vector<std::shared_ptr<Structure>> buildingList);
Expand Down Expand Up @@ -105,7 +109,9 @@ class UIClass {
glm::vec2 m_orerefineryCell;
glm::vec2 m_orerefineryTargetCell;

//ptr import from scene
std::shared_ptr<MapClass> m_Map;
std::shared_ptr<Player> m_Player;
};

#endif // PRACTICALTOOLSFORSIMPLEDESIGN_UI_HPP
30 changes: 15 additions & 15 deletions include/UI/UIScriptProcess.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@ class UIScriptProcess {
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;
float m_buildCoolDownTime = 0.F;
unitType m_currentStructureType;
bool b_isBuildingInCoolDown = false;
std::deque<unitType> m_buildQueue;
std::chrono::time_point<std::chrono::high_resolution_clock> m_buildStartTime;
std::chrono::time_point<std::chrono::high_resolution_clock>
m_CountDownCurrentTime;
m_currentCountDownTime;
// building end

// avatar
bool b_readytoSpawn = false;
float m_SpawnTime = 0.F;
unitType m_currentAvatar;
bool b_spawnInCD= false;
bool b_isReadyToSpawn = false;
float m_spawnCooldownTime = 0.F;
unitType m_currentAvatarType;
bool b_isSpawningInCooldown= false;
std::deque<unitType> m_spawnQueue;
std::chrono::time_point<std::chrono::high_resolution_clock> m_SpawnStartTime;
// avatar end
Expand All @@ -50,9 +50,9 @@ class UIScriptProcess {
~UIScriptProcess(){};
//
bool GetIfFinished(unitType type);
// if b_Stall==false not currently building
// if b_isBuildingInCoolDown==false not currently building
// true currently is building
void SetSTALL(bool value) { b_STALL = value; };
void SetSTALL(bool value) { b_isBuildingInCoolDown = value; };
/*
* false for used, true for finished
*/
Expand All @@ -72,12 +72,12 @@ class UIScriptProcess {
float GetStructureTime(unitType type);
float GetSpawnTime(unitType type);

unitType GetCurrentStructure() { return m_currentStructure; };
unitType GetCurrentStructure() { return m_currentStructureType; };

// spawn unit
std::shared_ptr<Avatar> spawnAvatar();
bool getIfReadytoSpawn(){return b_readytoSpawn;}
void setIfReadytoSpawn(bool b){b_readytoSpawn=b;}
bool getIfReadytoSpawn(){return b_isReadyToSpawn;}
void setIfReadytoSpawn(bool b){b_isReadyToSpawn=b;}

private:
std::shared_ptr<Structure> barracks = std::make_shared<Barracks>();
Expand Down
7 changes: 5 additions & 2 deletions src/Scene/DefaultScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ void DefaultScene::Start() {
// m_GameObjectManager.Start();

//m_dummy.Start({5, 5}, m_Map);
m_UI.importMap(m_Map);
m_UI.importPlayer(m_Player);

m_Player->setTotalCurrency(50000);
}

void DefaultScene::Update() {
Expand Down Expand Up @@ -55,11 +59,10 @@ void DefaultScene::Update() {
m_testdraw.DrawUsingCamera(trans2, 1);
// m_GameObjectManager.Update();

if (m_UI.getIfAnythingCanSelectToBuild()) {
if (m_UI.getIfAnyBuildingReadyToBuild()) {
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());
}
Expand Down
4 changes: 2 additions & 2 deletions src/UI/UI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void UIClass::Update() {
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());

ButtonScript.Update(getIfAnythingCanSelectToBuild());
ButtonScript.Update(getIfAnyBuildingReadyToBuild());
// printf("(UI)Button Lock :
// %s,%s\n",selectLock()?"Unlock":"Lock",b_SelectToBuild?"True":"False");
}
Expand Down Expand Up @@ -429,7 +429,7 @@ bool UIClass::selectLock() {
return !(b_Baracks | b_OreRefinery | b_PowerPlants | b_WarFactory |
b_ADVPowerPlant);
}
bool UIClass::getIfAnythingCanSelectToBuild() {
bool UIClass::getIfAnyBuildingReadyToBuild() {
return b_SelectToBuild &&
(ButtonScript.GetIfFinished(unitType::BARRACKS) ||
ButtonScript.GetIfFinished(unitType::POWER_PLANT) ||
Expand Down
68 changes: 34 additions & 34 deletions src/UI/UIScriptProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ bool UIScriptProcess::GetIfFinished(unitType type) {
}

float UIScriptProcess::GetCDLeft() {
if (b_STALL) {
if (b_isBuildingInCoolDown) {
std::chrono::duration<double> elapsed =
m_CountDownCurrentTime - m_StartTime;
return TargetTime - elapsed.count();
m_currentCountDownTime - m_buildStartTime;
return m_buildCoolDownTime - elapsed.count();
} else {
return -1.F;
}
Expand All @@ -49,63 +49,63 @@ std::string UIScriptProcess::GetFormattedCD() {
}

void UIScriptProcess::CountDown() {
m_CountDownCurrentTime = std::chrono::high_resolution_clock::now();
m_currentCountDownTime = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed =
m_CountDownCurrentTime - m_StartTime;
m_currentCountDownTime - m_buildStartTime;
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");
m_currentCountDownTime - m_SpawnStartTime;
if (b_isBuildingInCoolDown) {
// printf("(Button) CD: %.2f,%s\n", m_buildCoolDownTime - elapsed.count(),
// elapsed.count() >= m_buildCoolDownTime ? "True" : "False");
}
if (b_spawnInCD) {
if (b_isSpawningInCooldown) {
printf("(UISC) CD: %.2f,%s\n", unitElapsed.count(),
elapsed.count() >= m_SpawnTime ? "True" : "False");
elapsed.count() >= m_spawnCooldownTime ? "True" : "False");
}
if (elapsed.count() >= TargetTime && b_STALL) {
if (elapsed.count() >= m_buildCoolDownTime && b_isBuildingInCoolDown) {
// printf("(Button) Construction Finished\n");
SetIfFinished(m_currentStructure, true);
b_STALL = false;
SetIfFinished(m_currentStructureType, true);
b_isBuildingInCoolDown = false;
return;
}
if (unitElapsed.count() >= m_SpawnTime && b_spawnInCD) {
b_readytoSpawn=true;
b_spawnInCD = false;
if (unitElapsed.count() >= m_spawnCooldownTime && b_isSpawningInCooldown) {
b_isReadyToSpawn=true;
b_isSpawningInCooldown = false;
printf("(UISC)Unit Ready\n");
return;
}
}
void UIScriptProcess::SetCountDown(float time) {
TargetTime = time;
m_StartTime = std::chrono::high_resolution_clock::now();
m_buildCoolDownTime = time;
m_buildStartTime = std::chrono::high_resolution_clock::now();
}
void UIScriptProcess::SetSpawnCountDown(float time) {
m_SpawnTime = time;
m_spawnCooldownTime = time;
m_SpawnStartTime = std::chrono::high_resolution_clock::now();
}

void UIScriptProcess::AddToBuildQueue(unitType type) {
if (GetIfFinished(type)) {
return;
}
buildQueue.push_back(type);
m_buildQueue.push_back(type);
return;
}
void UIScriptProcess::Update(bool queueContinue) {
//(buildQueue.size() > 1 && queueContinue && !b_STALL) for waiting player to
//(m_buildQueue.size() > 1 && queueContinue && !b_isBuildingInCoolDown) for waiting player to
//build strucutre, then continue operating
if ((buildQueue.size() == 1 && !b_STALL) ||
((buildQueue.size() > 1 && queueContinue && !b_STALL))) {
m_currentStructure = buildQueue.front();
buildQueue.pop_front();
b_STALL = true;
SetCountDown(GetStructureTime(m_currentStructure));
}
if (m_spawnQueue.size() !=0 && !b_spawnInCD) {
m_currentAvatar = m_spawnQueue.front();
if ((m_buildQueue.size() == 1 && !b_isBuildingInCoolDown) ||
((m_buildQueue.size() > 1 && queueContinue && !b_isBuildingInCoolDown))) {
m_currentStructureType = m_buildQueue.front();
m_buildQueue.pop_front();
b_isBuildingInCoolDown = true;
SetCountDown(GetStructureTime(m_currentStructureType));
}
if (m_spawnQueue.size() !=0 && !b_isSpawningInCooldown) {
m_currentAvatarType = m_spawnQueue.front();
m_spawnQueue.pop_front();
b_spawnInCD = true;
SetSpawnCountDown(GetSpawnTime(m_currentAvatar));
b_isSpawningInCooldown = true;
SetSpawnCountDown(GetSpawnTime(m_currentAvatarType));
}
CountDown();
}
Expand Down Expand Up @@ -168,7 +168,7 @@ float UIScriptProcess::GetSpawnTime(unitType type){

std::shared_ptr<Avatar> UIScriptProcess::spawnAvatar(){
printf("(UISC)spawnAvatar\n");
switch (m_currentAvatar) {
switch (m_currentAvatarType) {
case unitType::INFANTRY:{
return std::make_unique<Infantry>();
}
Expand Down

0 comments on commit 7af60dd

Please sign in to comment.