Skip to content

Commit

Permalink
split gameobject manager
Browse files Browse the repository at this point in the history
  • Loading branch information
jonylu7 committed Apr 25, 2024
1 parent 1f75572 commit 81dd62e
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 62 deletions.
2 changes: 2 additions & 0 deletions include/Map/Map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

class MapClass : public Core::Drawable {
public:
MapClass() {}
~MapClass() {}
void Init(unsigned int width, unsigned int height);
void Init(std::vector<std::vector<std::shared_ptr<TileClass>>> map,
unsigned int width, unsigned int height);
Expand Down
10 changes: 5 additions & 5 deletions include/Mechanics/Constructing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
#include "Structure/Structure.hpp"
class Constructing : public StructureArray {
public:
Constructing(std::shared_ptr<MapClass> map)
: StructureArray(map) {}
Constructing() {}
virtual ~Constructing() {}
void
AddStructSelectingConstructionSite(std::shared_ptr<Structure> newstruct) {
Expand All @@ -18,16 +17,17 @@ class Constructing : public StructureArray {
m_IsSelectingConstructionSite = true;
}

void SelectdConstructionSite() {
void SelectdConstructionSite(std::shared_ptr<MapClass> m_Map) {
if (m_IsSelectingConstructionSite) {
if (m_StructSelectingConstructionSite->getConstructed()) {
Append(m_StructSelectingConstructionSite);
Append(m_Map, m_StructSelectingConstructionSite);
m_IsSelectingConstructionSite = false;
}
m_StructSelectingConstructionSite->Update();
}
}
void SetOccupiedAreaUnbuildable(std::shared_ptr<Structure> structure) {
void SetOccupiedAreaUnbuildable(std::shared_ptr<MapClass> m_Map,
std::shared_ptr<Structure> structure) {
for (auto i : structure->GetAbsoluteOccupiedArea()) {
m_Map->getTileByCellPosition(i)->setBuildable(false);
m_Map->getTileByCellPosition(i)->setWalkable(false);
Expand Down
64 changes: 64 additions & 0 deletions include/Mechanics/CursorSelection.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// Created by 盧威任 on 4/24/24.
//

#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_CURSORSELECTION_HPP
#define PRACTICALTOOLSFORSIMPLEDESIGN_CURSORSELECTION_HPP
#include "Map/Map.hpp"
#include "Map/MapUtility.hpp"
#include "Selectable.hpp"
#include "pch.hpp"
class CursorSelection {
public:
CursorSelection() {}
virtual ~CursorSelection() {}
void CursorSelect(std::shared_ptr<MapClass> m_Map, 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)) {
// clear up last selected
for (auto i : lastSeletctedObjects) {
i->setSelected(false);
}
lastSeletctedObjects.clear();

// get cursor position
*end = Util::Input::GetCursorPosition();

// select objects
auto startcell = MapUtil::GlobalCoordToCellCoord(
MapUtil::ScreenToGlobalCoord(*start));
auto endcell = MapUtil::GlobalCoordToCellCoord(
MapUtil::ScreenToGlobalCoord(*end));

int max_x_cell = std::max(startcell.x, endcell.x);
int max_y_cell = std::max(startcell.y, endcell.y);
int min_x_cell = std::min(startcell.x, endcell.x);
int min_y_cell = std::min(startcell.y, endcell.y);

for (int i = min_y_cell; i <= max_y_cell; i++) {
for (int j = min_x_cell; j <= max_x_cell; j++) {
auto objects = m_Map->getTileByCellPosition(glm::vec2(j, i))
->getSelectableObjects();
for (auto i : objects) {
if (i->getSelected() == false) {
i->setSelected(true);
lastSeletctedObjects.push_back(i);
}
}
}
}
}
}

protected:
std::vector<std::shared_ptr<Selectable>> lastSeletctedObjects;
glm::vec2 cursorstart;
glm::vec2 cursorend;
};

;
#endif // PRACTICALTOOLSFORSIMPLEDESIGN_CURSORSELECTION_HPP
60 changes: 7 additions & 53 deletions include/Mechanics/GameObjectManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "FindValidPathToDest.hpp"
#include "GameObjectID.hpp"
#include "Mechanics/Constructing.hpp"
#include "Mechanics/CursorSelection.hpp"
#include "Mechanics/StructureArray.hpp"
#include "Player.hpp"
#include "Structure/AdvencePowerPlants.hpp"
Expand All @@ -21,10 +22,9 @@
#include <unordered_map>
#include <utility>

class GameObjectManager : public Constructing {
class GameObjectManager : public Constructing, public CursorSelection {
public:
GameObjectManager()
: Constructing(m_Map) {}
GameObjectManager() {}
~GameObjectManager() {}
void Start(std::shared_ptr<MapClass> map, std::shared_ptr<Player> player,
std::shared_ptr<CursorClass> cursor) {
Expand All @@ -42,13 +42,13 @@ class GameObjectManager : public Constructing {

for (auto pair : m_BuiltStructure) {
pair->Update();
SetOccupiedAreaUnbuildable(pair);
SetOccupiedAreaUnbuildable(m_Map, pair);
}
for (auto unit : m_UnitArray) {
unit->Update();
}

CursorSelect(&cursorstart, &cursorend);
CursorSelect(m_Map, &cursorstart, &cursorend);

// currency update
std::chrono::high_resolution_clock::time_point m_currentTime =
Expand All @@ -57,52 +57,11 @@ class GameObjectManager : public Constructing {
if (elapsed.count() - m_lastElapsed >= 1) { // update every second
m_lastElapsed = elapsed.count();
}
SelectdConstructionSite();
SelectdConstructionSite(m_Map);
}

// Select Unit to take action

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)) {
// clear up last selected
for (auto i : lastSeletctedObjects) {
i->setSelected(false);
}
lastSeletctedObjects.clear();

// get cursor position
*end = Util::Input::GetCursorPosition();

// select objects
auto startcell = MapUtil::GlobalCoordToCellCoord(
MapUtil::ScreenToGlobalCoord(*start));
auto endcell = MapUtil::GlobalCoordToCellCoord(
MapUtil::ScreenToGlobalCoord(*end));

int max_x_cell = std::max(startcell.x, endcell.x);
int max_y_cell = std::max(startcell.y, endcell.y);
int min_x_cell = std::min(startcell.x, endcell.x);
int min_y_cell = std::min(startcell.y, endcell.y);

for (int i = min_y_cell; i <= max_y_cell; i++) {
for (int j = min_x_cell; j <= max_x_cell; j++) {
auto objects = m_Map->getTileByCellPosition(glm::vec2(j, i))
->getSelectableObjects();
for (auto i : objects) {
if (i->getSelected() == false) {
i->setSelected(true);
lastSeletctedObjects.push_back(i);
}
}
}
}
}
}

void Append(std::shared_ptr<Avatar> newUnit) {
m_UnitArray.push_back(newUnit);
}
Expand Down Expand Up @@ -135,17 +94,12 @@ class GameObjectManager : public Constructing {
}

private:
std::vector<std::shared_ptr<Selectable>> lastSeletctedObjects;

std::vector<std::shared_ptr<Avatar>> m_UnitArray;
FindValidPathToDest m_wayPointUnit;
// std::shared_ptr<MapClass> m_Map = std::make_shared<MapClass>();
std::shared_ptr<MapClass> m_Map = std::make_shared<MapClass>();
std::shared_ptr<Player> m_Player;
std::shared_ptr<CursorClass> m_Cursor;
std::chrono::high_resolution_clock::time_point m_StartTime;
double m_lastElapsed = 0.F;
glm::vec2 cursorstart;
glm::vec2 cursorend;
};

#endif // PRACTICALTOOLSFORSIMPLEDESIGN_GAMEOBJECTMANAGER_HPP
7 changes: 3 additions & 4 deletions include/Mechanics/StructureArray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
#include "pch.hpp"
class StructureArray {
public:
StructureArray(std::shared_ptr<MapClass> map)
: m_Map(map) {}
StructureArray() {}
virtual ~StructureArray() {}

void Append(std::shared_ptr<Structure> newstruct) {
void Append(std::shared_ptr<MapClass> m_Map,
std::shared_ptr<Structure> newstruct) {
m_BuiltStructure.push_back(newstruct);
m_Map->AppendSelectableObjectByCellPosition(
MapUtil::GlobalCoordToCellCoord(newstruct->GetObjectLocation()),
Expand All @@ -21,6 +21,5 @@ class StructureArray {

protected:
std::vector<std::shared_ptr<Structure>> m_BuiltStructure;
std::shared_ptr<MapClass> m_Map = std::make_shared<MapClass>();
};
#endif // PRACTICALTOOLSFORSIMPLEDESIGN_STRUCTUREARRAY_HPP

0 comments on commit 81dd62e

Please sign in to comment.