Skip to content

Commit

Permalink
Merge pull request #14 from ntut-Tu/main
Browse files Browse the repository at this point in the history
Structure update
  • Loading branch information
jonylu7 authored Mar 10, 2024
2 parents 7a5d376 + b47c4c7 commit ac4b69c
Show file tree
Hide file tree
Showing 22 changed files with 196 additions and 221 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ set(SRC_FILES

${SRC_DIR}/Structure/Structure.cpp
${SRC_DIR}/Structure/WayPoint.cpp
${SRC_DIR}/Structure/WayPointTool.cpp
${SRC_DIR}/Structure/MousOverlapTool.cpp
${SRC_DIR}/Structure/IWayPointStructure.cpp
${SRC_DIR}/Structure/HighLight.cpp
${SRC_DIR}/UIScriptProcess.cpp
Expand Down Expand Up @@ -141,7 +141,7 @@ set(INCLUDE_FILES

${INCLUDE_DIR}/Structure/Structure.hpp
${INCLUDE_DIR}/Structure/WayPoint.hpp
${INCLUDE_DIR}/Structure/WayPointTool.h
${INCLUDE_DIR}/Structure/MousOverlapTool.h
${INCLUDE_DIR}/Structure/IWayPointStructure.hpp
${INCLUDE_DIR}/HighLight.h
${INCLUDE_DIR}/UIScriptProcess.h
Expand Down
1 change: 1 addition & 0 deletions include/Map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class MapClass : public Core::Drawable {
}

m_Grid.Start(lineV);
m_Grid.SetActivate(true);
}

private:
Expand Down
5 changes: 3 additions & 2 deletions include/Scene/DefaultScene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
#include "Structure/AdvencePowerPlants.hpp"
#include "Structure/Barracks.hpp"
#include "Structure/IWayPointStructure.hpp"
#include "Structure/MousOverlapTool.h"
#include "Structure/OreRefinery.hpp"
#include "Structure/PowerPlants.hpp"
#include "Structure/WarFactory.hpp"
#include "Structure/WayPoint.hpp"
#include "Structure/WayPointTool.h"


class DefaultScene {
public:
Expand Down Expand Up @@ -58,7 +59,7 @@ class DefaultScene {
std::make_shared<SpriteSheet>();

// Way Point Test Start---------------
WayPointTool m_waypointTool;
MousOverlapTool m_waypointTool;
std::shared_ptr<Barracks> m_Barracks = std::make_shared<Barracks>();
std::shared_ptr<OreRefinery> m_OreRefinery =
std::make_shared<OreRefinery>();
Expand Down
4 changes: 2 additions & 2 deletions include/Structure/Barracks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define PRACTICALTOOLSFORSIMPLEDESIGN_STRUCTURE_BARRACKS_HPP

#include "Structure/IWayPointStructure.hpp"
#include "Structure/MousOverlapTool.h"
#include "Structure/Structure.hpp"
#include "Util/Image.hpp"
#include "Util/Keycode.hpp"
Expand All @@ -15,7 +16,6 @@ class Barracks : public Structure,public IWayPointStructure{
private:
std::shared_ptr<WayPoint> m_wayPoint = std::make_shared<WayPoint>();
HighLight m_HighLight;
bool b_select= true;
Grid m_Grid;
Line m_Line;
std::vector<Line> m_lineVector;
Expand All @@ -28,8 +28,8 @@ class Barracks : public Structure,public IWayPointStructure{
virtual void onSelected(bool selected)override;
virtual void SetAttachVisible(bool visible)override;

virtual void updateFixed()override;
virtual void updateMoveable()override;
virtual void attachmentUpdate()override;
};

#endif
6 changes: 4 additions & 2 deletions include/Structure/IWayPointStructure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
class IWayPointStructure {
private:
glm::vec2 waypointLocation = {0, 0};

public:
virtual glm::vec2 GetWayPointLocation() { return this->waypointLocation; };
void SetWayPointLocation(glm::vec2 newLocation) {
void SetWayPointLocation(glm::vec2 newLocation) { //=DrawLocation+Cell
int _x=newLocation.x/CELL_SIZE.x;
int _y=newLocation.y/CELL_SIZE.y;
newLocation={_x*CELL_SIZE.x,_y*CELL_SIZE.y};
this->waypointLocation = {newLocation.x + 0.5 * CELL_SIZE.x,
newLocation.y + 0.5 * CELL_SIZE.y};
};
Expand Down
23 changes: 23 additions & 0 deletions include/Structure/MousOverlapTool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// Created by nudle on 2024/3/5.
//

#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_MOUSOVERLAPTOOL_H
#define PRACTICALTOOLSFORSIMPLEDESIGN_MOUSOVERLAPTOOL_H

#include <vector>
#include "glm/glm.hpp"
#include "Util/Input.hpp"
#include "Structure/IWayPointStructure.hpp"
#include "Map.hpp"

class MousOverlapTool {
private:
public:
MousOverlapTool(){};
~MousOverlapTool(){};

static bool checkMous(glm::vec2 objPos/*,glm::vec2 objSize*/);
};

#endif // PRACTICALTOOLSFORSIMPLEDESIGN_MOUSOVERLAPTOOL_H
2 changes: 1 addition & 1 deletion include/Structure/OreRefinery.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class OreRefinery:public Structure,public IWayPointStructure{
virtual void onSelected(bool selected)override;
virtual void SetAttachVisible(bool visible)override;

virtual void updateFixed()override;
virtual void updateMoveable()override;
virtual void attachmentUpdate()override;
};
#endif
11 changes: 9 additions & 2 deletions include/Structure/Structure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "Util/Image.hpp"
#include "Util/Input.hpp"
#include "Util/TransformUtils.hpp"
#include "Structure/MousOverlapTool.h"
#include "glm/glm.hpp"
#define CELL 48.F
#define DEFAULT_ZINDEX 15
Expand Down Expand Up @@ -48,10 +49,13 @@ class Structure : public Util::GameObject {
virtual void SetObjectLocation(glm::vec2 location);
glm::vec2 GetObjectLocation() { return this->ObjectLocation; }
glm::vec2 GetTranScale() { return m_Transform.scale; };
virtual void SetAttachVisible(bool visible);
glm::vec2 GetDrawLocation(){return DrawLocation;};

glm::vec2 ChangeToCell(glm::vec2 location);
virtual void onSelected(bool selected);
virtual void SetAttachVisible(bool visible);
virtual void attachmentUpdate(); // this function now will update attachment's location and draw as well


/*
void SetElectricPower(float electricPower);
Expand All @@ -73,12 +77,15 @@ class Structure : public Util::GameObject {
private:
updateMode m_CurrentState = updateMode::Invisidable;
glm::vec2 ObjectLocation = {100, 100};
glm::vec2 DrawLocation = {ObjectLocation.x+CELL_SIZE.x,ObjectLocation.y+CELL_SIZE.y};
float electricPower;
float buildingTime;
float buildingCost;
float buildingHp;
HighLight m_HighLight;
bool b_select = false;
protected:
bool b_selected= false;
bool b_selectingNewWayPoint=false;
};

#endif // PRACTICALTOOLSFORSIMPLEDESIGN_STRUCTURE_HPP
22 changes: 10 additions & 12 deletions include/Structure/WarFactory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,23 @@
#include "WayPoint.hpp"

class WarFactory : public Structure, public IWayPointStructure {
private:
std::shared_ptr<WayPoint> m_wayPoint = std::make_shared<WayPoint>();
HighLight m_HighLight;
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;
virtual void onSelected(bool selected)override;
virtual void SetAttachVisible(bool visible)override;

private:
std::shared_ptr<WayPoint> m_wayPoint = std::make_shared<WayPoint>();
HighLight m_HighLight;
bool b_select = true;
Grid m_Grid;
Line m_Line;
std::vector<Line> m_lineVector;
virtual void updateMoveable()override;
virtual void attachmentUpdate()override;
};

#endif
2 changes: 1 addition & 1 deletion include/Structure/WayPoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class WayPoint : public Util::GameObject {
public:
WayPoint()
: Util::GameObject() {
m_Transform.scale = {0.2, 0.2};
m_Transform.scale = {1, 1};
};
void Update(const Util::Transform &transform = Util::Transform()) override;

Expand Down
21 changes: 0 additions & 21 deletions include/Structure/WayPointTool.h

This file was deleted.

12 changes: 10 additions & 2 deletions include/UI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class UIClass {
std::vector<ImVec2> getSpriteSheetCoordByIndex(int index);
bool getImageButtonBySpriteSheetIndex(int index);

void objUpdate();

private:
SpriteSheet m_SpriteSheet;

Expand All @@ -56,8 +58,14 @@ class UIClass {
std::shared_ptr<Structure> oreRefinery = std::make_shared<OreRefinery>();
std::shared_ptr<Structure> powerPlant = std::make_shared<PowerPlants>();
std::shared_ptr<Structure> warFactory = std::make_shared<WarFactory>();
std::shared_ptr<Structure> advPowerPlant =
std::make_shared<ADVPowerPlants>();
std::shared_ptr<Structure> advPowerPlant = std::make_shared<ADVPowerPlants>();
std::vector<std::shared_ptr<Structure>> StructureArray={
barracks,
oreRefinery,
powerPlant,
warFactory,
advPowerPlant,
};
};

#endif // PRACTICALTOOLSFORSIMPLEDESIGN_UI_HPP
3 changes: 1 addition & 2 deletions include/UIScriptProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
#include <chrono>
#include "Util/GameObject.hpp"


class UIScriptProcess{
private:
bool b_Baracks,b_OreRefinery,b_PowerPlants,b_WarFactory,b_ADVPowerPlant= false;
float Counter=0.F,TargetTime=0.F;
bool b_STALL=false;
std::queue<std::shared_ptr<Structure>> buildQueue;
std::shared_ptr<Structure> temp_PTR;
Util::GameObject m_debug;
Util::GameObject m_debug1;
public:
UIScriptProcess(){};
//
Expand Down
66 changes: 22 additions & 44 deletions src/Structure/Barracks.cpp
Original file line number Diff line number Diff line change
@@ -1,53 +1,25 @@
#include "Structure/Barracks.hpp"

void Barracks::Start() {
this->SetWayPointLocation({this->GetObjectLocation().x + 50,
this->GetObjectLocation().y + 50}); // 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"));
// Set ZIndex
this->SetZIndex(DEFAULT_ZINDEX);
m_wayPoint->SetZIndex(DEFAULT_ZINDEX);
m_HighLight.SetZIndex(DEFAULT_ZINDEX);
// Set Attachment Location &
m_HighLight.SetZIndex(DEFAULT_ZINDEX-1);
// Set Attachment Scale &
// 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())}));
m_Grid.SetActivate(false);
onSelected(false);
// State
SetCurrentUpdateMode(Structure::updateMode::Moveable);
}
void Barracks::updateFixed() {
m_Grid.setLine(GetObjectLocation(), GetWayPointLocation());
Util::Transform Trans;
m_Grid.DrawUsingCamera(Trans, DEFAULT_ZINDEX);

// Attachment and self readjust location and draw---------------
m_wayPoint->SetObjectLocation(this->GetWayPointLocation());
m_HighLight.SetObjectLocation({GetObjectLocation().x + 0.5 * CELL_SIZE.x,
GetObjectLocation().y + 0.5 * CELL_SIZE.y});
this->Draw();
m_wayPoint->Draw();
m_HighLight.Draw();

// Script when select--------------------
if (Util::Input::IsKeyDown(Util::Keycode::T)) {
b_select = !b_select;
}
if (b_select) {
onSelected(true);
} else {
onSelected(false);
}
}
void Barracks::updateMoveable() {
glm::vec2 location = Util::Input::GetCursorPosition();
location = MapClass::ScreenToGlobalCoord(location);
Expand All @@ -58,25 +30,22 @@ void Barracks::updateMoveable() {
this->Draw();
if (Util::Input::IsKeyPressed(Util::Keycode::MOUSE_LB)) {
this->SetObjectLocation(location);

m_HighLight.SetObjectLocation(
{GetObjectLocation().x + 0.5 * CELL_SIZE.x,
GetObjectLocation().y + 0.5 * CELL_SIZE.y});

this->SetWayPointLocation(
{this->GetObjectLocation().x + 48 - 0.5 * CELL_SIZE.x,
this->GetObjectLocation().y + 48 - 0.5 * CELL_SIZE.y});
this->SetWayPointLocation({GetDrawLocation().x+CELL_SIZE.x,GetDrawLocation().y+CELL_SIZE.y});
onSelected(false);
this->SetCurrentUpdateMode(updateMode::Fixed);
}
}

void Barracks::onSelected(bool selected) {
if(b_selectingNewWayPoint){
this->SetWayPointLocation(MapClass::ScreenToGlobalCoord(Util::Input::GetCursorPosition()));
b_selectingNewWayPoint= false;
}
attachmentUpdate();
this->SetAttachVisible(selected);
if (selected) {
if (Util::Input::IsKeyDown(Util::Keycode::V)) {
this->SetWayPointLocation(
{GetWayPointLocation().x + 5, GetWayPointLocation().y + 5});
if (Util::Input::IsKeyPressed(Util::Keycode::V)){
b_selectingNewWayPoint= true;
}
}
}
Expand All @@ -85,3 +54,12 @@ void Barracks::SetAttachVisible(bool visible) {
m_HighLight.SetVisible(visible);
m_Grid.SetActivate(visible);
}
void Barracks::attachmentUpdate(){
m_HighLight.SetObjectLocation(this->GetDrawLocation());
m_wayPoint->SetObjectLocation(this->GetWayPointLocation());
m_Grid.setLine(GetDrawLocation(), GetWayPointLocation());
m_wayPoint->Draw();
m_HighLight.Draw();
Util::Transform Trans;
m_Grid.DrawUsingCamera(Trans, DEFAULT_ZINDEX);
}
14 changes: 14 additions & 0 deletions src/Structure/MousOverlapTool.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// Created by nudle on 2024/3/5.
//

#include "Structure/MousOverlapTool.h"


bool MousOverlapTool::checkMous(glm::vec2 objPos/*,glm::vec2 objSize*/) {
glm::vec2 mousPos=MapClass::ScreenToGlobalCoord(Util::Input::GetCursorPosition());
//return (mousPos.x<objPos.x+objSize.x) && (mousPos.x>objPos.x-objSize.x) && (mousPos.y>objPos.y-objSize.y) && (mousPos.y<objPos.y-objSize.y);
mousPos = MapClass::GlobalCoordToCellCoord(mousPos);
objPos = MapClass::GlobalCoordToCellCoord(objPos);
return (mousPos==objPos);
}
Loading

0 comments on commit ac4b69c

Please sign in to comment.