diff --git a/include/GameObjectManager.hpp b/include/GameObjectManager.hpp index 4258fc34..6447782c 100644 --- a/include/GameObjectManager.hpp +++ b/include/GameObjectManager.hpp @@ -22,11 +22,10 @@ class GameObjectManager { public: GameObjectManager() {} ~GameObjectManager() {} - void Start() { - /* - for (auto pair : m_StrcutMap) { - pair.second->Start(); - */ + void Start(std::shared_ptr map, std::shared_ptr player) { + m_Map = map; + m_Player = player; + for (auto pair : m_BuiltStructure) { pair->Start(); } @@ -117,8 +116,6 @@ class GameObjectManager { return m_BuiltStructure; } - void importPlayer(std::shared_ptr player) { m_Player = player; } - void setNewDestination(glm::vec2 destination) { auto queue = m_wayPointUnit.findPath(destination, destination); } @@ -135,7 +132,7 @@ class GameObjectManager { std::vector> m_BuiltStructure; std::vector> m_UnitArray; FindValidPathToDest m_wayPointUnit; - std::shared_ptr m_Map; + std::shared_ptr m_Map = std::make_shared(); std::shared_ptr m_Player; std::chrono::high_resolution_clock::time_point m_StartTime; double m_lastElapsed = 0.F; diff --git a/include/Unit/PathUtility.hpp b/include/Unit/PathUtility.hpp index 2756ec0e..982ba691 100644 --- a/include/Unit/PathUtility.hpp +++ b/include/Unit/PathUtility.hpp @@ -4,8 +4,8 @@ #ifndef PRACTICALTOOLSFORSIMPLEDESIGN_PATHUTILITY_HPP #define PRACTICALTOOLSFORSIMPLEDESIGN_PATHUTILITY_HPP -#include #include "pch.hpp" +#include enum class MoveDirection { UP, UP_RIGHT, @@ -26,11 +26,13 @@ class PathUtility { static MoveDirection getDirByRelativeCells(glm::vec2 currentcell, glm::vec2 destinationcell); - static MoveDirection findNewDirWhenNotTouchedByObstacle(Side side, glm::vec2 currentcell, MoveDirection currentdir); - static bool isTouchedByObstacle(Side side, glm::vec2 currentcell, MoveDirection currentdir); - static MoveDirection findNewDirWhenCrash(Side side, glm::vec2 currentcell, MoveDirection currentdir); - - + static MoveDirection + findNewDirWhenNotTouchedByObstacle(Side side, glm::vec2 currentcell, + MoveDirection currentdir); + static bool isTouchedByObstacle(Side side, glm::vec2 currentcell, + MoveDirection currentdir); + static MoveDirection findNewDirWhenCrash(Side side, glm::vec2 currentcell, + MoveDirection currentdir); }; #endif // PRACTICALTOOLSFORSIMPLEDESIGN_PATHUTILITY_HPP diff --git a/src/FindValidPathToDest.cpp b/src/FindValidPathToDest.cpp index 79e27b8d..9550048f 100644 --- a/src/FindValidPathToDest.cpp +++ b/src/FindValidPathToDest.cpp @@ -4,79 +4,83 @@ #include "FindValidPathToDest.hpp" -std::deque FindValidPathToDest::findPath(glm::vec2 currentcell, glm::vec2 destinationcell) { +std::deque +FindValidPathToDest::findPath(glm::vec2 currentcell, + glm::vec2 destinationcell) { std::deque m_dirQue; - if (m_Map->getTileByCellPosition(destinationcell)->getWalkable() == - false) { + if (m_Map->getTileByCellPosition(destinationcell)->getWalkable() == false) { return m_dirQue; } Side whichSideToTouchObstacle = randomlyChooseSide(); - while (currentcell != destinationcell) { std::vector newDirque; bool arrived = findStraightPath(currentcell, destinationcell, &newDirque); - for(auto i:newDirque){ + for (auto i : newDirque) { m_dirQue.push_back(i); - currentcell= PathUtility::getNextCellByCurrent(i,currentcell); + currentcell = PathUtility::getNextCellByCurrent(i, currentcell); } - //push newdirque into dirque and update current cell and current dir - + // push newdirque into dirque and update current cell and current dir if (arrived) { break; - }else{ - auto facingDir=PathUtility::getDirByRelativeCells(currentcell, destinationcell); - //turn + } else { + auto facingDir = PathUtility::getDirByRelativeCells( + currentcell, destinationcell); + // turn MoveDirection turndir; - if(m_Map->getWalkable(PathUtility::getNextCellByCurrent(facingDir,currentcell))==false){ - turndir=findNewDirWhenCrash(whichSideToTouchObstacle,currentcell,facingDir); + if (m_Map->getWalkable(PathUtility::getNextCellByCurrent( + facingDir, currentcell)) == false) { + turndir = findNewDirWhenCrash(whichSideToTouchObstacle, + currentcell, facingDir); }; + std::vector movealong = + moveAlongsideObstacle(whichSideToTouchObstacle, currentcell, + turndir, destinationcell); - std::vector movealong=moveAlongsideObstacle(whichSideToTouchObstacle,currentcell,turndir,destinationcell); - - for(auto i:movealong){ + for (auto i : movealong) { m_dirQue.push_back(i); - currentcell= PathUtility::getNextCellByCurrent(i,currentcell); + currentcell = PathUtility::getNextCellByCurrent(i, currentcell); } - } } } +std::vector +FindValidPathToDest::moveAlongsideObstacle(Side side, glm::vec2 currentcell, + MoveDirection currentdir, + glm::vec2 destinationcell) { -std::vector FindValidPathToDest::moveAlongsideObstacle(Side side, glm::vec2 currentcell, - MoveDirection currentdir,glm::vec2 destinationcell){ - - //side + // side // - //return dirque - //check + // return dirque + // check std::vector path; - while(!canResumeWalkingStraight(currentcell,destinationcell)){ + while (!canResumeWalkingStraight(currentcell, destinationcell)) { - //if next is not touched by obstacle, turn - if(!isTouchedByObstacle(side,currentcell,currentdir)){ - currentdir= PathUtility::findNewDirWhenNotTouchedByObstacle(side,currentcell,currentdir); + // if next is not touched by obstacle, turn + if (!isTouchedByObstacle(side, currentcell, currentdir)) { + currentdir = PathUtility::findNewDirWhenNotTouchedByObstacle( + side, currentcell, currentdir); } - //if next to be obstacle, turn findNewDirWhenCrash + // if next to be obstacle, turn findNewDirWhenCrash - //walk along + // walk along path.push_back(currentdir); - currentcell= PathUtility::getNextCellByCurrent(currentdir,currentcell); - - + currentcell = + PathUtility::getNextCellByCurrent(currentdir, currentcell); } return path; - } -MoveDirection FindValidPathToDest::findNewDirWhenCrash(Side side, glm::vec2 currentcell, MoveDirection currentdir) { +MoveDirection +FindValidPathToDest::findNewDirWhenCrash(Side side, glm::vec2 currentcell, + MoveDirection currentdir) { MoveDirection newdir = currentdir; while (m_Map ->getTileByCellPosition( @@ -156,9 +160,9 @@ MoveDirection FindValidPathToDest::findNewDirWhenCrash(Side side, glm::vec2 curr return newdir; } - -bool FindValidPathToDest::isTouchedByObstacle(Side side, glm::vec2 currentcell, MoveDirection currentdir) { - //abs +bool FindValidPathToDest::isTouchedByObstacle(Side side, glm::vec2 currentcell, + MoveDirection currentdir) { + // abs std::shared_ptr upTile = m_Map->getTileByCellPosition( glm::vec2(currentcell.x, currentcell.y + 1)); std::shared_ptr downTile = m_Map->getTileByCellPosition( @@ -177,70 +181,69 @@ bool FindValidPathToDest::isTouchedByObstacle(Side side, glm::vec2 currentcell, std::shared_ptr downLeftTile = m_Map->getTileByCellPosition( glm::vec2(currentcell.x - 1, currentcell.y - 1)); - - std::shared_ptr checkedTile=std::make_shared(); + std::shared_ptr checkedTile = std::make_shared(); switch (currentdir) { case MoveDirection::RIGHT: if (side == Side::R) { - checkedTile=downTile; + checkedTile = downTile; } else if (side == Side::L) { - checkedTile=upTile; + checkedTile = upTile; } break; case MoveDirection::LEFT: if (side == Side::R) { - checkedTile=upTile; + checkedTile = upTile; } else if (side == Side::L) { - checkedTile=downTile; + checkedTile = downTile; } break; case MoveDirection::UP: if (side == Side::R) { - checkedTile=rightTile; + checkedTile = rightTile; } else if (side == Side::L) { - checkedTile=leftTile; + checkedTile = leftTile; } break; case MoveDirection::DOWN: if (side == Side::R) { - checkedTile=leftTile; + checkedTile = leftTile; } else if (side == Side::L) { - checkedTile=rightTile; + checkedTile = rightTile; } break; case MoveDirection::UP_RIGHT: if (side == Side::R) { - checkedTile=rightTile; + checkedTile = rightTile; } else if (side == Side::L) { - checkedTile=upTile; + checkedTile = upTile; } break; case MoveDirection::DOWN_LEFT: if (side == Side::R) { - checkedTile=leftTile; + checkedTile = leftTile; } else if (side == Side::L) { - checkedTile=downTile; + checkedTile = downTile; } break; case MoveDirection::DOWN_RIGHT: if (side == Side::R) { - checkedTile=downTile; + checkedTile = downTile; } else if (side == Side::L) { - checkedTile=rightTile; + checkedTile = rightTile; } break; case MoveDirection::UP_LEFT: if (side == Side::R) { - checkedTile=upTile; + checkedTile = upTile; } else if (side == Side::L) { - checkedTile=leftTile; + checkedTile = leftTile; } break; @@ -249,14 +252,16 @@ bool FindValidPathToDest::isTouchedByObstacle(Side side, glm::vec2 currentcell, break; } - if(checkedTile->getWalkable()== true){ + if (checkedTile->getWalkable() == true) { return false; - }else{ + } else { return true; } } -bool FindValidPathToDest::findStraightPath(glm::vec2 currentcell, glm::vec2 destinationcell, std::vector *path) { +bool FindValidPathToDest::findStraightPath(glm::vec2 currentcell, + glm::vec2 destinationcell, + std::vector *path) { while (currentcell != destinationcell) { MoveDirection followingDir = @@ -265,7 +270,8 @@ bool FindValidPathToDest::findStraightPath(glm::vec2 currentcell, glm::vec2 dest if (m_Map->getWalkable( PathUtility::getNextCellByCurrent(followingDir, currentcell))) { path->push_back(followingDir); - currentcell = PathUtility::getNextCellByCurrent(followingDir, currentcell); + currentcell = + PathUtility::getNextCellByCurrent(followingDir, currentcell); } else { // if meet obstacle, stop in front of it, but unknown dir. return false; @@ -274,13 +280,12 @@ bool FindValidPathToDest::findStraightPath(glm::vec2 currentcell, glm::vec2 dest return true; } -bool FindValidPathToDest::canResumeWalkingStraight(glm::vec2 currentcell, glm::vec2 destinationcell){ +bool FindValidPathToDest::canResumeWalkingStraight(glm::vec2 currentcell, + glm::vec2 destinationcell) { std::vector path; - auto arrived=findStraightPath(currentcell,destinationcell,&path); - if(arrived || !path.empty()){ + auto arrived = findStraightPath(currentcell, destinationcell, &path); + if (arrived || !path.empty()) { return true; } return false; } - - diff --git a/src/Scene/DefaultScene.cpp b/src/Scene/DefaultScene.cpp index dfcf3ac1..680ef63e 100644 --- a/src/Scene/DefaultScene.cpp +++ b/src/Scene/DefaultScene.cpp @@ -25,7 +25,7 @@ void DefaultScene::Start() { // m_GameObjectManager.Start(); // m_dummy.Start({5, 5}, m_Map); - m_GameObjectManager->importPlayer(m_Player); + m_GameObjectManager->Start(m_Map, m_Player); m_UI.importMap(m_Map); m_UI.importPlayer(m_Player); m_UI.importGameObjManager(m_GameObjectManager); diff --git a/src/Unit/PathUtility.cpp b/src/Unit/PathUtility.cpp index a3c3ae20..25eac07e 100644 --- a/src/Unit/PathUtility.cpp +++ b/src/Unit/PathUtility.cpp @@ -3,8 +3,8 @@ // #include "Unit/PathUtility.hpp" - -MoveDirection PathUtility::findNewDirWhenNotTouchedByObstacle(Side side, glm::vec2 currentcell, MoveDirection currentdir) { +MoveDirection PathUtility::findNewDirWhenNotTouchedByObstacle( + Side side, glm::vec2 currentcell, MoveDirection currentdir) { MoveDirection newdir; switch (currentdir) { case MoveDirection::RIGHT: @@ -74,12 +74,12 @@ MoveDirection PathUtility::findNewDirWhenNotTouchedByObstacle(Side side, glm::ve case MoveDirection::IDLE: // printf("Direction debug didn't move\n"); break; - } return newdir; } -MoveDirection PathUtility::getDirByRelativeCells(glm::vec2 currentcell, glm::vec2 destinationcell) { +MoveDirection PathUtility::getDirByRelativeCells(glm::vec2 currentcell, + glm::vec2 destinationcell) { MoveDirection direction; int destinationCellX = destinationcell.x; int destinationCellY = destinationcell.y; @@ -128,7 +128,8 @@ MoveDirection PathUtility::getDirByRelativeCells(glm::vec2 currentcell, glm::vec return direction; } -glm::vec2 PathUtility::getNextCellByCurrent(MoveDirection currentdir, glm::vec2 currentcell) { +glm::vec2 PathUtility::getNextCellByCurrent(MoveDirection currentdir, + glm::vec2 currentcell) { switch (currentdir) { case MoveDirection::RIGHT: { currentcell = {currentcell.x + 1, currentcell.y}; @@ -168,4 +169,4 @@ glm::vec2 PathUtility::getNextCellByCurrent(MoveDirection currentdir, glm::vec2 } } return currentcell; -} \ No newline at end of file +}