Skip to content

Commit

Permalink
Merge pull request #39 from jonylu7/buttonUpdate
Browse files Browse the repository at this point in the history
button update
  • Loading branch information
jonylu7 authored Mar 28, 2024
2 parents ccd8e3b + be59df7 commit d8b110e
Show file tree
Hide file tree
Showing 13 changed files with 193 additions and 48 deletions.
29 changes: 28 additions & 1 deletion include/GameObjectManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_GAMEOBJECTMANAGER_HPP
#define PRACTICALTOOLSFORSIMPLEDESIGN_GAMEOBJECTMANAGER_HPP
#include "GameObjectID.hpp"
#include "Player.hpp"
#include "Structure/AdvencePowerPlants.hpp"
#include "Structure/Barracks.hpp"
#include "Structure/OreRefinery.hpp"
Expand All @@ -14,6 +15,7 @@
#include "Unit/Avatar.hpp"
#include <unordered_map>
#include <utility>
#include <chrono>
class GameObjectManager {
public:
GameObjectManager() {}
Expand All @@ -26,6 +28,7 @@ class GameObjectManager {
for (auto pair : m_BuiltStructure) {
pair->Start();
}
m_StartTime = std::chrono::high_resolution_clock::now();
}
glm::vec2 start;
glm::vec2 end;
Expand All @@ -38,6 +41,14 @@ class GameObjectManager {
}

CursorSelect(&start, &end);

//currency update
std::chrono::high_resolution_clock::time_point m_currentTime = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = m_currentTime - m_StartTime;
if(elapsed.count()-m_lastElapsed>=1){//update every second
m_lastElapsed=elapsed.count();
updateTotalCurrency();
}
}

void CursorSelect(glm::vec2 *start, glm::vec2 *end) {
Expand All @@ -62,7 +73,7 @@ class GameObjectManager {
newstruct->Start();
m_BuiltStructure.push_back(newstruct);
}
void unitAppend(std::shared_ptr<Avatar> newUnit) {
void Append(std::shared_ptr<Avatar> newUnit) {
m_UnitArray.push_back(newUnit);
printf("(GOM) push back success\n");
}
Expand All @@ -76,16 +87,32 @@ class GameObjectManager {
}
return totalPower;
}
int GetTotalCurrency() {
return m_Player->getTotalCurrency();
}
void updateTotalCurrency(){
int totalCurrency = m_Player->getTotalCurrency();
if(m_BuiltStructure.size()>0){
for (int i = 0; i < m_BuiltStructure.size(); i++) {
totalCurrency += m_BuiltStructure[i]->GetBuildingIncome();
}
}
m_Player->setTotalCurrency(totalCurrency);
}

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

void importPlayer(std::shared_ptr<Player> player){m_Player=player;}
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::shared_ptr<Player> m_Player;
std::chrono::high_resolution_clock::time_point m_StartTime;
double m_lastElapsed=0.F;
};

