Skip to content

Commit

Permalink
build structure refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jonylu7 committed Mar 26, 2024
1 parent e088b96 commit ccd8e3b
Show file tree
Hide file tree
Showing 19 changed files with 178 additions and 329 deletions.
6 changes: 2 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,14 @@ set(SRC_FILES

${SRC_DIR}/Structure/Structure.cpp
${SRC_DIR}/Structure/WayPoint.cpp
${SRC_DIR}/Structure/MouseOverlapTool.cpp
${SRC_DIR}/Structure/IWayPointStructure.cpp
${SRC_DIR}/Structure/HighLight.cpp
${SRC_DIR}/UI/UIScriptProcess.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}/Structure/WayPointStructure.cpp

${SRC_DIR}/Unit/PathfindingUnit.cpp

Expand All @@ -107,6 +105,7 @@ set(INCLUDE_FILES
${INCLUDE_DIR}/Core/Texture.hpp
${INCLUDE_DIR}/Core/TextureUtils.hpp
${INCLUDE_DIR}/Core/Drawable.hpp
${INCLUDE_DIR}/Structure/WayPointStructure.hpp


${INCLUDE_DIR}/Util/LoadTextFile.hpp
Expand Down Expand Up @@ -147,7 +146,6 @@ set(INCLUDE_FILES

${INCLUDE_DIR}/Structure/Structure.hpp
${INCLUDE_DIR}/Structure/WayPoint.hpp
${INCLUDE_DIR}/Structure/MouseOverlapTool.h
${INCLUDE_DIR}/Structure/IWayPointStructure.hpp
${INCLUDE_DIR}/HighLight.h
${INCLUDE_DIR}/House.hpp
Expand Down
30 changes: 26 additions & 4 deletions include/GameObjectManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,35 @@ class GameObjectManager {
pair->Start();
}
}
glm::vec2 start;
glm::vec2 end;
void Update() {
for (auto pair : m_BuiltStructure) {
pair->Update();
}
for (auto unit : m_UnitArray){
for (auto unit : m_UnitArray) {
unit->Update();
printf("(GOM) update back success\n");
}

CursorSelect(&start, &end);
}

void CursorSelect(glm::vec2 *start, glm::vec2 *end) {
if (Util::Input::IsKeyDown(Util::Keycode::MOUSE_LB)) {
*start = Util::Input::GetCursorPosition();
}
if (Util::Input::IsKeyPressed(Util::Keycode::MOUSE_LB)) {
*end = Util::Input::GetCursorPosition();
}
MapUtil::ScreenToGlobalCoord(*start);
}

static bool ifObjectClicked(glm::vec2 objpos, glm::vec2 objsize,
glm::vec2 mousestart, glm::vec2 mouseend) {
mousestart = MapUtil::GlobalCoordToCellCoord(mousestart);
mouseend = MapUtil::GlobalCoordToCellCoord(mouseend);
objpos = MapUtil::GlobalCoordToCellCoord(objpos);
return (mousestart == objpos);
}

void Append(std::shared_ptr<Structure> newstruct) {
Expand All @@ -56,14 +77,15 @@ class GameObjectManager {
return totalPower;
}

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

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;

};

#endif // PRACTICALTOOLSFORSIMPLEDESIGN_GAMEOBJECTMANAGER_HPP
3 changes: 1 addition & 2 deletions include/Scene/DefaultScene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "Structure/AdvencePowerPlants.hpp"
#include "Structure/Barracks.hpp"
#include "Structure/IWayPointStructure.hpp"
#include "Structure/MouseOverlapTool.h"
#include "Structure/OreRefinery.hpp"
#include "Structure/PowerPlants.hpp"
#include "Structure/WarFactory.hpp"
Expand Down Expand Up @@ -49,7 +48,7 @@ class DefaultScene {
Util::Renderer m_Renderer;
UIClass m_UI;
std::shared_ptr<MapClass> m_Map = std::make_shared<MapClass>();
std::shared_ptr<Player> m_Player=std::make_shared<Player>();
std::shared_ptr<Player> m_Player = std::make_shared<Player>();
GameObjectManager m_Manager;

Avatar m_dummy;
Expand Down
16 changes: 16 additions & 0 deletions include/Selectable.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// Created by 盧威任 on 3/26/24.
//

#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_SELECTABLE_HPP
#define PRACTICALTOOLSFORSIMPLEDESIGN_SELECTABLE_HPP
class Selectable {
public:
virtual void onSelected() = 0;
bool getSelected() { return b_Selected; }
void setSelected(bool value) { b_Selected = value; }

private:
bool b_Selected;
};
#endif // PRACTICALTOOLSFORSIMPLEDESIGN_SELECTABLE_HPP
33 changes: 5 additions & 28 deletions include/Structure/Barracks.hpp
Original file line number Diff line number Diff line change
@@ -1,40 +1,17 @@
#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_STRUCTURE_BARRACKS_HPP
#define PRACTICALTOOLSFORSIMPLEDESIGN_STRUCTURE_BARRACKS_HPP

#include "Grid.hpp"
#include "HighLight.h"
#include "Line.hpp"
#include "Map.hpp"
#include "Structure/IWayPointStructure.hpp"
#include "Structure/MouseOverlapTool.h"
#include "Structure/Structure.hpp"
#include "Util/Image.hpp"
#include "Util/Input.hpp"
#include "Util/Keycode.hpp"
#include "WayPoint.hpp"

class Barracks : 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;
#include "WayPointStructure.hpp"

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

: Structure(electricPower, buildingTime, buildingCost, buildingHp,
GameObjectID(unitType::BARRACKS, house)){};
: WayPointStructure(electricPower, buildingTime, buildingCost,
buildingHp,
GameObjectID(unitType::BARRACKS, house)){};
void Start() override;

virtual void onSelected(bool selected) override;
virtual void SetAttachVisible(bool visible) override;

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

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

public:
virtual glm::vec2 GetWayPointLocation() { return this->waypointLocation; };
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};
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: 0 additions & 23 deletions include/Structure/MouseOverlapTool.h

This file was deleted.

32 changes: 5 additions & 27 deletions include/Structure/OreRefinery.hpp
Original file line number Diff line number Diff line change
@@ -1,37 +1,15 @@
#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_STRUCTURE_OreRefinery_HPP
#define PRACTICALTOOLSFORSIMPLEDESIGN_STRUCTURE_OreRefinery_HPP

#include "Grid.hpp"
#include "HighLight.h"
#include "Line.hpp"
#include "Structure/IWayPointStructure.hpp"
#include "Structure/Structure.hpp"
#include "Util/Image.hpp"
#include "Util/Input.hpp"
#include "Util/Keycode.hpp"
#include "WayPoint.hpp"

class OreRefinery : 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;
#include "WayPointStructure.hpp"
class OreRefinery : public WayPointStructure {

public:
OreRefinery(float electricPower = -30.F, float buildingTime = 100.F,
float buildingCost = 2000.F, float buildingHp = 900.F,
HouseType house = HouseType::NONE)
: Structure(electricPower, buildingTime, buildingCost, buildingHp,
GameObjectID(unitType::ORE_REF, house)){};
: WayPointStructure(electricPower, buildingTime, buildingCost,
buildingHp,
GameObjectID(unitType::ORE_REF, house)){};
void Start() override;

virtual void onSelected(bool selected) override;
virtual void SetAttachVisible(bool visible) override;

virtual void updateMoveable() override;
virtual void attachmentUpdate() override;
};
#endif
10 changes: 5 additions & 5 deletions include/Structure/Structure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
#include "GameObjectID.hpp"
#include "HighLight.h"
#include "Map.hpp"
#include "Structure/MouseOverlapTool.h"
#include "Selectable.hpp"
#include "Util/GameObject.hpp"
#include "Util/Image.hpp"
#include "Util/Input.hpp"
#include "Util/TransformUtils.hpp"
#include "glm/glm.hpp"
#define DEFAULT_ZINDEX 15

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

public:
enum class updateMode { Invisidable, Moveable, Fixed };
Expand Down Expand Up @@ -57,9 +57,10 @@ class Structure : public Util::GameObject {
void SetID(GameObjectID id) { m_ID = id; };

static glm::vec2 ChangeToCell(glm::vec2 location);
virtual void onSelected(bool selected);
void onSelected() override;
virtual void attachmentUpdate(); // this function now will update
// attachment's location and draw as well
// attachment's location and draw as
// well
bool getBuilt() {
if (m_CurrentState == updateMode::Fixed) {
return true;
Expand Down Expand Up @@ -98,7 +99,6 @@ class Structure : public Util::GameObject {
GameObjectID m_ID;

protected:
bool b_selected = false;
bool b_selectingNewWayPoint = false;
};

Expand Down
31 changes: 5 additions & 26 deletions include/Structure/WarFactory.hpp
Original file line number Diff line number Diff line change
@@ -1,38 +1,17 @@
#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_STRUCTURE_WarFactory_HPP
#define PRACTICALTOOLSFORSIMPLEDESIGN_STRUCTURE_WarFactory_HPP

#include "GameObjectID.hpp"
#include "Grid.hpp"
#include "HighLight.h"
#include "Line.hpp"
#include "Structure/IWayPointStructure.hpp"
#include "Structure/Structure.hpp"
#include "Util/Image.hpp"
#include "Util/Input.hpp"
#include "Util/Keycode.hpp"
#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;
#include "WayPointStructure.hpp"
class WarFactory : public WayPointStructure {

public:
WarFactory(float electricPower = -30.F, float buildingTime = 100.F,
float buildingCost = 2000.F, float buildingHp = 1000.F,
HouseType house = HouseType::NONE)
: Structure(electricPower, buildingTime, buildingCost, buildingHp,
GameObjectID(unitType::WAR_FACT, house)){};
: WayPointStructure(electricPower, buildingTime, buildingCost,
buildingHp,
GameObjectID(unitType::WAR_FACT, house)){};
void Start() override;

virtual void onSelected(bool selected) override;
virtual void SetAttachVisible(bool visible) override;

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

#endif
41 changes: 41 additions & 0 deletions include/Structure/WayPointStructure.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// Created by 盧威任 on 3/26/24.
//

#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_MOVEABLESTRUCTURE_HPP
#define PRACTICALTOOLSFORSIMPLEDESIGN_MOVEABLESTRUCTURE_HPP
#include "Grid.hpp"
#include "HighLight.h"
#include "Line.hpp"
#include "Structure/IWayPointStructure.hpp"
#include "Structure/Structure.hpp"
#include "Util/Image.hpp"
#include "Util/Input.hpp"
#include "Util/Keycode.hpp"
#include "WayPoint.hpp"

class WayPointStructure : public Structure, public IWayPointStructure {
protected:
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;

public:
WayPointStructure(float electricPower = -10.F, float buildingTime = 10.F,
float buildingCost = 20.F, float buildingHp = 90.F,
GameObjectID id = GameObjectID(unitType::null,
HouseType::NONE))
: Structure(electricPower, buildingTime, buildingCost, buildingHp,
id){};
virtual ~WayPointStructure(){};

virtual void onSelected() override;
virtual void SetAttachVisible(bool visible) override;

virtual void updateMoveable() override;
virtual void attachmentUpdate() override;
};
#endif // PRACTICALTOOLSFORSIMPLEDESIGN_MOVEABLESTRUCTURE_HPP
Loading

0 comments on commit ccd8e3b

Please sign in to comment.