Advertisement
Guest User

Untitled

a guest
Jun 26th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. #pragma once
  2. #include <list>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include "Vector2.h"
  7. #include "SpriteBatch.h"
  8. #include "GameObject.h"
  9.  
  10. class Graph : public GameObject
  11. {
  12. struct Node;
  13. struct Edge;
  14.  
  15. public:
  16. Graph();
  17. Graph(int rows, int columns, float disBetweenNodes, Vector3 nodeDimensions);
  18. ~Graph();
  19. void AddNode(Node * node);
  20. void AddEdge(Node * from, Node * to);
  21. void FindPath(Node * startNode, Node * endNode, std::list<Node*>& outPath);
  22. void DrawNodes(SpriteBatch* batch);
  23. void Draw(SpriteBatch* batch) override;
  24.  
  25. struct Node
  26. {
  27. Vector2 pos;
  28. bool isTraversed = false;
  29. bool inQueue = false;
  30. float gScore;
  31. float hScore;
  32. float fScore;
  33. Node* parent;
  34. std::vector<Edge> connections;
  35.  
  36. Node() : gScore(INFINITY), parent(nullptr) {}
  37.  
  38. Node(Vector2 a_pos, Node* a_parent) :
  39. pos(a_pos), gScore(INFINITY), parent(a_parent){}
  40.  
  41. Node(Vector2 a_pos) :
  42. pos(a_pos), gScore(INFINITY), parent(nullptr){}
  43.  
  44. Node(char nodeChar) :
  45. pos(Vector2(0, 0)), gScore(INFINITY), parent(nullptr){}
  46. };
  47.  
  48. struct Edge
  49. {
  50. union
  51. {
  52. float weight;
  53. float magnitude;
  54. };
  55. Node* connection;
  56.  
  57. Edge() : weight(0), connection(nullptr) {}
  58. Edge(Node* node, float weight) : weight(weight), connection(node) {}
  59. Edge(Node* node) : weight(0), connection(node) {}
  60. };
  61.  
  62. int cNodes;
  63. std::list<Node*> nodes;
  64. };
  65.  
  66. #include "Graph.h"
  67.  
  68. bool sortAscending(Graph::Node* a, Graph::Node * b)
  69. {
  70. return a->gScore < b->gScore;
  71. }
  72.  
  73. Graph::Graph()
  74. {
  75. }
  76.  
  77. Graph::Graph(int rows, int columns, float disBetweenNodes, Vector3 nodeDimensions)
  78. {
  79. Vector2 nodePos;
  80. cNodes = rows * columns;
  81. m_dimensions = nodeDimensions;
  82.  
  83. for (size_t x = 0; x < rows; x++)
  84. {
  85. for (size_t y = 0; y < columns; y++)
  86. {
  87. nodePos.x = x * disBetweenNodes + m_dimensions.x / 2;
  88. nodePos.y = y * disBetweenNodes + m_dimensions.y / 2;
  89. nodes.push_back(new Node(nodePos));
  90. }
  91. }
  92. }
  93.  
  94. Graph::~Graph()
  95. {
  96. }
  97.  
  98. void Graph::AddNode(Node * node)
  99. {
  100. //nodes.push_back(node);
  101. }
  102.  
  103. void Graph::AddEdge(Node * from, Node * to)
  104. {
  105. float magnitude;
  106. magnitude = (to->pos - from->pos).magnitude();
  107. from->connections.push_back(Edge(to, magnitude));
  108. }
  109.  
  110. void Graph::DrawNodes(SpriteBatch * batch)
  111. {
  112. for (size_t i = 0; i < cNodes; i++)
  113. {
  114. batch->drawSprite(m_sprite, nodes.at(i)->pos.x, nodes.at(i)->pos.y, m_dimensions.x, m_dimensions.y);
  115. }
  116. }
  117.  
  118. void Graph::Draw(SpriteBatch * batch)
  119. {
  120. DrawNodes(batch);
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement