Implemented energy costs
This commit is contained in:
66
gamedata.cpp
66
gamedata.cpp
@ -67,6 +67,7 @@ void GameData::toggle_turn()
|
||||
{
|
||||
mode = MODE_SELECT;
|
||||
build_type = VERTEX_NONE;
|
||||
produce_energy();
|
||||
}
|
||||
}
|
||||
|
||||
@ -83,8 +84,6 @@ void GameData::handle_click(int x, int y)
|
||||
|
||||
GameVertex* v = dynamic_cast<GameVertex*>(vertex_at(x, y, 0));
|
||||
|
||||
// fixme - energy expenditure should happen in each of these cases except
|
||||
// MODE_SELECT
|
||||
switch (mode)
|
||||
{
|
||||
case MODE_SELECT:
|
||||
@ -100,11 +99,15 @@ void GameData::handle_click(int x, int y)
|
||||
break;
|
||||
case MODE_MOVE:
|
||||
if (current == NULL) return;
|
||||
if (MathUtils::distance(current->x, current->y, current->z, x, y, 0)
|
||||
<= get_range())
|
||||
int dist = MathUtils::distance(current->x, current->y, current->z,
|
||||
x, y, 0);
|
||||
if (dist <= get_range())
|
||||
{
|
||||
current->x = x;
|
||||
current->y = y;
|
||||
if (turn->spend_energy(dist))
|
||||
{
|
||||
current->x = x;
|
||||
current->y = y;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
@ -123,7 +126,21 @@ bool GameData::select_vertex(int x, int y)
|
||||
|
||||
bool GameData::add_vertex(int x, int y, int z, int r, int colour)
|
||||
{
|
||||
if (build_type == VERTEX_NONE) return false;
|
||||
int energy_cost = 0;
|
||||
switch (build_type)
|
||||
{
|
||||
case VERTEX_NONE:
|
||||
return false;
|
||||
case VERTEX_ATTACKER:
|
||||
energy_cost = 50;
|
||||
break;
|
||||
case VERTEX_DEFENDER:
|
||||
energy_cost = 25;
|
||||
break;
|
||||
case VERTEX_PRODUCER:
|
||||
energy_cost = 25 + (25 * num_vertices_by_type(build_type, turn));
|
||||
break;
|
||||
}
|
||||
|
||||
GameVertex* v = new GameVertex(x, y, z, r, colour, 10, build_type, turn);
|
||||
|
||||
@ -134,12 +151,15 @@ bool GameData::add_vertex(int x, int y, int z, int r, int colour)
|
||||
{
|
||||
if (Graph::add_vertex(v))
|
||||
{
|
||||
toggle_turn();
|
||||
return true;
|
||||
if (turn->spend_energy(energy_cost))
|
||||
{
|
||||
toggle_turn();
|
||||
return true;
|
||||
}
|
||||
else remove_vertex(v);
|
||||
}
|
||||
}
|
||||
|
||||
// really, we shouldn't be able to get here. return false just in case
|
||||
delete v;
|
||||
return false;
|
||||
}
|
||||
@ -161,6 +181,7 @@ bool GameData::add_vertex(int x, int y, int z, int r, int colour)
|
||||
|
||||
if (Graph::add_vertex(v, current))
|
||||
{
|
||||
if (!turn->spend_energy(energy_cost)) remove_vertex(v);
|
||||
mode = MODE_SELECT;
|
||||
return true;
|
||||
}
|
||||
@ -245,3 +266,28 @@ bool GameData::endgame()
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
int GameData::num_vertices_by_type(VertexType type, Player* player)
|
||||
{
|
||||
int num = 0;
|
||||
|
||||
for (list<Vertex*>::iterator cursor = vertices.begin();
|
||||
cursor != vertices.end(); cursor++)
|
||||
{
|
||||
GameVertex* v = dynamic_cast<GameVertex*>(*cursor);
|
||||
|
||||
if (player != NULL && v->player != player) continue;
|
||||
|
||||
if (v->type == type) num++;
|
||||
}
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
|
||||
void GameData::produce_energy()
|
||||
{
|
||||
int amount = 25 * num_vertices_by_type(VERTEX_PRODUCER, turn);
|
||||
turn->add_energy(amount);
|
||||
}
|
||||
|
Reference in New Issue
Block a user