Most of the attack functionality now in place, but it's not all quite working yet

This commit is contained in:
2011-06-24 14:59:18 -04:00
parent e51a328641
commit 6aedad51e5
4 changed files with 142 additions and 23 deletions

View File

@ -22,9 +22,14 @@ GameData::~GameData() { }
void GameData::toggle_turn()
{
if (player == PLAYER1) player = PLAYER2;
else if (player == PLAYER2) player = PLAYER1;
mode = MODE_MOVE;
current = NULL;
if (!endgame())
{
if (player == PLAYER1) player = PLAYER2;
else if (player == PLAYER2) player = PLAYER1;
}
}
@ -38,15 +43,23 @@ void GameData::do_vertex(int x, int y, int r)
return;
}
int colour;
if (player == PLAYER1) colour = PLAYER1_COLOUR;
if (player == PLAYER2) colour = PLAYER2_COLOUR;
if (mode == MODE_MOVE)
{
int colour;
if (player == PLAYER1) colour = PLAYER1_COLOUR;
if (player == PLAYER2) colour = PLAYER2_COLOUR;
if (point_in_vertex(x, y, r)) select_vertex(x, y);
if (point_in_vertex(x, y)) select_vertex(x, y);
else add_vertex(x, y, r, colour);
}
if (mode == MODE_ATTACK)
{
Vertex* v = vertex_at(x, y);
if (v == NULL) return;
if (v->colour == colour) select_vertex(x, y);
else attack_vertex(v);
}
}
@ -72,9 +85,9 @@ bool GameData::add_vertex(int x, int y, int r, int colour)
{
if (mode == MODE_ATTACK) return false;
// this is the special case for adding the first vertex for each player
if (current == NULL)
{
// this is the special case for adding the first vertex for each player
if ((player == PLAYER1 && !player1_played) ||
(player == PLAYER2 && !player2_played))
{
@ -98,7 +111,6 @@ bool GameData::add_vertex(int x, int y, int r, int colour)
calculate_strength(*(vertices.rbegin())));
#endif
clear_current_vertex();
toggle_turn();
return true;
}
@ -171,12 +183,53 @@ int GameData::get_range(Vertex* node)
else if (mode == MODE_MOVE) return 100;
else if (mode == MODE_ATTACK)
{
int range = 200;
list<Vertex*> neighbors = get_neighbors(node);
for(list<Vertex*>::iterator cursor = neighbors.begin();
cursor != neighbors.end(); cursor++)
{
Vertex* v = *cursor;
range -= 100 - MathUtils::distance(v->x, v->y, node->x, node->y);
}
}
}
void GameData::attack_vertex(Vertex* target)
{
float atk_str = calculate_strength(current);
float def_str = calculate_strength(target);
float armor = def_str / 10; // how much energy it takes to deal 1 damage
int damage = (int)(atk_str / armor);
target->score -= damage;
if (target->score <= 0) remove_vertex(target);
#ifdef DEBUG
fprintf(stderr, "debug: GameData::attack_vertex(): atk_str=%2.f, def_str=%2.f, armor=%2.f, damage=%d\n", atk_str, def_str, armor, damage);
#endif
toggle_turn();
}
bool GameData::endgame()
{
if (!(player1_played && player2_played)) return false;
if (get_colour(PLAYER1_COLOUR).empty())
{
player = WIN2;
return true;
}
if (get_colour(PLAYER2_COLOUR).empty())
{
player = WIN1;
return true;
}
return false;
}