From 9f49d6dd17588b19d8b1b4a80e3f454cf45ea612 Mon Sep 17 00:00:00 2001 From: jonylu7 Date: Fri, 8 Mar 2024 22:43:23 +0800 Subject: [PATCH] draw lines and update --- include/Structure/Structure.hpp | 40 +++++++++-------- include/Structure/WarFactory.hpp | 35 ++++++++------- src/Grid.cpp | 3 +- src/Scene/DefaultScene.cpp | 2 +- src/Structure/WarFactory.cpp | 77 ++++++++++++++++++-------------- 5 files changed, 86 insertions(+), 71 deletions(-) diff --git a/include/Structure/Structure.hpp b/include/Structure/Structure.hpp index 939a736f..a8432175 100644 --- a/include/Structure/Structure.hpp +++ b/include/Structure/Structure.hpp @@ -4,13 +4,13 @@ #ifndef PRACTICALTOOLSFORSIMPLEDESIGN_STRUCTURE_HPP #define PRACTICALTOOLSFORSIMPLEDESIGN_STRUCTURE_HPP +#include "HighLight.h" +#include "Map.hpp" #include "Util/GameObject.hpp" +#include "Util/Image.hpp" #include "Util/Input.hpp" #include "Util/TransformUtils.hpp" #include "glm/glm.hpp" -#include "HighLight.h" -#include "Util/Image.hpp" -#include "Map.hpp" #define CELL 48.F #define DEFAULT_ZINDEX 15 @@ -18,17 +18,22 @@ class Structure : public Util::GameObject { public: enum class updateMode { Invisidable, Moveable, Fixed }; - Structure(): - electricPower(100.F),buildingTime(100.F), - buildingCost(100.F),buildingHp(100.F){ - m_CurrentState=updateMode::Invisidable; + Structure() + : electricPower(100.F), + buildingTime(100.F), + buildingCost(100.F), + buildingHp(100.F) { + m_CurrentState = updateMode::Invisidable; }; - Structure(float electricPower,float buildingTime, - float buildingCost,float buildingHp): - electricPower(electricPower),buildingTime(buildingTime), - buildingCost(buildingCost),buildingHp(buildingHp){ - m_Transform.scale={1,1}; - //this->SetZIndex(DEFAULT_ZINDEX); + + Structure(float electricPower, float buildingTime, float buildingCost, + float buildingHp) + : electricPower(electricPower), + buildingTime(buildingTime), + buildingCost(buildingCost), + buildingHp(buildingHp) { + m_Transform.scale = {1, 1}; + // this->SetZIndex(DEFAULT_ZINDEX); }; ~Structure(){}; @@ -37,13 +42,12 @@ class Structure : public Util::GameObject { virtual void updateMoveable(); virtual void updateInvinsible(); - void Start() override; updateMode GetCurrentUpdateMode() const { return m_CurrentState; }; void SetCurrentUpdateMode(updateMode mode) { m_CurrentState = mode; }; virtual void SetObjectLocation(glm::vec2 location); - glm::vec2 GetObjectLocation(){return this->ObjectLocation; } - glm::vec2 GetTranScale(){return m_Transform.scale;}; + glm::vec2 GetObjectLocation() { return this->ObjectLocation; } + glm::vec2 GetTranScale() { return m_Transform.scale; }; glm::vec2 ChangeToCell(glm::vec2 location); virtual void onSelected(bool selected); @@ -68,13 +72,13 @@ class Structure : public Util::GameObject { private: updateMode m_CurrentState = updateMode::Invisidable; - glm::vec2 ObjectLocation={100,100}; + glm::vec2 ObjectLocation = {100, 100}; float electricPower; float buildingTime; float buildingCost; float buildingHp; HighLight m_HighLight; - bool b_select= false; + bool b_select = false; }; #endif // PRACTICALTOOLSFORSIMPLEDESIGN_STRUCTURE_HPP diff --git a/include/Structure/WarFactory.hpp b/include/Structure/WarFactory.hpp index 0e12b256..163d0201 100644 --- a/include/Structure/WarFactory.hpp +++ b/include/Structure/WarFactory.hpp @@ -1,35 +1,36 @@ #ifndef PRACTICALTOOLSFORSIMPLEDESIGN_STRUCTURE_WarFactory_HPP #define PRACTICALTOOLSFORSIMPLEDESIGN_STRUCTURE_WarFactory_HPP +#include "Grid.hpp" +#include "HighLight.h" +#include "Line.hpp" #include "Structure/IWayPointStructure.hpp" #include "Structure/Structure.hpp" #include "Util/Image.hpp" -#include "Util/Keycode.hpp" #include "Util/Input.hpp" +#include "Util/Keycode.hpp" #include "WayPoint.hpp" -#include "HighLight.h" -#include "Line.hpp" -#include "Grid.hpp" -class WarFactory : public Structure,public IWayPointStructure{ +class WarFactory : public Structure, public IWayPointStructure { +public: + WarFactory(float electricPower = -30.F, float buildingTime = 100.F, + float buildingCost = 2000.F, float buildingHp = 1000.F) + : Structure(electricPower, buildingTime, buildingCost, buildingHp){}; + void Start() override; + + virtual void onSelected(bool selected) override; + virtual void SetAttachVisible(bool visible) override; + + virtual void updateFixed() override; + virtual void updateMoveable() override; + private: std::shared_ptr m_wayPoint = std::make_shared(); HighLight m_HighLight; - bool b_select= true; + bool b_select = true; Grid m_Grid; Line m_Line; std::vector m_lineVector; -public: - WarFactory(float electricPower=-30.F,float buildingTime=100.F, - float buildingCost=2000.F,float buildingHp=1000.F): - Structure(electricPower, buildingTime, buildingCost, buildingHp){}; - void Start() override; - - virtual void onSelected(bool selected)override; - virtual void SetAttachVisible(bool visible)override; - - virtual void updateFixed()override; - virtual void updateMoveable()override; }; #endif diff --git a/src/Grid.cpp b/src/Grid.cpp index 9bec0f9e..b1466502 100644 --- a/src/Grid.cpp +++ b/src/Grid.cpp @@ -30,7 +30,7 @@ void Grid::DrawUsingCamera(const Util::Transform &transform, m_Program.Bind(); m_Program.Validate(); m_VertexArray->Bind(); - + auto cp = CameraClass::getProjectionMatrix(); auto cv = CameraClass::getViewMatrix(); @@ -71,6 +71,7 @@ void Grid::InitVertexAndColor() { std::make_unique(color, 3)); } void Grid::setLine(glm::vec2 from, glm::vec2 to) { + m_VertexArray = std::make_unique(); m_lineVector.clear(); // 清空原有的線段 m_lineVector.push_back(Line(from, to)); InitVertexAndColor(); // 重新初始化頂點和顏色 diff --git a/src/Scene/DefaultScene.cpp b/src/Scene/DefaultScene.cpp index 337f1a09..dc77f443 100644 --- a/src/Scene/DefaultScene.cpp +++ b/src/Scene/DefaultScene.cpp @@ -45,7 +45,7 @@ void DefaultScene::Start() { m_Map.readMapAndTileSet(m_OgMap, m_tileSets); */ - m_Map.Init(maps, m_TileSetSpriteSheet, 10, 10); + m_Map.Init(maps, m_TileSetSpriteSheet, 255, 255); m_UI.Start(); // Way Point Test Start--------------- diff --git a/src/Structure/WarFactory.cpp b/src/Structure/WarFactory.cpp index 6fbd3437..1cb626dd 100644 --- a/src/Structure/WarFactory.cpp +++ b/src/Structure/WarFactory.cpp @@ -1,66 +1,75 @@ #include "Structure/WarFactory.hpp" -void WarFactory::Start(){ - this->SetWayPointLocation({this->GetObjectLocation().x+20,this->GetObjectLocation().y+20});//Debug Only - //Set Texture---------------------------------------- - this->SetDrawable(std::make_unique("../assets/sprites/barracks.png")); - m_wayPoint->SetDrawable(std::make_unique("../assets/sprites/flagB.png")); - m_HighLight.SetDrawable(std::make_unique("../assets/sprites/HighLightB.png")); +void WarFactory::Start() { + this->SetWayPointLocation({this->GetObjectLocation().x + 20, + this->GetObjectLocation().y + 20}); // Debug Only + // Set Texture---------------------------------------- + this->SetDrawable( + std::make_unique("../assets/sprites/barracks.png")); + m_wayPoint->SetDrawable( + std::make_unique("../assets/sprites/flagB.png")); + m_HighLight.SetDrawable( + std::make_unique("../assets/sprites/HighLightB.png")); this->SetZIndex(DEFAULT_ZINDEX); m_wayPoint->SetZIndex(DEFAULT_ZINDEX); m_HighLight.SetZIndex(DEFAULT_ZINDEX); - //Set Attachment Location & Visibility---------------------------------------- + // Set Attachment Location & + // Visibility---------------------------------------- m_wayPoint->SetObjectLocation(this->GetWayPointLocation()); m_HighLight.SetHLScale(this->GetTranScale()); m_HighLight.SetObjectLocation(this->GetObjectLocation()); this->SetAttachVisible(false); - //Line Setup---------------------------------------- - m_Grid.Start(std::vector({Line(GetObjectLocation(),GetWayPointLocation())})); + // Line Setup---------------------------------------- + m_Grid.Start(std::vector({Line( + GetObjectLocation() + glm::vec2{50.F, 50.F}, GetWayPointLocation())})); m_Grid.SetActivate(false); - //State + // State SetCurrentUpdateMode(Structure::updateMode::Moveable); } void WarFactory::updateFixed() { - m_Grid.setLine(GetObjectLocation(),GetWayPointLocation()); + m_Grid.setLine(GetObjectLocation(), GetWayPointLocation()); Util::Transform Trans; - m_Grid.DrawUsingCamera(Trans,DEFAULT_ZINDEX); + m_Grid.DrawUsingCamera(Trans, DEFAULT_ZINDEX); - - //Attachment and self readjust location and draw--------------- + // Attachment and self readjust location and draw--------------- m_wayPoint->SetObjectLocation(this->GetWayPointLocation()); m_HighLight.SetObjectLocation(this->GetObjectLocation()); this->Draw(); m_wayPoint->Draw(); m_HighLight.Draw(); - //Script when select-------------------- - if(Util::Input::IsKeyDown(Util::Keycode::T)){ - b_select=!b_select; + // Script when select-------------------- + if (Util::Input::IsKeyDown(Util::Keycode::T)) { + b_select = !b_select; } - if(b_select){ - onSelected(true); - }else{ - onSelected(false); + if (b_select) { + onSelected(true); + } else { + onSelected(false); } } -void WarFactory::updateMoveable(){ - glm::vec2 location=Util::Input::GetCursorPosition(); - location=MapClass::ScreenToGlobalCoord(location); +void WarFactory::updateMoveable() { + glm::vec2 location = Util::Input::GetCursorPosition(); + location = MapClass::ScreenToGlobalCoord(location); this->SetObjectLocation(location); this->SetVisible(true); this->Draw(); - if(Util::Input::IsKeyPressed(Util::Keycode::MOUSE_LB)){ - this->SetObjectLocation(location); - m_HighLight.SetObjectLocation({GetObjectLocation().x+0.5*CELL,GetObjectLocation().y+0.5*CELL}); - this->SetWayPointLocation({this->GetObjectLocation().x+48-0.5*CELL,this->GetObjectLocation().y+48-0.5*CELL}); - onSelected(false); - this->SetCurrentUpdateMode(updateMode::Fixed); + if (Util::Input::IsKeyPressed(Util::Keycode::MOUSE_LB)) { + this->SetObjectLocation(location); + m_HighLight.SetObjectLocation({GetObjectLocation().x + 0.5 * CELL, + GetObjectLocation().y + 0.5 * CELL}); + this->SetWayPointLocation( + {this->GetObjectLocation().x + 48 - 0.5 * CELL, + this->GetObjectLocation().y + 48 - 0.5 * CELL}); + onSelected(false); + this->SetCurrentUpdateMode(updateMode::Fixed); } } void WarFactory::onSelected(bool selected) { this->SetAttachVisible(selected); - if(selected){ - if(Util::Input::IsKeyDown(Util::Keycode::V)){ - this->SetWayPointLocation({GetWayPointLocation().x+5,GetWayPointLocation().y+5}); + if (selected) { + if (Util::Input::IsKeyDown(Util::Keycode::V)) { + this->SetWayPointLocation( + {GetWayPointLocation().x + 5, GetWayPointLocation().y + 5}); } } } @@ -68,4 +77,4 @@ void WarFactory::SetAttachVisible(bool visible) { m_wayPoint->SetVisible(visible); m_HighLight.SetVisible(visible); m_Grid.SetActivate(visible); -} \ No newline at end of file +}