#endif // PRACTICALTOOLSFORSIMPLEDESIGN_GAMEOBJECTMANAGER_HPP
2 changes: 1 addition & 1 deletion include/Scene/DefaultScene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class DefaultScene {
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;
std::shared_ptr<GameObjectManager> m_Manager = std::make_shared<GameObjectManager>();

Avatar m_dummy;
};
Expand Down
2 changes: 1 addition & 1 deletion include/Structure/AdvencePowerPlants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "Structure.hpp"
class ADVPowerPlants : public Structure {
public:
ADVPowerPlants(float electricPower = 200.F, float buildingTime = 25.F,
ADVPowerPlants(float electricPower = 200.F, float buildingTime = 25.F*CHEAT,
float buildingCost = 500.F, float buildingHp = 700.F,
HouseType house = HouseType::NONE)
: Structure(electricPower, buildingTime, buildingCost, buildingHp,
Expand Down
3 changes: 2 additions & 1 deletion include/Structure/Barracks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

#include "WayPointStructure.hpp"


class Barracks : public WayPointStructure {
public:
Barracks(float electricPower = -20.F, float buildingTime = 1.F,
Barracks(float electricPower = -20.F, float buildingTime = 15.F*CHEAT,
float buildingCost = 300.F, float buildingHp = 800.F,
HouseType house = HouseType::NONE)

Expand Down
3 changes: 2 additions & 1 deletion include/Structure/OreRefinery.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
class OreRefinery : public WayPointStructure {

public:
OreRefinery(float electricPower = -30.F, float buildingTime = 100.F,
OreRefinery(float electricPower = -30.F, float buildingTime = 100.F*CHEAT,
float buildingCost = 2000.F, float buildingHp = 900.F,
HouseType house = HouseType::NONE)
: WayPointStructure(electricPower, buildingTime, buildingCost,
buildingHp,
GameObjectID(unitType::ORE_REF, house)){};
void Start() override;
virtual float GetBuildingIncome()override{return 50.F;}//debug
};
#endif
2 changes: 1 addition & 1 deletion include/Structure/PowerPlants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class PowerPlants : public Structure {
public:
PowerPlants(float electricPower = 100.F, float buildingTime = 15.F,
PowerPlants(float electricPower = 100.F, float buildingTime = 15.F*CHEAT,
float buildingCost = 300.F, float buildingHp = 400.F,
HouseType house = HouseType::NONE)
: Structure(electricPower, buildingTime, buildingCost, buildingHp,
Expand Down
4 changes: 4 additions & 0 deletions include/Structure/Structure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "Util/TransformUtils.hpp"
#include "glm/glm.hpp"
#define DEFAULT_ZINDEX 15
#define CHEAT 0.1F

class Structure : public Util::GameObject, public Selectable {

Expand Down Expand Up @@ -83,6 +84,8 @@ class Structure : public Util::GameObject, public Selectable {
float GetElectricPower();
float GetBuildingTime();
float GetBuildingCost();
virtual float GetBuildingIncome(){return buildingIncome;};
void SetBuildingIncome(float income){buildingIncome=income;}
float GetBuildingHp();
GameObjectID GetID() { return m_ID; }

Expand All @@ -95,6 +98,7 @@ class Structure : public Util::GameObject, public Selectable {
float buildingTime;
float buildingCost;
float buildingHp;
float buildingIncome=0.F;
HighLight m_HighLight;
GameObjectID m_ID;

Expand Down
9 changes: 7 additions & 2 deletions 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 "GameObjectManager.hpp"
#include "Player.hpp"
#include "SpriteSheet.hpp"
#include "UI/UIScriptProcess.hpp"
Expand Down Expand Up @@ -40,8 +41,11 @@ class UIClass {

//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;}

void importPlayer(std::shared_ptr<Player> m_Player){this->m_Player=m_Player; ButtonScript.importPlayer(m_Player);}
void importGameObjManager(std::shared_ptr<GameObjectManager> gameObjectManager){
m_gameObjectManager=gameObjectManager;
ButtonScript.importGameObjManager(gameObjectManager);
}
//check if building has built
void checkExistBuilding(std::vector<std::shared_ptr<Structure>> buildingList);
private:
Expand Down Expand Up @@ -112,6 +116,7 @@ class UIClass {
//ptr import from scene
std::shared_ptr<MapClass> m_Map;
std::shared_ptr<Player> m_Player;
std::shared_ptr<GameObjectManager> m_gameObjectManager;
};

#endif // PRACTICALTOOLSFORSIMPLEDESIGN_UI_HPP
32 changes: 27 additions & 5 deletions include/UI/UIScriptProcess.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_UISCRIPTPROCESS_HPP
#define PRACTICALTOOLSFORSIMPLEDESIGN_UISCRIPTPROCESS_HPP
#include "GameObjectID.hpp"
#include "GameObjectManager.hpp"
#include "Player.hpp"
#include "Structure/AdvencePowerPlants.hpp"
#include "Structure/Barracks.hpp"
#include "Structure/OreRefinery.hpp"
Expand All @@ -20,6 +22,9 @@

class UIScriptProcess {
private:
std::shared_ptr<Player> m_player;
std::shared_ptr<GameObjectManager> m_gameObjectManager;

// building
bool b_Baracks = false;
bool b_OreRefinery = false;
Expand All @@ -28,28 +33,37 @@ class UIScriptProcess {
bool b_ADVPowerPlant = false;

float m_buildCoolDownTime = 0.F;
float m_offPowerBuildCoolDownTime = 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_currentCountDownTime;
std::chrono::time_point<std::chrono::high_resolution_clock> m_buildTempTime;
std::chrono::time_point<std::chrono::high_resolution_clock>m_currentCountDownTime;
float m_lastBuildElapsed=0.F;
int m_currentBuildRemainingCost=0;
// building end

// avatar
bool b_isReadyToSpawn = false;
float m_spawnCooldownTime = 0.F;
float m_spawnCoolDownTime = 0.F;
float m_offPowerSpawnCoolDownTime = 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;
std::chrono::time_point<std::chrono::high_resolution_clock> m_SpawnTempTime;
float m_lastSpawnElapsed=0.F;
int m_currentSpawnRemainingCost=0;
// avatar end

public:
UIScriptProcess(){};
~UIScriptProcess(){};
//

//Getter/Setter
bool GetIfFinished(unitType type);
int GetObjCost(unitType type);
// if b_isBuildingInCoolDown==false not currently building
// true currently is building
void SetSTALL(bool value) { b_isBuildingInCoolDown = value; };
Expand All @@ -59,15 +73,19 @@ class UIScriptProcess {
void SetIfFinished(unitType type, bool value);
float GetCDLeft();
std::string GetFormattedCD();

// Event
void AddToBuildQueue(unitType type);
void AddToSpawnQueue(unitType type);
void Update(bool queueContinue);

// CountDown
void SetCountDown(float time);
void SetBuildCountDown(float time);
void SetSpawnCountDown(float time);
float GetBuildCountDownTime();
float GetSpawnCountDownTime();
void CountDown();

// transform to ptr
float GetStructureTime(unitType type);
float GetSpawnTime(unitType type);
Expand All @@ -79,6 +97,10 @@ class UIScriptProcess {
bool getIfReadytoSpawn(){return b_isReadyToSpawn;}
void setIfReadytoSpawn(bool b){b_isReadyToSpawn=b;}

//import from scene
void importPlayer(std::shared_ptr<Player> player){m_player=player;}
void importGameObjManager(std::shared_ptr<GameObjectManager> gameObjectManager){m_gameObjectManager=gameObjectManager;}

private:
std::shared_ptr<Structure> barracks = std::make_shared<Barracks>();
std::shared_ptr<Structure> oreRefinery = std::make_shared<OreRefinery>();
Expand Down
13 changes: 8 additions & 5 deletions src/Scene/DefaultScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,19 @@ void DefaultScene::Start() {
// m_GameObjectManager.Start();

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

m_Player->setTotalCurrency(50000);

m_Player->setTotalCurrency(5000);
}

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

m_Manager.Update();
m_Manager->Update();

Util::Transform trans;
m_Map->Draw(trans, 0);
Expand All @@ -59,10 +62,10 @@ void DefaultScene::Update() {
// m_GameObjectManager.Update();

if (m_UI.getIfAnyBuildingReadyToBuild()) {
m_Manager.Append(m_UI.getSelectedBuilding());
m_Manager->Append(m_UI.getSelectedBuilding());
}
m_UI.checkExistBuilding(m_Manager.getStructureArray());
m_UI.checkExistBuilding(m_Manager->getStructureArray());
if(m_UI.getIfUnitReadyToSpawn()){
m_Manager.unitAppend(m_UI.getUnitFromUI());
m_Manager->Append(m_UI.getUnitFromUI());
}
}
3 changes: 1 addition & 2 deletions src/Structure/OreRefinery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
#include "Structure/OreRefinery.hpp"
void OreRefinery::Start() {
// Set Texture----------------------------------------
this->SetDrawable(
std::make_unique<Util::Image>("../assets/sprites/barracks.png"));
SetDrawable(std::make_unique<Util::Image>("../assets/sprites/OreRefinery.png"));
m_wayPoint->SetDrawable(
std::make_unique<Util::Image>("../assets/sprites/flagB.png"));
m_HighLight.SetDrawable(
Expand Down
8 changes: 4 additions & 4 deletions src/UI/UI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ void UIClass::ShowHeaderSection() {
.c_str());
// ImGui::Text(fmt::format("Zoom: {}",
// m_SceneCamera.getCameraZoom()).c_str());
ImGui::Text(fmt::format("$ {}", 1000).c_str());
ImGui::Text(fmt::format("Power {}", 50).c_str());
ImGui::Text(fmt::format("$ {}", m_Player->getTotalCurrency()).c_str());
ImGui::Text(fmt::format("Power {}", m_gameObjectManager->GetTotalPower()).c_str());
ImGui::PushFont(sacker_med);
if (ImGui::Button("Grid")) {
}
Expand Down Expand Up @@ -456,14 +456,14 @@ void UIClass::checkExistBuilding(
} else if (std::dynamic_pointer_cast<WarFactory>(i) &&
!b_warfactoryBuilt) {
m_warfactoryTargetCell = MapUtil::GlobalCoordToCellCoord(
std::dynamic_pointer_cast<Barracks>(i)->GetWayPointLocation());
std::dynamic_pointer_cast<WarFactory>(i)->GetWayPointLocation());
b_warfactoryBuilt = true;
m_warfactoryCell =
MapUtil::GlobalCoordToCellCoord(i->GetObjectLocation());
} else if (std::dynamic_pointer_cast<OreRefinery>(i) &&
!b_orerefineryBuilt) {
m_orerefineryTargetCell = MapUtil::GlobalCoordToCellCoord(
std::dynamic_pointer_cast<Barracks>(i)->GetWayPointLocation());
std::dynamic_pointer_cast<OreRefinery>(i)->GetWayPointLocation());
b_orerefineryBuilt = true;
m_orerefineryCell =
MapUtil::GlobalCoordToCellCoord(i->GetObjectLocation());
Expand Down
Loading

0 comments on commit d8b110e

Please sign in to comment.