Refactored code to do dynamic_casts in fewer places

This commit is contained in:
2011-07-03 16:57:47 -04:00
parent ed6e0c2bf2
commit c5d6167e8c
4 changed files with 35 additions and 37 deletions

View File

@ -97,17 +97,16 @@ void Game::render()
// Background image first
DrawUtils::draw(display, background, 0, 0);
list<Vertex*> vertices = data.get_vertices();
// Now paint on the targeting circle
if (data.get_current_vertex(true) != NULL)
{
Vertex* v = data.get_current_vertex();
GameVertex* v = data.get_current_vertex();
DrawUtils::draw_circle_filled(display, v->x, v->y, range,
range_colour);
}
// First draw the edges, so that they don't obscure any data
list<Vertex*> vertices = data.get_vertices();
for (list<Vertex*>::iterator cursor = vertices.begin();
cursor != vertices.end(); cursor++)
{
@ -125,7 +124,7 @@ void Game::render()
// ring the selected vertex and write info about it
if (data.get_current_vertex() != NULL)
{
Vertex* v = data.get_current_vertex();
GameVertex* v = data.get_current_vertex();
DrawUtils::draw_circle(display, v->x, v->y, v->r + 5, 0x000000);
draw_stats(v);
}
@ -134,7 +133,7 @@ void Game::render()
for (list<Vertex*>::iterator cursor = vertices.begin();
cursor != vertices.end(); cursor++)
{
Vertex* v = *cursor;
GameVertex* v = dynamic_cast<GameVertex*>(*cursor);
draw_node(v);
}
@ -149,8 +148,7 @@ void Game::render()
0x000000);
if (data.get_current_vertex() != NULL &&
dynamic_cast<GameVertex*>(data.get_current_vertex())->player ==
data.get_turn())
data.get_current_vertex()->player == data.get_turn())
{
for (list<MenuButton*>::iterator cursor = buttons.begin();
cursor != buttons.end(); cursor++)
@ -193,7 +191,7 @@ void Game::draw_button(MenuButton* button)
}
void Game::draw_node(Vertex* v)
void Game::draw_node(GameVertex* v)
{
DrawUtils::draw_circle_filled(display, v->x, v->y, v->r,
v->colour);
@ -201,7 +199,7 @@ void Game::draw_node(Vertex* v)
DrawUtils::draw_text(display, itos(v->score), v->x, v->y, font,
0x00ff00, true, true);
switch (dynamic_cast<GameVertex*>(v)->type)
switch (v->type)
{
case VERTEX_ATTACKER:
DrawUtils::draw(display, attacker_icon, v->x + 5, v-> y + 5);
@ -242,7 +240,7 @@ void Game::handle_button_press(ButtonAction action)
}
void Game::draw_stats(Vertex* v)
void Game::draw_stats(GameVertex* v)
{
int line_num = 0;
int line_height = 14;
@ -251,16 +249,13 @@ void Game::draw_stats(Vertex* v)
int adj_y = y;
DrawUtils::draw_text(display, "player:", x, adj_y, font);
DrawUtils::draw_text(display,
dynamic_cast<GameVertex*>(v)->player->get_name(),
x + 50, adj_y, font);
DrawUtils::draw_text(display, v->player->get_name(), x + 50, adj_y, font);
line_num++;
adj_y = y + line_num * line_height;
DrawUtils::draw_text(display, "type:", x, adj_y, font);
VertexType vtype = dynamic_cast<GameVertex*>(v)->type;
string type;
switch(vtype)
switch(v->type)
{
case VERTEX_ATTACKER:
type = "attacker";
@ -277,13 +272,13 @@ void Game::draw_stats(Vertex* v)
line_num++;
adj_y = y + line_num * line_height;
DrawUtils::draw_text(display, "atk:", x, adj_y, font);
DrawUtils::draw_text(display, itos(dynamic_cast<GameVertex*>(v)->calculate_attack()),
DrawUtils::draw_text(display, itos(v->calculate_attack()),
x + 50, adj_y, font);
line_num++;
adj_y = y + line_num * line_height;
DrawUtils::draw_text(display, "armor:", x, adj_y, font);
DrawUtils::draw_text(display, itos(dynamic_cast<GameVertex*>(v)->calculate_armor()),
DrawUtils::draw_text(display, itos(v->calculate_armor()),
x + 50, adj_y, font);
line_num++;
@ -291,7 +286,7 @@ void Game::draw_stats(Vertex* v)
DrawUtils::draw_text(display, "hp:", x, adj_y, font);
DrawUtils::draw_text(display, itos(v->score), x + 50, adj_y, font);
if (vtype == VERTEX_PRODUCER)
if (v->type == VERTEX_PRODUCER)
{
line_num++;
adj_y = y + line_num * line_height;