Created GameVertex class so that we can store game-specific data in the vertices

This commit is contained in:
Anna Rose 2011-07-01 22:04:58 -04:00
parent e221f1990c
commit a2f36a447b
4 changed files with 39 additions and 11 deletions

View File

@ -14,6 +14,15 @@ int GameData::BASE_BUILD_RADIUS = 75;
int GameData::NODE_RADIUS = 10; int GameData::NODE_RADIUS = 10;
GameVertex::GameVertex(int x, int y, int z, int r, int colour, int score,
VertexType type, Player* player)
: Vertex(x, y, z, r, colour, score)
{
this->type = type;
this->player = player;
}
GameData::GameData() GameData::GameData()
: Graph(true) : Graph(true)
{ {
@ -108,12 +117,15 @@ bool GameData::select_vertex(int x, int y, bool only_mine)
bool GameData::add_vertex(int x, int y, int z, int r, int colour) bool GameData::add_vertex(int x, int y, int z, int r, int colour)
{ {
GameVertex* v = new GameVertex(x, y, z, r, colour, 10, VERTEX_ATTACKER,
turn);
if (current == NULL) if (current == NULL)
{ {
// this is the special case for adding the first vertex for each player // this is the special case for adding the first vertex for each player
if (!turn->has_played()) if (!turn->has_played())
{ {
if (Graph::add_vertex(x, y, z, r, colour, 10)) if (Graph::add_vertex(v))
{ {
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "debug: GameData::add_vertex(): strength=%2.f\n", calculate_strength(*(vertices.rbegin()))); fprintf(stderr, "debug: GameData::add_vertex(): strength=%2.f\n", calculate_strength(*(vertices.rbegin())));
@ -124,17 +136,26 @@ bool GameData::add_vertex(int x, int y, int z, int r, int colour)
} }
// really, we shouldn't be able to get here. return false just in case // really, we shouldn't be able to get here. return false just in case
delete v;
return false; return false;
} }
// same here - just a logic check // same here - just a logic check
if (current->colour != turn->get_colour()) return false; if (current->colour != turn->get_colour())
{
delete v;
return false;
}
// This is the range check... // This is the range check...
if (MathUtils::distance(current->x, current->y, 0, x, y, 0) > get_range()) if (MathUtils::distance(current->x, current->y, 0, v->x, v->y, 0) >
get_range())
{
delete v;
return false; return false;
}
if (Graph::add_vertex(x, y, z, r, colour, 10, current)) if (Graph::add_vertex(v, current))
{ {
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "debug: GameData::add_vertex(): strength=%.2f\n", fprintf(stderr, "debug: GameData::add_vertex(): strength=%.2f\n",
@ -144,6 +165,8 @@ bool GameData::add_vertex(int x, int y, int z, int r, int colour)
toggle_turn(); toggle_turn();
return true; return true;
} }
delete v;
return false; return false;
} }

View File

@ -12,6 +12,16 @@
enum Mode {MODE_MOVE, MODE_ATTACK, MODE_BUILD, MODE_SELECT}; enum Mode {MODE_MOVE, MODE_ATTACK, MODE_BUILD, MODE_SELECT};
enum VertexType {VERTEX_ATTACKER, VERTEX_DEFENDER, VERTEX_PRODUCER}; enum VertexType {VERTEX_ATTACKER, VERTEX_DEFENDER, VERTEX_PRODUCER};
class GameVertex : public Vertex
{
public:
GameVertex(int x, int y, int z, int r, int colour = 0, int score = 0,
VertexType type = VERTEX_ATTACKER, Player* player = NULL);
VertexType type;
Player* player;
};
class GameData : public Graph class GameData : public Graph
{ {
public: public:

View File

@ -87,17 +87,14 @@ bool Graph::crosses_edge(Vertex* a, Vertex* b)
} }
bool Graph::add_vertex(int x, int y, int z, int r, int colour, int score, Vertex* src) bool Graph::add_vertex(Vertex* v, Vertex* src)
{ {
Vertex* v = new Vertex(x, y, z, r, colour, score);
// Make sure the nodes won't overlap // Make sure the nodes won't overlap
if (vertex_would_overlap(v->x, v->y, v->z, v->r)) if (vertex_would_overlap(v->x, v->y, v->z, v->r))
{ {
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "debug: Graph::add_vertex(): failed to add due to vertex collision: x=%d, y=%d, z=%d, r=%d\n", v->x, v->y, v->z, v->r); fprintf(stderr, "debug: Graph::add_vertex(): failed to add due to vertex collision: x=%d, y=%d, z=%d, r=%d\n", v->x, v->y, v->z, v->r);
#endif #endif
delete v;
return false; return false;
} }
@ -108,7 +105,6 @@ bool Graph::add_vertex(int x, int y, int z, int r, int colour, int score, Vertex
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "debug: Graph::add_vertex(): failed to add due to edge collision: x1=%d, y1=%d, x2=%d, y2=%d\n", v->x, v->y, src->x, src->y); fprintf(stderr, "debug: Graph::add_vertex(): failed to add due to edge collision: x1=%d, y1=%d, x2=%d, y2=%d\n", v->x, v->y, src->x, src->y);
#endif #endif
delete v;
return false; return false;
} }

View File

@ -48,8 +48,7 @@ class Graph
bool point_in_vertex(int x, int y, int z); bool point_in_vertex(int x, int y, int z);
Vertex * vertex_at(int x, int y, int z); Vertex * vertex_at(int x, int y, int z);
virtual bool add_vertex(int x, int y, int z, int r, int colour = 0, virtual bool add_vertex(Vertex* v, Vertex* src=NULL);
int score = 0, Vertex* src=NULL);
void remove_vertex(Vertex* target); void remove_vertex(Vertex* target);
bool is_planar() const { return planar; } bool is_planar() const { return planar; }