Refactored code to do dynamic_casts in fewer places
This commit is contained in:
parent
ed6e0c2bf2
commit
c5d6167e8c
31
game.cpp
31
game.cpp
|
@ -97,17 +97,16 @@ void Game::render()
|
||||||
// Background image first
|
// Background image first
|
||||||
DrawUtils::draw(display, background, 0, 0);
|
DrawUtils::draw(display, background, 0, 0);
|
||||||
|
|
||||||
list<Vertex*> vertices = data.get_vertices();
|
|
||||||
|
|
||||||
// Now paint on the targeting circle
|
// Now paint on the targeting circle
|
||||||
if (data.get_current_vertex(true) != NULL)
|
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,
|
DrawUtils::draw_circle_filled(display, v->x, v->y, range,
|
||||||
range_colour);
|
range_colour);
|
||||||
}
|
}
|
||||||
|
|
||||||
// First draw the edges, so that they don't obscure any data
|
// 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();
|
for (list<Vertex*>::iterator cursor = vertices.begin();
|
||||||
cursor != vertices.end(); cursor++)
|
cursor != vertices.end(); cursor++)
|
||||||
{
|
{
|
||||||
|
@ -125,7 +124,7 @@ void Game::render()
|
||||||
// ring the selected vertex and write info about it
|
// ring the selected vertex and write info about it
|
||||||
if (data.get_current_vertex() != NULL)
|
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);
|
DrawUtils::draw_circle(display, v->x, v->y, v->r + 5, 0x000000);
|
||||||
draw_stats(v);
|
draw_stats(v);
|
||||||
}
|
}
|
||||||
|
@ -134,7 +133,7 @@ void Game::render()
|
||||||
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;
|
GameVertex* v = dynamic_cast<GameVertex*>(*cursor);
|
||||||
draw_node(v);
|
draw_node(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -149,8 +148,7 @@ void Game::render()
|
||||||
0x000000);
|
0x000000);
|
||||||
|
|
||||||
if (data.get_current_vertex() != NULL &&
|
if (data.get_current_vertex() != NULL &&
|
||||||
dynamic_cast<GameVertex*>(data.get_current_vertex())->player ==
|
data.get_current_vertex()->player == data.get_turn())
|
||||||
data.get_turn())
|
|
||||||
{
|
{
|
||||||
for (list<MenuButton*>::iterator cursor = buttons.begin();
|
for (list<MenuButton*>::iterator cursor = buttons.begin();
|
||||||
cursor != buttons.end(); cursor++)
|
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,
|
DrawUtils::draw_circle_filled(display, v->x, v->y, v->r,
|
||||||
v->colour);
|
v->colour);
|
||||||
|
@ -201,7 +199,7 @@ void Game::draw_node(Vertex* v)
|
||||||
DrawUtils::draw_text(display, itos(v->score), v->x, v->y, font,
|
DrawUtils::draw_text(display, itos(v->score), v->x, v->y, font,
|
||||||
0x00ff00, true, true);
|
0x00ff00, true, true);
|
||||||
|
|
||||||
switch (dynamic_cast<GameVertex*>(v)->type)
|
switch (v->type)
|
||||||
{
|
{
|
||||||
case VERTEX_ATTACKER:
|
case VERTEX_ATTACKER:
|
||||||
DrawUtils::draw(display, attacker_icon, v->x + 5, v-> y + 5);
|
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_num = 0;
|
||||||
int line_height = 14;
|
int line_height = 14;
|
||||||
|
@ -251,16 +249,13 @@ void Game::draw_stats(Vertex* v)
|
||||||
int adj_y = y;
|
int adj_y = y;
|
||||||
|
|
||||||
DrawUtils::draw_text(display, "player:", x, adj_y, font);
|
DrawUtils::draw_text(display, "player:", x, adj_y, font);
|
||||||
DrawUtils::draw_text(display,
|
DrawUtils::draw_text(display, v->player->get_name(), x + 50, adj_y, font);
|
||||||
dynamic_cast<GameVertex*>(v)->player->get_name(),
|
|
||||||
x + 50, adj_y, font);
|
|
||||||
|
|
||||||
line_num++;
|
line_num++;
|
||||||
adj_y = y + line_num * line_height;
|
adj_y = y + line_num * line_height;
|
||||||
DrawUtils::draw_text(display, "type:", x, adj_y, font);
|
DrawUtils::draw_text(display, "type:", x, adj_y, font);
|
||||||
VertexType vtype = dynamic_cast<GameVertex*>(v)->type;
|
|
||||||
string type;
|
string type;
|
||||||
switch(vtype)
|
switch(v->type)
|
||||||
{
|
{
|
||||||
case VERTEX_ATTACKER:
|
case VERTEX_ATTACKER:
|
||||||
type = "attacker";
|
type = "attacker";
|
||||||
|
@ -277,13 +272,13 @@ void Game::draw_stats(Vertex* v)
|
||||||
line_num++;
|
line_num++;
|
||||||
adj_y = y + line_num * line_height;
|
adj_y = y + line_num * line_height;
|
||||||
DrawUtils::draw_text(display, "atk:", x, adj_y, font);
|
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);
|
x + 50, adj_y, font);
|
||||||
|
|
||||||
line_num++;
|
line_num++;
|
||||||
adj_y = y + line_num * line_height;
|
adj_y = y + line_num * line_height;
|
||||||
DrawUtils::draw_text(display, "armor:", x, adj_y, font);
|
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);
|
x + 50, adj_y, font);
|
||||||
|
|
||||||
line_num++;
|
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, "hp:", x, adj_y, font);
|
||||||
DrawUtils::draw_text(display, itos(v->score), x + 50, 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++;
|
line_num++;
|
||||||
adj_y = y + line_num * line_height;
|
adj_y = y + line_num * line_height;
|
||||||
|
|
4
game.h
4
game.h
|
@ -38,9 +38,9 @@ class Game : public GameState
|
||||||
bool right, bool middle);
|
bool right, bool middle);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void draw_stats(Vertex* v);
|
void draw_stats(GameVertex* v);
|
||||||
void draw_button(MenuButton* button);
|
void draw_button(MenuButton* button);
|
||||||
void draw_node(Vertex* v);
|
void draw_node(GameVertex* v);
|
||||||
|
|
||||||
void handle_button_press(ButtonAction action);
|
void handle_button_press(ButtonAction action);
|
||||||
|
|
||||||
|
|
29
gamedata.cpp
29
gamedata.cpp
|
@ -25,17 +25,20 @@ GameData::GameData()
|
||||||
GameData::~GameData() { }
|
GameData::~GameData() { }
|
||||||
|
|
||||||
|
|
||||||
Vertex* GameData::get_current_vertex(bool only_mine) const
|
GameVertex* GameData::get_current_vertex(bool only_mine) const
|
||||||
{
|
{
|
||||||
if (only_mine)
|
if (only_mine)
|
||||||
{
|
{
|
||||||
if (current != NULL &&
|
if (current != NULL)
|
||||||
dynamic_cast<GameVertex*>(current)->player == turn) return current;
|
{
|
||||||
|
GameVertex* ret = dynamic_cast<GameVertex*>(current);
|
||||||
|
if (ret->player == turn) return ret;
|
||||||
|
}
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
return current;
|
return dynamic_cast<GameVertex*>(current);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -78,7 +81,7 @@ void GameData::handle_click(int x, int y)
|
||||||
int colour;
|
int colour;
|
||||||
colour = turn->get_colour();
|
colour = turn->get_colour();
|
||||||
|
|
||||||
Vertex* v = vertex_at(x, y, 0);
|
GameVertex* v = dynamic_cast<GameVertex*>(vertex_at(x, y, 0));
|
||||||
|
|
||||||
// fixme - energy expenditure should happen in each of these cases except
|
// fixme - energy expenditure should happen in each of these cases except
|
||||||
// MODE_SELECT
|
// MODE_SELECT
|
||||||
|
@ -92,7 +95,7 @@ void GameData::handle_click(int x, int y)
|
||||||
add_vertex(x, y, 0, r, colour);
|
add_vertex(x, y, 0, r, colour);
|
||||||
break;
|
break;
|
||||||
case MODE_ATTACK:
|
case MODE_ATTACK:
|
||||||
if (v == NULL || dynamic_cast<GameVertex*>(v)->player == turn) return;
|
if (v == NULL || v->player == turn) return;
|
||||||
if (v->colour != colour) attack_vertex(v);
|
if (v->colour != colour) attack_vertex(v);
|
||||||
break;
|
break;
|
||||||
case MODE_MOVE:
|
case MODE_MOVE:
|
||||||
|
@ -111,7 +114,7 @@ void GameData::handle_click(int x, int y)
|
||||||
|
|
||||||
bool GameData::select_vertex(int x, int y)
|
bool GameData::select_vertex(int x, int y)
|
||||||
{
|
{
|
||||||
Vertex * v = vertex_at(x, y, 0);
|
GameVertex * v = dynamic_cast<GameVertex*>(vertex_at(x, y, 0));
|
||||||
if (v == NULL) return false;
|
if (v == NULL) return false;
|
||||||
|
|
||||||
current = v;
|
current = v;
|
||||||
|
@ -143,7 +146,7 @@ bool GameData::add_vertex(int x, int y, int z, int r, int colour)
|
||||||
}
|
}
|
||||||
|
|
||||||
// same here - just a logic check
|
// same here - just a logic check
|
||||||
if (dynamic_cast<GameVertex*>(current)->player != turn)
|
if (current->player != turn)
|
||||||
{
|
{
|
||||||
delete v;
|
delete v;
|
||||||
return false;
|
return false;
|
||||||
|
@ -180,11 +183,11 @@ void GameData::set_mode(Mode m)
|
||||||
if (mode == MODE_ATTACK && m != mode) build_type = VERTEX_NONE;
|
if (mode == MODE_ATTACK && m != mode) build_type = VERTEX_NONE;
|
||||||
|
|
||||||
// The other modes all require current to match the player
|
// The other modes all require current to match the player
|
||||||
if (dynamic_cast<GameVertex*>(current)->player == turn) mode = m;
|
if (current->player == turn) mode = m;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int GameData::get_range(Vertex* node)
|
int GameData::get_range(GameVertex* node)
|
||||||
{
|
{
|
||||||
if (node == NULL) node = current;
|
if (node == NULL) node = current;
|
||||||
|
|
||||||
|
@ -210,10 +213,10 @@ int GameData::get_range(Vertex* node)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void GameData::attack_vertex(Vertex* target)
|
void GameData::attack_vertex(GameVertex* target)
|
||||||
{
|
{
|
||||||
float atk = dynamic_cast<GameVertex*>(current)->calculate_attack();
|
float atk = current->calculate_attack();
|
||||||
float armor = dynamic_cast<GameVertex*>(target)->calculate_armor();
|
float armor = target->calculate_armor();
|
||||||
int damage = (int)(atk / armor);
|
int damage = (int)(atk / armor);
|
||||||
target->score -= damage;
|
target->score -= damage;
|
||||||
if (target->score <= 0) remove_vertex(target);
|
if (target->score <= 0) remove_vertex(target);
|
||||||
|
|
|
@ -18,7 +18,7 @@ class GameData : public Graph
|
||||||
GameData();
|
GameData();
|
||||||
~GameData();
|
~GameData();
|
||||||
|
|
||||||
Vertex* get_current_vertex(bool only_mine = false) const;
|
GameVertex* get_current_vertex(bool only_mine = false) const;
|
||||||
void clear_current_vertex();
|
void clear_current_vertex();
|
||||||
|
|
||||||
void toggle_turn();
|
void toggle_turn();
|
||||||
|
@ -26,7 +26,7 @@ class GameData : public Graph
|
||||||
// select or add vertex, as appropriate
|
// select or add vertex, as appropriate
|
||||||
void handle_click(int x, int y);
|
void handle_click(int x, int y);
|
||||||
bool select_vertex(int x, int y);
|
bool select_vertex(int x, int y);
|
||||||
void attack_vertex(Vertex* target);
|
void attack_vertex(GameVertex* target);
|
||||||
|
|
||||||
bool add_vertex(int x, int y, int z, int r, int colour);
|
bool add_vertex(int x, int y, int z, int r, int colour);
|
||||||
|
|
||||||
|
@ -38,14 +38,14 @@ class GameData : public Graph
|
||||||
|
|
||||||
// returns the move/attack range for the specified node
|
// returns the move/attack range for the specified node
|
||||||
// (or the selected node if node == NULL)
|
// (or the selected node if node == NULL)
|
||||||
int get_range(Vertex* node = NULL);
|
int get_range(GameVertex* node = NULL);
|
||||||
|
|
||||||
// check for (and set, if needed) winner
|
// check for (and set, if needed) winner
|
||||||
bool endgame();
|
bool endgame();
|
||||||
Player* get_turn() const { return turn; }
|
Player* get_turn() const { return turn; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Vertex* current;
|
GameVertex* current;
|
||||||
Player player1, player2;
|
Player player1, player2;
|
||||||
Player* turn;
|
Player* turn;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user