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 "drawutils.h"
|
||||
#include "mathutils.h"
|
||||
#include "debug.h"
|
||||
|
||||
int GameCore::NODE_SIZE = 25;
|
||||
int GameCore::MAX_MOVE_DISTANCE = 100;
|
||||
int GameCore::NODE_RADIUS = 12;
|
||||
|
||||
GameCore::GameCore()
|
||||
{
|
||||
|
@ -73,14 +75,14 @@ void GameCore::render()
|
|||
{
|
||||
Vertex* v = graph.get_current_vertex();
|
||||
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();
|
||||
cursor != vertices.end(); 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);
|
||||
}
|
||||
|
||||
|
@ -118,5 +120,11 @@ void GameCore::on_exit()
|
|||
|
||||
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
|
||||
Graph graph;
|
||||
|
||||
// constants
|
||||
static int NODE_SIZE;
|
||||
static int NODE_RADIUS;
|
||||
static int MAX_MOVE_DISTANCE;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
54
graph.cpp
54
graph.cpp
|
@ -1,9 +1,6 @@
|
|||
#include "graph.h"
|
||||
#include "debug.h"
|
||||
#include "mathutils.h"
|
||||
|
||||
int Graph::MAX_MOVE_DISTANCE = 100;
|
||||
|
||||
Graph::Graph()
|
||||
{
|
||||
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();
|
||||
cursor != vertices.end(); cursor++)
|
||||
{
|
||||
Vertex v = *(*cursor);
|
||||
if (((x_min >= v.x_min && x_min <= v.x_max) &&
|
||||
((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;
|
||||
}
|
||||
Vertex* v = *cursor;
|
||||
if (MathUtils::distance(v->x, v->y, x, y) <= v->r) return true;
|
||||
}
|
||||
|
||||
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);
|
||||
else add_vertex(x, y, size);
|
||||
if (vertex_present(x, y, r)) select_vertex(x, y);
|
||||
else add_vertex(x, y, r);
|
||||
}
|
||||
|
||||
|
||||
|
@ -63,7 +42,7 @@ void Graph::select_vertex(int x, int y)
|
|||
cursor != vertices.end(); 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;
|
||||
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 &&
|
||||
(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, r);
|
||||
|
||||
Vertex* v = new Vertex(x, y);
|
||||
v->x_min = x - size/2;
|
||||
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;
|
||||
// Make sure the nodes won't overlap
|
||||
if (vertex_present(v->x, v->y, v->r + r)) return;
|
||||
|
||||
vertices.push_back(v);
|
||||
|
||||
|
|
15
graph.h
15
graph.h
|
@ -14,15 +14,12 @@ using std::list;
|
|||
class Vertex
|
||||
{
|
||||
public:
|
||||
Vertex(int x, int y)
|
||||
Vertex(int x, int y, int r)
|
||||
{ this->x = x; this->y = y; }
|
||||
|
||||
int x;
|
||||
int y;
|
||||
int x_min;
|
||||
int y_min;
|
||||
int x_max;
|
||||
int y_max;
|
||||
int r;
|
||||
};
|
||||
|
||||
struct Edge
|
||||
|
@ -42,14 +39,12 @@ class Graph
|
|||
list<Vertex*> get_vertices() { return vertices; }
|
||||
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; }
|
||||
|
||||
static int MAX_MOVE_DISTANCE;
|
||||
|
||||
private:
|
||||
void add_vertex(int x, int y, int size);
|
||||
void select_vertex(int x, int y);
|
||||
void add_vertex(int x, int y, int r);
|
||||
|
||||
Vertex* current_vertex;
|
||||
list<Vertex*> vertices;
|
||||
|
|
Loading…
Reference in New Issue
Block a user