diff --git a/game.cpp b/game.cpp index a8673db..acdd0c3 100644 --- a/game.cpp +++ b/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) { GameVertex* v = data.get_current_vertex(); @@ -169,6 +169,8 @@ void Game::render() } } + draw_player_info(); + 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) { switch (action) diff --git a/game.h b/game.h index bd67e1f..2e00db4 100644 --- a/game.h +++ b/game.h @@ -41,6 +41,7 @@ class Game : public GameState void draw_stats(GameVertex* v); void draw_button(MenuButton* button); void draw_node(GameVertex* v); + void draw_player_info(); void handle_button_press(ButtonAction action); diff --git a/gamedata.cpp b/gamedata.cpp index ab9a856..b170161 100644 --- a/gamedata.cpp +++ b/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(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::iterator cursor = vertices.begin(); + cursor != vertices.end(); cursor++) + { + GameVertex* v = dynamic_cast(*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); +} diff --git a/gamedata.h b/gamedata.h index be1f30c..22521cf 100644 --- a/gamedata.h +++ b/gamedata.h @@ -44,6 +44,10 @@ class GameData : public Graph Player* get_turn() const { return turn; } private: + int num_vertices_by_type(VertexType type, Player* player = NULL); + void produce_energy(); + + GameVertex* current; Player player1, player2; Player* turn; diff --git a/player.cpp b/player.cpp index cd13d4c..e6f9812 100644 --- a/player.cpp +++ b/player.cpp @@ -4,7 +4,7 @@ Player::Player(string name, unsigned int colour) { this->name = name; this->colour = colour; - energy = 50; + energy = 25; }