Refactored code to do dynamic_casts in fewer places
This commit is contained in:
29
gamedata.cpp
29
gamedata.cpp
@ -25,17 +25,20 @@ 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 (current != NULL &&
|
||||
dynamic_cast<GameVertex*>(current)->player == turn) return current;
|
||||
if (current != NULL)
|
||||
{
|
||||
GameVertex* ret = dynamic_cast<GameVertex*>(current);
|
||||
if (ret->player == turn) return ret;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return current;
|
||||
return dynamic_cast<GameVertex*>(current);
|
||||
}
|
||||
|
||||
|
||||
@ -78,7 +81,7 @@ void GameData::handle_click(int x, int y)
|
||||
int 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
|
||||
// MODE_SELECT
|
||||
@ -92,7 +95,7 @@ void GameData::handle_click(int x, int y)
|
||||
add_vertex(x, y, 0, r, colour);
|
||||
break;
|
||||
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);
|
||||
break;
|
||||
case MODE_MOVE:
|
||||
@ -111,7 +114,7 @@ void GameData::handle_click(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;
|
||||
|
||||
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
|
||||
if (dynamic_cast<GameVertex*>(current)->player != turn)
|
||||
if (current->player != turn)
|
||||
{
|
||||
delete v;
|
||||
return false;
|
||||
@ -180,11 +183,11 @@ void GameData::set_mode(Mode m)
|
||||
if (mode == MODE_ATTACK && m != mode) build_type = VERTEX_NONE;
|
||||
|
||||
// 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;
|
||||
|
||||
@ -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 armor = dynamic_cast<GameVertex*>(target)->calculate_armor();
|
||||
float atk = current->calculate_attack();
|
||||
float armor = target->calculate_armor();
|
||||
int damage = (int)(atk / armor);
|
||||
target->score -= damage;
|
||||
if (target->score <= 0) remove_vertex(target);
|
||||
|
Reference in New Issue
Block a user