Implemented energy costs
This commit is contained in:
parent
cfe5666c04
commit
79740d3cee
17
game.cpp
17
game.cpp
|
@ -125,7 +125,7 @@ void Game::render()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ring the selected vertex and write info about it
|
// highlight the selected vertex and write info about it
|
||||||
if (data.get_current_vertex() != NULL)
|
if (data.get_current_vertex() != NULL)
|
||||||
{
|
{
|
||||||
GameVertex* v = data.get_current_vertex();
|
GameVertex* v = data.get_current_vertex();
|
||||||
|
@ -169,6 +169,8 @@ void Game::render()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
draw_player_info();
|
||||||
|
|
||||||
SDL_Flip(display);
|
SDL_Flip(display);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -218,6 +220,19 @@ void Game::draw_node(GameVertex* v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Game::draw_player_info()
|
||||||
|
{
|
||||||
|
Player* player = data.get_turn();
|
||||||
|
|
||||||
|
DrawUtils::draw_text(display, player->get_name(), 680, display->h - 95,
|
||||||
|
font);
|
||||||
|
|
||||||
|
string temp = "Energy: ";
|
||||||
|
temp += itos(player->get_energy());
|
||||||
|
DrawUtils::draw_text(display, temp, 680, display->h - 80, font);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void Game::handle_button_press(ButtonAction action)
|
void Game::handle_button_press(ButtonAction action)
|
||||||
{
|
{
|
||||||
switch (action)
|
switch (action)
|
||||||
|
|
1
game.h
1
game.h
|
@ -41,6 +41,7 @@ class Game : public GameState
|
||||||
void draw_stats(GameVertex* v);
|
void draw_stats(GameVertex* v);
|
||||||
void draw_button(MenuButton* button);
|
void draw_button(MenuButton* button);
|
||||||
void draw_node(GameVertex* v);
|
void draw_node(GameVertex* v);
|
||||||
|
void draw_player_info();
|
||||||
|
|
||||||
void handle_button_press(ButtonAction action);
|
void handle_button_press(ButtonAction action);
|
||||||
|
|
||||||
|
|
66
gamedata.cpp
66
gamedata.cpp
|
@ -67,6 +67,7 @@ void GameData::toggle_turn()
|
||||||
{
|
{
|
||||||
mode = MODE_SELECT;
|
mode = MODE_SELECT;
|
||||||
build_type = VERTEX_NONE;
|
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));
|
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)
|
switch (mode)
|
||||||
{
|
{
|
||||||
case MODE_SELECT:
|
case MODE_SELECT:
|
||||||
|
@ -100,11 +99,15 @@ void GameData::handle_click(int x, int y)
|
||||||
break;
|
break;
|
||||||
case MODE_MOVE:
|
case MODE_MOVE:
|
||||||
if (current == NULL) return;
|
if (current == NULL) return;
|
||||||
if (MathUtils::distance(current->x, current->y, current->z, x, y, 0)
|
int dist = MathUtils::distance(current->x, current->y, current->z,
|
||||||
<= get_range())
|
x, y, 0);
|
||||||
|
if (dist <= get_range())
|
||||||
{
|
{
|
||||||
current->x = x;
|
if (turn->spend_energy(dist))
|
||||||
current->y = y;
|
{
|
||||||
|
current->x = x;
|
||||||
|
current->y = y;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
break;
|
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)
|
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);
|
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))
|
if (Graph::add_vertex(v))
|
||||||
{
|
{
|
||||||
toggle_turn();
|
if (turn->spend_energy(energy_cost))
|
||||||
return true;
|
{
|
||||||
|
toggle_turn();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else remove_vertex(v);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// really, we shouldn't be able to get here. return false just in case
|
|
||||||
delete v;
|
delete v;
|
||||||
return false;
|
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 (Graph::add_vertex(v, current))
|
||||||
{
|
{
|
||||||
|
if (!turn->spend_energy(energy_cost)) remove_vertex(v);
|
||||||
mode = MODE_SELECT;
|
mode = MODE_SELECT;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -245,3 +266,28 @@ bool GameData::endgame()
|
||||||
|
|
||||||
return false;
|
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);
|
||||||
|
}
|
||||||
|
|
|
@ -44,6 +44,10 @@ class GameData : public Graph
|
||||||
Player* get_turn() const { return turn; }
|
Player* get_turn() const { return turn; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
int num_vertices_by_type(VertexType type, Player* player = NULL);
|
||||||
|
void produce_energy();
|
||||||
|
|
||||||
|
|
||||||
GameVertex* current;
|
GameVertex* current;
|
||||||
Player player1, player2;
|
Player player1, player2;
|
||||||
Player* turn;
|
Player* turn;
|
||||||
|
|
|
@ -4,7 +4,7 @@ Player::Player(string name, unsigned int colour)
|
||||||
{
|
{
|
||||||
this->name = name;
|
this->name = name;
|
||||||
this->colour = colour;
|
this->colour = colour;
|
||||||
energy = 50;
|
energy = 25;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user