Skip to content

Commit

Permalink
Merge pull request #38 from ntut-Tu/buttonUpdate
Browse files Browse the repository at this point in the history
button update
  • Loading branch information
jonylu7 authored Mar 26, 2024
2 parents a858d4f + 153b40d commit 0205b79
Show file tree
Hide file tree
Showing 11 changed files with 215 additions and 42 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,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
15 changes: 14 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) {}

Expand All @@ -47,10 +56,14 @@ 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<Avatar>> m_UnitArray;

std::vector<std::vector<std::shared_ptr<TileClass>>> m_Map;
// std::vector<std::shared_ptr<Unit>> m_UnitArray;

};

#endif // PRACTICALTOOLSFORSIMPLEDESIGN_GAMEOBJECTMANAGER_HPP
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
3 changes: 2 additions & 1 deletion include/Structure/Barracks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ 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,
float buildingCost = 300.F, float buildingHp = 800.F,
HouseType house = HouseType::NONE)

: Structure(electricPower, buildingTime, buildingCost, buildingHp,
GameObjectID(unitType::BARRACKS, house)){};
void Start() override;
Expand Down
28 changes: 27 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,8 +32,18 @@ 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);
private:
UIScriptProcess ButtonScript;
void InitUnitQueue();
Expand Down Expand Up @@ -86,6 +97,21 @@ 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;

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

#endif // PRACTICALTOOLSFORSIMPLEDESIGN_UI_HPP
40 changes: 31 additions & 9 deletions include/UI/UIScriptProcess.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,48 @@
#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;

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_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

public:
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 @@ -47,15 +61,23 @@ 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_currentStructureType; };

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

private:
std::shared_ptr<Structure> barracks = std::make_shared<Barracks>();
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
14 changes: 11 additions & 3 deletions src/Scene/DefaultScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ void DefaultScene::Start() {
DrawOverlays::OverlayShapes::R_CROSS);
// m_GameObjectManager.Start();

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

m_Player->setTotalCurrency(50000);
}

void DefaultScene::Update() {
m_dummy.Update();
//m_dummy.Update();

m_Manager.Update();

Expand All @@ -54,7 +58,11 @@ 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());
if(m_UI.getIfUnitReadyToSpawn()){
m_Manager.unitAppend(m_UI.getUnitFromUI());
}
}
55 changes: 52 additions & 3 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 @@ -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 @@ -424,11 +429,55 @@ 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) ||
ButtonScript.GetIfFinished(unitType::ORE_REF) ||
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;
}
Loading

0 comments on commit 0205b79

Please sign in to comment.