Major refactoring - moved constants around, made graph class more graphy, put things where they belong. Simplified a lot of code that I wrote when I wasn't thinking clearly, apparently.
This commit is contained in:
parent
54b46d77d2
commit
d76fc8099e
16
gamecore.cpp
16
gamecore.cpp
|
@ -1,8 +1,10 @@
|
||||||
#include "gamecore.h"
|
#include "gamecore.h"
|
||||||
#include "drawutils.h"
|
#include "drawutils.h"
|
||||||
|
#include "mathutils.h"
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
|
|
||||||
int GameCore::NODE_SIZE = 25;
|
int GameCore::MAX_MOVE_DISTANCE = 100;
|
||||||
|
int GameCore::NODE_RADIUS = 12;
|
||||||
|
|
||||||
GameCore::GameCore()
|
GameCore::GameCore()
|
||||||
{
|
{
|
||||||
|
@ -73,14 +75,14 @@ void GameCore::render()
|
||||||
{
|
{
|
||||||
Vertex* v = graph.get_current_vertex();
|
Vertex* v = graph.get_current_vertex();
|
||||||
DrawUtils::draw_circle_filled(display, v->x, v->y,
|
DrawUtils::draw_circle_filled(display, v->x, v->y,
|
||||||
Graph::MAX_MOVE_DISTANCE, 0xcb1919, 128);
|
MAX_MOVE_DISTANCE, 0xcb1919, 128);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (list<Vertex*>::iterator cursor = vertices.begin();
|
for (list<Vertex*>::iterator cursor = vertices.begin();
|
||||||
cursor != vertices.end(); cursor++)
|
cursor != vertices.end(); cursor++)
|
||||||
{
|
{
|
||||||
Vertex v = *(*cursor);
|
Vertex v = *(*cursor);
|
||||||
DrawUtils::draw_circle_filled(display, v.x, v.y, NODE_SIZE>>1,
|
DrawUtils::draw_circle_filled(display, v.x, v.y, NODE_RADIUS,
|
||||||
0x000000);
|
0x000000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -118,5 +120,11 @@ void GameCore::on_exit()
|
||||||
|
|
||||||
void GameCore::on_lbutton_down(int x, int y)
|
void GameCore::on_lbutton_down(int x, int y)
|
||||||
{
|
{
|
||||||
graph.do_vertex(x, y, NODE_SIZE);
|
Vertex* cv = graph.get_current_vertex();
|
||||||
|
if (cv != NULL &&
|
||||||
|
(MathUtils::distance(cv->x, cv->y, x, y)
|
||||||
|
> MAX_MOVE_DISTANCE))
|
||||||
|
return;
|
||||||
|
|
||||||
|
graph.do_vertex(x, y, NODE_RADIUS);
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,8 +41,8 @@ class GameCore : public MainEvent
|
||||||
// data
|
// data
|
||||||
Graph graph;
|
Graph graph;
|
||||||
|
|
||||||
// constants
|
static int NODE_RADIUS;
|
||||||
static int NODE_SIZE;
|
static int MAX_MOVE_DISTANCE;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
54
graph.cpp
54
graph.cpp
|
@ -1,9 +1,6 @@
|
||||||
#include "graph.h"
|
#include "graph.h"
|
||||||
#include "debug.h"
|
|
||||||
#include "mathutils.h"
|
#include "mathutils.h"
|
||||||
|
|
||||||
int Graph::MAX_MOVE_DISTANCE = 100;
|
|
||||||
|
|
||||||
Graph::Graph()
|
Graph::Graph()
|
||||||
{
|
{
|
||||||
current_vertex = NULL;
|
current_vertex = NULL;
|
||||||
|
@ -19,41 +16,23 @@ Graph::~Graph()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Graph::vertex_present(int x, int y, int size)
|
bool Graph::vertex_present(int x, int y, int r)
|
||||||
{
|
{
|
||||||
int delta = size / 2;
|
|
||||||
|
|
||||||
int x_min = x - delta;
|
|
||||||
int x_max = x + delta;
|
|
||||||
int y_min = y - delta;
|
|
||||||
int y_max = y + delta;
|
|
||||||
|
|
||||||
for (list<Vertex*>::iterator cursor = vertices.begin();
|
for (list<Vertex*>::iterator cursor = vertices.begin();
|
||||||
cursor != vertices.end(); cursor++)
|
cursor != vertices.end(); cursor++)
|
||||||
{
|
{
|
||||||
Vertex v = *(*cursor);
|
Vertex* v = *cursor;
|
||||||
if (((x_min >= v.x_min && x_min <= v.x_max) &&
|
if (MathUtils::distance(v->x, v->y, x, y) <= v->r) return true;
|
||||||
((y_min >= v.y_min && y_min <= v.y_max) ||
|
|
||||||
(y_max >= v.y_min && y_max <= v.y_max))) ||
|
|
||||||
((x_max >= v.x_min && x_max <= v.x_max) &&
|
|
||||||
((y_min >= v.y_min && y_min <= v.y_max) ||
|
|
||||||
(y_max >= v.y_min && y_max <= v.y_max))))
|
|
||||||
{
|
|
||||||
#ifdef DEBUG
|
|
||||||
cerr << "debug: Graph::vertex_present(): vertex present at x=" << v.x << ", y=" << v.y << "\n";
|
|
||||||
#endif
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Graph::do_vertex(int x, int y, int size)
|
void Graph::do_vertex(int x, int y, int r)
|
||||||
{
|
{
|
||||||
if (vertex_present(x, y, size)) select_vertex(x, y);
|
if (vertex_present(x, y, r)) select_vertex(x, y);
|
||||||
else add_vertex(x, y, size);
|
else add_vertex(x, y, r);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -63,7 +42,7 @@ void Graph::select_vertex(int x, int y)
|
||||||
cursor != vertices.end(); cursor++)
|
cursor != vertices.end(); cursor++)
|
||||||
{
|
{
|
||||||
Vertex* v = *cursor;
|
Vertex* v = *cursor;
|
||||||
if (x >= v->x_min && x <= v->x_max && y >= v->y_min && y <= v->y_max)
|
if (MathUtils::distance(v->x, v->y, x, y) <= v->r)
|
||||||
{
|
{
|
||||||
current_vertex = v;
|
current_vertex = v;
|
||||||
return;
|
return;
|
||||||
|
@ -72,23 +51,12 @@ void Graph::select_vertex(int x, int y)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Graph::add_vertex(int x, int y, int size)
|
void Graph::add_vertex(int x, int y, int r)
|
||||||
{
|
{
|
||||||
if (current_vertex != NULL &&
|
Vertex* v = new Vertex(x, y, r);
|
||||||
(MathUtils::distance(static_cast<float>(current_vertex->x),
|
|
||||||
static_cast<float>(current_vertex->y),
|
|
||||||
static_cast<float>(x),
|
|
||||||
static_cast<float>(y))
|
|
||||||
> MAX_MOVE_DISTANCE))
|
|
||||||
return;
|
|
||||||
|
|
||||||
Vertex* v = new Vertex(x, y);
|
// Make sure the nodes won't overlap
|
||||||
v->x_min = x - size/2;
|
if (vertex_present(v->x, v->y, v->r + r)) return;
|
||||||
v->x_max = x + size/2;
|
|
||||||
v->y_min = y - size/2;
|
|
||||||
v->y_max = y + size/2;
|
|
||||||
|
|
||||||
if (vertex_present(v->x, v->y, 25)) return;
|
|
||||||
|
|
||||||
vertices.push_back(v);
|
vertices.push_back(v);
|
||||||
|
|
||||||
|
|
15
graph.h
15
graph.h
|
@ -14,15 +14,12 @@ using std::list;
|
||||||
class Vertex
|
class Vertex
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Vertex(int x, int y)
|
Vertex(int x, int y, int r)
|
||||||
{ this->x = x; this->y = y; }
|
{ this->x = x; this->y = y; }
|
||||||
|
|
||||||
int x;
|
int x;
|
||||||
int y;
|
int y;
|
||||||
int x_min;
|
int r;
|
||||||
int y_min;
|
|
||||||
int x_max;
|
|
||||||
int y_max;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Edge
|
struct Edge
|
||||||
|
@ -42,14 +39,12 @@ class Graph
|
||||||
list<Vertex*> get_vertices() { return vertices; }
|
list<Vertex*> get_vertices() { return vertices; }
|
||||||
list<Edge> get_edges() { return edges; }
|
list<Edge> get_edges() { return edges; }
|
||||||
|
|
||||||
void do_vertex(int x, int y, int size);
|
void select_vertex(int x, int y);
|
||||||
|
void do_vertex(int x, int y, int r);
|
||||||
Vertex* get_current_vertex() { return current_vertex; }
|
Vertex* get_current_vertex() { return current_vertex; }
|
||||||
|
|
||||||
static int MAX_MOVE_DISTANCE;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void add_vertex(int x, int y, int size);
|
void add_vertex(int x, int y, int r);
|
||||||
void select_vertex(int x, int y);
|
|
||||||
|
|
||||||
Vertex* current_vertex;
|
Vertex* current_vertex;
|
||||||
list<Vertex*> vertices;
|
list<Vertex*> vertices;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user