Code for some of the basic game design - selecting existing nodes, limiting edge distance

This commit is contained in:
Anna Rose 2011-06-22 22:32:33 -04:00
parent 53f1560393
commit 48c45c2191
4 changed files with 64 additions and 9 deletions

View File

@ -5,11 +5,15 @@
#include <iostream> #include <iostream>
#endif #endif
int GameCore::NODE_SIZE = 25;
int GameCore::MAX_MOVE_DISTANCE = 200;
GameCore::GameCore() GameCore::GameCore()
{ {
display = NULL; display = NULL;
background = NULL; background = NULL;
node = NULL; node = NULL;
move_template = NULL;
is_running = true; is_running = true;
} }
@ -51,7 +55,11 @@ bool GameCore::init()
node = DrawUtils::load("node.bmp"); node = DrawUtils::load("node.bmp");
DrawUtils::transpare(node, 255, 0, 255); DrawUtils::transpare(node, 255, 0, 255);
if (background == NULL || node == NULL) move_template = DrawUtils::load("move_template.bmp");
DrawUtils::transpare(move_template, 255, 0, 255);
SDL_SetAlpha(move_template, SDL_SRCALPHA, 128);
if (background == NULL || node == NULL || move_template == NULL)
{ {
#ifdef DEBUG #ifdef DEBUG
std::cerr << "GameCore::init(): error: Couldn't load an image file\n"; std::cerr << "GameCore::init(): error: Couldn't load an image file\n";
@ -85,6 +93,13 @@ void GameCore::render()
0x000000); 0x000000);
} }
if (graph.get_current_vertex() != NULL)
{
Vertex* v = graph.get_current_vertex();
DrawUtils::draw(display, move_template, v->x, v->y);
}
SDL_Flip(display); SDL_Flip(display);
} }
@ -112,5 +127,13 @@ void GameCore::on_exit()
void GameCore::on_lbutton_down(int x, int y) void GameCore::on_lbutton_down(int x, int y)
{ {
graph.add_vertex(x, y, 25); Vertex* v = graph.get_current_vertex();
Vertex new_v = Vertex();
new_v.x = x;
new_v.y = y;
if (v != NULL && (Vertex::distance(*v, new_v) > MAX_MOVE_DISTANCE))
return
graph.do_vertex(x, y, NODE_SIZE);
} }

View File

@ -38,9 +38,14 @@ class GameCore : public MainEvent
// textures to draw // textures to draw
SDL_Surface* background; SDL_Surface* background;
SDL_Surface* node; SDL_Surface* node;
SDL_Surface* move_template;
// data // data
Graph graph; Graph graph;
// constants
static int NODE_SIZE;
static int MAX_MOVE_DISTANCE;
}; };
#endif #endif

View File

@ -11,7 +11,7 @@ using std::cerr;
Graph::Graph() Graph::Graph()
{ {
last_vertex = NULL; current_vertex = NULL;
} }
Graph::~Graph() Graph::~Graph()
@ -54,6 +54,29 @@ bool Graph::vertex_present(int x, int y, int size)
return false; return false;
} }
void Graph::do_vertex(int x, int y, int size)
{
if (vertex_present(x, y, size)) select_vertex(x, y);
else add_vertex(x, y, size);
}
void Graph::select_vertex(int x, int y)
{
for (list<Vertex*>::iterator cursor = vertices.begin();
cursor != vertices.end(); cursor++)
{
Vertex* v = *cursor;
if (x >= v->x_min && x <= v->x_max && y >= v->y_min && y <= v->y_max)
{
current_vertex = v;
return;
}
}
}
void Graph::add_vertex(int x, int y, int size) void Graph::add_vertex(int x, int y, int size)
{ {
Vertex* v = new Vertex(); Vertex* v = new Vertex();
@ -69,17 +92,17 @@ void Graph::add_vertex(int x, int y, int size)
vertices.push_back(v); vertices.push_back(v);
if (last_vertex == NULL) if (current_vertex == NULL)
{ {
last_vertex = v; current_vertex = v;
return; return;
} }
Edge e; Edge e;
e.a = last_vertex; e.a = current_vertex;
e.b = v; e.b = v;
edges.push_back(e); edges.push_back(e);
last_vertex = v; current_vertex = v;
} }

View File

@ -41,10 +41,14 @@ 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 add_vertex(int x, int y, int size); void do_vertex(int x, int y, int size);
Vertex* get_current_vertex() { return current_vertex; }
private: private:
Vertex* last_vertex; void add_vertex(int x, int y, int size);
void select_vertex(int x, int y);
Vertex* current_vertex;
list<Vertex*> vertices; list<Vertex*> vertices;
list<Edge> edges; list<Edge> edges;
}; };