Skip to content

Commit

Permalink
Merge pull request #25 from ntut-Tu/TEST
Browse files Browse the repository at this point in the history
button update
  • Loading branch information
jonylu7 authored Mar 15, 2024
2 parents ae4f10e + bfefaa6 commit 65c5e9f
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 56 deletions.
8 changes: 5 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions include/GameObjectID.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_GAMEOBJECTID_HPP
#define PRACTICALTOOLSFORSIMPLEDESIGN_GAMEOBJECTID_HPP
#include <string>
#include <unordered_map>
enum class unitType {
// buildings
POWER_PLANT,
Expand Down
1 change: 1 addition & 0 deletions include/Scene/DefaultScene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class DefaultScene {
std::vector<int> m_OgMap;
std::shared_ptr<SpriteSheet> m_TileSetSpriteSheet =
std::make_shared<SpriteSheet>();
std::vector<std::shared_ptr<Structure>> m_BuiltStructure;
};

#endif // PRACTICALTOOLSFORSIMPLEDESIGN_DEFAULTSCENE_HPP
4 changes: 3 additions & 1 deletion include/Structure/Structure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -38,6 +39,7 @@ class Structure : public Util::GameObject {
m_Transform.scale = {1, 1};
// this->SetZIndex(DEFAULT_ZINDEX);
};

~Structure(){};

void Update() override;
Expand All @@ -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 getBuilt(){ if(m_CurrentState==updateMode::Fixed){return true;}else{return false;}}
/*
void SetElectricPower(float electricPower);
void SetBuildingTime(float buildingTime);
Expand Down
9 changes: 6 additions & 3 deletions include/UI/UI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -25,8 +25,12 @@ class UIClass {
static void setUnitConstructCount(unitType type, int value) {
s_unitConstructCount[type] += value;
}
static void resetUnitConstructCount(unitType type) {
s_unitConstructCount[type] =0;
}

private:
UIScriptProcess ButtonScript;
void InitUnitQueue();
void ShowCursorSelectionRegion(ImVec2 *start_pos, ImVec2 *end_pos,
ImGuiMouseButton mouse_button);
Expand All @@ -39,7 +43,6 @@ class UIClass {
getImageButtonBySpriteSheetIndex(std::shared_ptr<SpriteSheet> spritesheet,
int index);

private:
std::shared_ptr<SpriteSheet> m_StructureIconSpriteSheet =
std::make_shared<SpriteSheet>();
std::shared_ptr<SpriteSheet> m_InfantryIconSpriteSheet =
Expand All @@ -50,7 +53,7 @@ class UIClass {
static std::unordered_map<unitType, unsigned int> s_unitConstructCount;
Grid m_Grid;
glm::vec2 m_GridSize = {100, 100};
UIScriptProcess ButtonScript;


std::shared_ptr<Structure> barracks = std::make_shared<Barracks>();
std::shared_ptr<Structure> oreRefinery = std::make_shared<OreRefinery>();
Expand Down
19 changes: 7 additions & 12 deletions include/UI/UIScriptProcess.h → include/UI/UIScriptProcess.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
// 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 "GameObjectID.hpp"
#include <queue>
#include <future>
#include <chrono>
Expand All @@ -23,10 +24,13 @@ class UIScriptProcess{
bool b_STALL=false;
std::queue<std::shared_ptr<Structure>> buildQueue;
std::shared_ptr<Structure> temp_PTR;
std::chrono::time_point<std::chrono::high_resolution_clock> m_StartTime;
public:
UIScriptProcess(){};
~UIScriptProcess(){};
//
bool GetIfFinished(std::shared_ptr<Structure> structure);
bool GetIfFinished(unitType type);
void SetFinished(std::shared_ptr<Structure> structure);
//Event
void buttonEvent(std::shared_ptr<Structure>(m_Structure));
Expand All @@ -36,14 +40,5 @@ class UIScriptProcess{
//CountDown
void SetCoolDown(float time);
void CountDown();
//Delay??
std::future<void> 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
#endif // PRACTICALTOOLSFORSIMPLEDESIGN_UISCRIPTPROCESS_HPP
51 changes: 51 additions & 0 deletions include/Unit/PathfindingUnit.hpp
Original file line number Diff line number Diff line change
@@ -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 <m_targetCell.y - m_currentCell.y &&m_targetCell.x - m_currentCell.x > 0) {
m_currentDir = MoveDirection::Down;
}else if(m_targetCell.x-m_currentCell.x<m_targetCell.y-m_currentCell.y&&m_targetCell.x-m_currentCell.x<0){
m_currentDir=MoveDirection::Left;
}else if(m_targetCell.x-m_currentCell.x==m_targetCell.y-m_currentCell.y&&m_targetCell.x-m_currentCell.x<0){
m_currentDir=MoveDirection::Down;
}
}
};
#endif // PRACTICALTOOLSFORSIMPLEDESIGN_PATHFINDINGUNIT_HPP
58 changes: 35 additions & 23 deletions src/Scene/DefaultScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Util::Image>("../assets/sprites/Raccoon3.jpg"));
m_GameObjectList[0]->SetZIndex(10);

m_GameObjectList[1]->SetDrawable(
std::make_unique<Util::Image>("../assets/sprites/capybara.png"));
m_Structure->SetDrawable(
std::make_unique<Util::Image>("../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);
Expand All @@ -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() {
Expand All @@ -77,4 +56,37 @@ void DefaultScene::Update() {

m_testdraw.DrawUsingCamera(trans2, 1);
// m_GameObjectManager.Update();

if(UIClass::getUnitConstructCount(unitType::BARRACKS)>0){
UIClass::setUnitConstructCount(unitType::BARRACKS,-1);
m_BuiltStructure.push_back(std::make_unique<Barracks>());
m_BuiltStructure.back()->Start();
}
if(UIClass::getUnitConstructCount(unitType::POWER_PLANT) > 0) {
UIClass::setUnitConstructCount(unitType::POWER_PLANT,-1);
m_BuiltStructure.push_back(std::make_unique<PowerPlants>());
m_BuiltStructure.back()->Start();
}

if(UIClass::getUnitConstructCount(unitType::ORE_REF) > 0) {
UIClass::setUnitConstructCount(unitType::ORE_REF,-1);
m_BuiltStructure.push_back(std::make_unique<OreRefinery>());
m_BuiltStructure.back()->Start();
}

if(UIClass::getUnitConstructCount(unitType::WAR_FACT) > 0) {
UIClass::setUnitConstructCount(unitType::WAR_FACT,-1);
m_BuiltStructure.push_back(std::make_unique<WarFactory>());
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<ADVPowerPlants>());
m_BuiltStructure.back()->Start();
}
for(auto i :m_BuiltStructure){
i->Update();
}

}
1 change: 1 addition & 0 deletions src/Structure/Structure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ void Structure::updateMoveable() {
if (Util::Input::IsKeyPressed(Util::Keycode::MOUSE_LB)) {
this->SetObjectLocation(location);
this->SetCurrentUpdateMode(updateMode::Fixed);
//在這裡增加設置Tile屬性
}
}
void Structure::updateInvinsible() {
Expand Down
35 changes: 25 additions & 10 deletions src/UI/UI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,42 +82,57 @@ 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");
}

ImGui::NewLine();
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")) {
Expand Down
Loading

0 comments on commit 65c5e9f

Please sign in to comment.