Last active
October 14, 2018 12:45
-
-
Save BenjaminYde/2666706018982b6de5b0dadf0cf31444 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "MiniginPCH.h" | |
#include "NodeManager.h" | |
#include "SpriteComponent.h" | |
#include "Pill.h" | |
NodeManager::NodeManager(vector<bool> solidblocks) | |
{ | |
int currentIndex = 0; | |
int indexToCheck = 0; | |
int rowMax = 27; | |
int colMax = 21; | |
float nodeWidth = 18; | |
Vector2 currPos; | |
// get obstacles | |
for (auto row = 0; row < rowMax; ++row) | |
for (auto col = 0; col < colMax; ++col) | |
{ | |
currPos = Vector2((col)* nodeWidth, row * nodeWidth); | |
currentIndex = row * (colMax)+col; | |
if (!solidblocks.at(currentIndex)) | |
m_NoObstacles.push_back(new Node(currPos)); | |
} | |
int counter = 0; | |
for(auto row = 0; row < rowMax ; ++row) | |
{ | |
for(auto col = 0; col < colMax ; ++col) | |
{ | |
currentIndex = row * (colMax)+col; | |
if (currentIndex == 490) | |
currentIndex = currentIndex; | |
if(!solidblocks.at(currentIndex)) | |
{ | |
currPos = Vector2((col)* nodeWidth, row * nodeWidth); | |
// check up | |
indexToCheck = currentIndex - colMax; | |
if (indexToCheck >= 0 && indexToCheck <= ((rowMax * colMax) - 1)) | |
if (!solidblocks.at(indexToCheck)) | |
{ | |
for(auto n : m_NoObstacles) | |
{ | |
if(n->m_Pos == Vector2{ currPos.x , currPos.y - nodeWidth }) | |
{ | |
m_NoObstacles.at(counter)->m_Connections.push_back(n); | |
break; | |
} | |
} | |
} | |
// check down | |
indexToCheck = currentIndex + colMax; | |
if (indexToCheck >= 0 && indexToCheck <= ((rowMax * colMax) - 1)) | |
if (!solidblocks.at(indexToCheck)) | |
{ | |
for (auto n : m_NoObstacles) | |
{ | |
if (n->m_Pos == Vector2{ currPos.x , currPos.y + nodeWidth }) | |
{ | |
m_NoObstacles.at(counter)->m_Connections.push_back(n); | |
break; | |
} | |
} | |
} | |
// check left | |
indexToCheck = currentIndex - 1; | |
if (indexToCheck >= 0 && indexToCheck <= ((rowMax * colMax) - 1)) | |
if (!solidblocks.at(indexToCheck)) | |
for (auto n : m_NoObstacles) | |
{ | |
if (n->m_Pos == Vector2{ currPos.x - nodeWidth , currPos.y }) | |
{ | |
m_NoObstacles.at(counter)->m_Connections.push_back(n); | |
break; | |
} | |
} | |
// check right | |
indexToCheck = currentIndex + 1; | |
if (indexToCheck >= 0 && indexToCheck <= ((rowMax * colMax) - 1)) | |
if (!solidblocks.at(indexToCheck)) | |
for (auto n : m_NoObstacles) | |
{ | |
if (n->m_Pos == Vector2{ currPos.x + nodeWidth, currPos.y }) | |
{ | |
m_NoObstacles.at(counter)->m_Connections.push_back(n); | |
break; | |
} | |
} | |
// counter | |
++counter; | |
} | |
} | |
} | |
} | |
NodeManager::~NodeManager() | |
{ | |
for (auto node : m_NoObstacles) | |
SafeDelete(node); | |
m_NoObstacles.clear(); | |
} | |
Node * NodeManager::GetNodeFromLocation(Vector2 pos) | |
{ | |
UNREFERENCED_PARAMETER(pos); | |
pos.x += 8; | |
pos.y += 8; | |
int x = int(pos.x / 18); | |
int y = int(pos.y / 18); | |
if (x < 21 && y < 27) | |
return *find_if(m_NoObstacles.begin(), m_NoObstacles.end(), [x, y](Node * n) {return int(n->m_Pos.x / 18) == x && int(n->m_Pos.y / 18) == y; }); | |
else | |
return m_NoObstacles.at(0); | |
} | |
COLLIDERDIR NodeManager::FindPath(Node * pStart, Node * pEnd) | |
{ | |
if(pStart == pEnd) | |
return COLLIDERDIR::none; | |
ResetNodes(); | |
vector<Node*> openList; | |
vector<Node*> finalList; | |
Node * currentNode = pStart; | |
CalcCosts(currentNode, pStart, pEnd); | |
currentNode->m_IsVisited = true; | |
// FIRST SEARCH | |
while (true) | |
{ | |
// calc costs | |
for (Node * node : currentNode->m_Connections) | |
{ | |
if (!node->m_IsVisited) | |
{ | |
CalcCosts(node, pStart, pEnd); | |
openList.push_back(node); | |
} | |
} | |
// get connection with lowest cost | |
if(openList.size() > 0) | |
{ | |
Node * lowestConnection = openList.at(0); | |
for (auto c : openList) | |
{ | |
if (c->cost < lowestConnection->cost) lowestConnection = c; | |
} | |
currentNode = lowestConnection; | |
currentNode->m_IsVisited = true; | |
//cout << to_string(int(currentNode->m_Pos.x/18)+1) + " " + to_string(int(currentNode->m_Pos.y/18)+1) + "\n"; | |
// pop current connection from openlist | |
openList.erase(std::remove(openList.begin(), openList.end(), currentNode), openList.end()); | |
} | |
if (currentNode == pEnd) | |
{ | |
//m_CheckNode->SetPosition(currentNode->m_Pos.x, currentNode->m_Pos.y); | |
break; | |
} | |
} | |
// REVERSE SEARCH | |
currentNode = pEnd; | |
//pEnd = pStart; | |
//string += to_string(int(currentNode->m_Pos.x / 18) + 1) + " " + to_string(int(currentNode->m_Pos.y / 18) + 1) + " -- "; | |
//int index = 0; | |
currentNode->m_IsVisitedSecondTime = true; | |
openList.clear(); | |
while(true) | |
{ | |
for(auto node : currentNode->m_Connections) | |
{ | |
if (node->m_IsVisited && !node->m_IsVisitedSecondTime) | |
{ | |
CalcCosts(node, pEnd, pStart); | |
openList.push_back(node); | |
} | |
} | |
if (openList.size() > 0) | |
{ | |
Node * lowestConnection = openList.at(0); | |
for (auto c : openList) | |
{ | |
if (c->cost < lowestConnection->cost) lowestConnection = c; | |
} | |
currentNode = lowestConnection; | |
currentNode->m_IsVisitedSecondTime = true; | |
finalList.push_back(currentNode); | |
openList.erase(std::remove(openList.begin(), openList.end(), currentNode), openList.end()); | |
} | |
if (currentNode == pStart) | |
{ | |
finalList.pop_back(); | |
break; | |
} | |
} | |
// SET STRING | |
reverse(finalList.begin(), finalList.end()); | |
if(finalList.size() > 0) | |
{ | |
if (finalList.at(0)->m_Pos.y < pStart->m_Pos.y) return COLLIDERDIR::top; | |
if (finalList.at(0)->m_Pos.y > pStart->m_Pos.y) return COLLIDERDIR::down; | |
if (finalList.at(0)->m_Pos.x < pStart->m_Pos.x) return COLLIDERDIR::left; | |
if (finalList.at(0)->m_Pos.x > pStart->m_Pos.x) return COLLIDERDIR::right; | |
} | |
return COLLIDERDIR::none; | |
} | |
void NodeManager::CalcConnections(Node * node) | |
{ | |
UNREFERENCED_PARAMETER(node); | |
} | |
float NodeManager::CalcCosts(Node * currentNode, Node * pStart, Node * pEnd) | |
{ | |
UNREFERENCED_PARAMETER(pStart); | |
Vector2 dirToPlayer = pEnd->m_Pos - currentNode->m_Pos; | |
float length = dirToPlayer.Length(); | |
currentNode->cost = length; | |
return length; | |
} | |
void NodeManager::ResetNodes() | |
{ | |
for (auto node : m_NoObstacles) | |
{ | |
node->m_IsVisited = false; | |
node->m_IsVisitedSecondTime = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment