Skip to content

Commit

Permalink
draw lines and update
Browse files Browse the repository at this point in the history
  • Loading branch information
jonylu7 committed Mar 8, 2024
1 parent eeeaa19 commit 9f49d6d
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 71 deletions.
40 changes: 22 additions & 18 deletions include/Structure/Structure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,36 @@

#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

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

Expand All @@ -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);
Expand All @@ -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
35 changes: 18 additions & 17 deletions include/Structure/WarFactory.hpp
Original file line number Diff line number Diff line change
@@ -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<WayPoint> m_wayPoint = std::make_shared<WayPoint>();
HighLight m_HighLight;
bool b_select= true;
bool b_select = true;
Grid m_Grid;
Line m_Line;
std::vector<Line> 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
3 changes: 2 additions & 1 deletion src/Grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -71,6 +71,7 @@ void Grid::InitVertexAndColor() {
std::make_unique<Core::VertexBuffer>(color, 3));
}
void Grid::setLine(glm::vec2 from, glm::vec2 to) {
m_VertexArray = std::make_unique<Core::VertexArray>();
m_lineVector.clear(); // 清空原有的線段
m_lineVector.push_back(Line(from, to));
InitVertexAndColor(); // 重新初始化頂點和顏色
Expand Down
2 changes: 1 addition & 1 deletion src/Scene/DefaultScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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---------------
Expand Down
77 changes: 43 additions & 34 deletions src/Structure/WarFactory.cpp
Original file line number Diff line number Diff line change
@@ -1,71 +1,80 @@
#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<Util::Image>("../assets/sprites/barracks.png"));
m_wayPoint->SetDrawable(std::make_unique<Util::Image>("../assets/sprites/flagB.png"));
m_HighLight.SetDrawable(std::make_unique<Util::Image>("../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<Util::Image>("../assets/sprites/barracks.png"));
m_wayPoint->SetDrawable(
std::make_unique<Util::Image>("../assets/sprites/flagB.png"));
m_HighLight.SetDrawable(
std::make_unique<Util::Image>("../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>({Line(GetObjectLocation(),GetWayPointLocation())}));
// Line Setup----------------------------------------
m_Grid.Start(std::vector<Line>({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});
}
}
}
void WarFactory::SetAttachVisible(bool visible) {
m_wayPoint->SetVisible(visible);
m_HighLight.SetVisible(visible);
m_Grid.SetActivate(visible);
}
}

0 comments on commit 9f49d6d

Please sign in to comment.