Skip to content

Commit

Permalink
run
Browse files Browse the repository at this point in the history
  • Loading branch information
jonylu7 committed Apr 12, 2024
1 parent 01ca3ee commit c9d0f7d
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 86 deletions.
13 changes: 5 additions & 8 deletions include/GameObjectManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ class GameObjectManager {
public:
GameObjectManager() {}
~GameObjectManager() {}
void Start() {
/*
for (auto pair : m_StrcutMap) {
pair.second->Start();
*/
void Start(std::shared_ptr<MapClass> map, std::shared_ptr<Player> player) {
m_Map = map;
m_Player = player;

for (auto pair : m_BuiltStructure) {
pair->Start();
}
Expand Down Expand Up @@ -117,8 +116,6 @@ class GameObjectManager {
return m_BuiltStructure;
}

void importPlayer(std::shared_ptr<Player> player) { m_Player = player; }

void setNewDestination(glm::vec2 destination) {
auto queue = m_wayPointUnit.findPath(destination, destination);
}
Expand All @@ -135,7 +132,7 @@ class GameObjectManager {
std::vector<std::shared_ptr<Structure>> m_BuiltStructure;
std::vector<std::shared_ptr<Avatar>> m_UnitArray;
FindValidPathToDest m_wayPointUnit;
std::shared_ptr<MapClass> m_Map;
std::shared_ptr<MapClass> m_Map = std::make_shared<MapClass>();
std::shared_ptr<Player> m_Player;
std::chrono::high_resolution_clock::time_point m_StartTime;
double m_lastElapsed = 0.F;
Expand Down
14 changes: 8 additions & 6 deletions include/Unit/PathUtility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_PATHUTILITY_HPP
#define PRACTICALTOOLSFORSIMPLEDESIGN_PATHUTILITY_HPP
#include <iostream>
#include "pch.hpp"
#include <iostream>
enum class MoveDirection {
UP,
UP_RIGHT,
Expand All @@ -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
135 changes: 70 additions & 65 deletions src/FindValidPathToDest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,79 +4,83 @@

#include "FindValidPathToDest.hpp"

std::deque<MoveDirection> FindValidPathToDest::findPath(glm::vec2 currentcell, glm::vec2 destinationcell) {
std::deque<MoveDirection>
FindValidPathToDest::findPath(glm::vec2 currentcell,
glm::vec2 destinationcell) {
std::deque<MoveDirection> 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<MoveDirection> 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<MoveDirection> movealong =
moveAlongsideObstacle(whichSideToTouchObstacle, currentcell,
turndir, destinationcell);

std::vector<MoveDirection> 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<MoveDirection>
FindValidPathToDest::moveAlongsideObstacle(Side side, glm::vec2 currentcell,
MoveDirection currentdir,
glm::vec2 destinationcell) {

std::vector<MoveDirection> FindValidPathToDest::moveAlongsideObstacle(Side side, glm::vec2 currentcell,
MoveDirection currentdir,glm::vec2 destinationcell){

//side
// side
//
//return dirque
//check
// return dirque
// check
std::vector<MoveDirection> 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(
Expand Down Expand Up @@ -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<TileClass> upTile = m_Map->getTileByCellPosition(
glm::vec2(currentcell.x, currentcell.y + 1));
std::shared_ptr<TileClass> downTile = m_Map->getTileByCellPosition(
Expand All @@ -177,70 +181,69 @@ bool FindValidPathToDest::isTouchedByObstacle(Side side, glm::vec2 currentcell,
std::shared_ptr<TileClass> downLeftTile = m_Map->getTileByCellPosition(
glm::vec2(currentcell.x - 1, currentcell.y - 1));


std::shared_ptr<TileClass> checkedTile=std::make_shared<TileClass>();
std::shared_ptr<TileClass> checkedTile = std::make_shared<TileClass>();
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;

Expand All @@ -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<MoveDirection> *path) {
bool FindValidPathToDest::findStraightPath(glm::vec2 currentcell,
glm::vec2 destinationcell,
std::vector<MoveDirection> *path) {

while (currentcell != destinationcell) {
MoveDirection followingDir =
Expand All @@ -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;
Expand All @@ -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<MoveDirection> 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;
}


2 changes: 1 addition & 1 deletion src/Scene/DefaultScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 7 additions & 6 deletions src/Unit/PathUtility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -168,4 +169,4 @@ glm::vec2 PathUtility::getNextCellByCurrent(MoveDirection currentdir, glm::vec2
}
}
return currentcell;
}
}

0 comments on commit c9d0f7d

Please sign in to comment.