Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <list>
- #include <vector>
- #include <queue>
- #include <algorithm>
- #include "Vector2.h"
- #include "SpriteBatch.h"
- #include "GameObject.h"
- class Graph : public GameObject
- {
- struct Node;
- struct Edge;
- public:
- Graph();
- Graph(int rows, int columns, float disBetweenNodes, Vector3 nodeDimensions);
- ~Graph();
- void AddNode(Node * node);
- void AddEdge(Node * from, Node * to);
- void FindPath(Node * startNode, Node * endNode, std::list<Node*>& outPath);
- void DrawNodes(SpriteBatch* batch);
- void Draw(SpriteBatch* batch) override;
- struct Node
- {
- Vector2 pos;
- bool isTraversed = false;
- bool inQueue = false;
- float gScore;
- float hScore;
- float fScore;
- Node* parent;
- std::vector<Edge> connections;
- Node() : gScore(INFINITY), parent(nullptr) {}
- Node(Vector2 a_pos, Node* a_parent) :
- pos(a_pos), gScore(INFINITY), parent(a_parent){}
- Node(Vector2 a_pos) :
- pos(a_pos), gScore(INFINITY), parent(nullptr){}
- Node(char nodeChar) :
- pos(Vector2(0, 0)), gScore(INFINITY), parent(nullptr){}
- };
- struct Edge
- {
- union
- {
- float weight;
- float magnitude;
- };
- Node* connection;
- Edge() : weight(0), connection(nullptr) {}
- Edge(Node* node, float weight) : weight(weight), connection(node) {}
- Edge(Node* node) : weight(0), connection(node) {}
- };
- int cNodes;
- std::list<Node*> nodes;
- };
- #include "Graph.h"
- bool sortAscending(Graph::Node* a, Graph::Node * b)
- {
- return a->gScore < b->gScore;
- }
- Graph::Graph()
- {
- }
- Graph::Graph(int rows, int columns, float disBetweenNodes, Vector3 nodeDimensions)
- {
- Vector2 nodePos;
- cNodes = rows * columns;
- m_dimensions = nodeDimensions;
- for (size_t x = 0; x < rows; x++)
- {
- for (size_t y = 0; y < columns; y++)
- {
- nodePos.x = x * disBetweenNodes + m_dimensions.x / 2;
- nodePos.y = y * disBetweenNodes + m_dimensions.y / 2;
- nodes.push_back(new Node(nodePos));
- }
- }
- }
- Graph::~Graph()
- {
- }
- void Graph::AddNode(Node * node)
- {
- //nodes.push_back(node);
- }
- void Graph::AddEdge(Node * from, Node * to)
- {
- float magnitude;
- magnitude = (to->pos - from->pos).magnitude();
- from->connections.push_back(Edge(to, magnitude));
- }
- void Graph::DrawNodes(SpriteBatch * batch)
- {
- for (size_t i = 0; i < cNodes; i++)
- {
- batch->drawSprite(m_sprite, nodes.at(i)->pos.x, nodes.at(i)->pos.y, m_dimensions.x, m_dimensions.y);
- }
- }
- void Graph::Draw(SpriteBatch * batch)
- {
- DrawNodes(batch);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement