From eccc773457a6f56894ea29c7318ca288124b46ca Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Wed, 6 Jul 2011 16:17:30 -0400 Subject: [PATCH] Added a 'ghost node' so players can see where their new nodes will go, and fixed some bugs in the process --- game.cpp | 22 +++++++++++++++++ gamedata.cpp | 67 +++++++++++++++++++++++++++++++++----------------- gamedata.h | 9 ++++--- gamevertex.cpp | 8 +++--- 4 files changed, 77 insertions(+), 29 deletions(-) diff --git a/game.cpp b/game.cpp index 9a98f39..a1be683 100644 --- a/game.cpp +++ b/game.cpp @@ -144,6 +144,27 @@ void Game::render() draw_stats(v); } + + // If applicable, draw a 'ghost node'... maybe at some point we can make + // this semi-transparent + GameVertex* current = data.get_current_vertex(true); + if (current != NULL && ((data.get_mode() == MODE_BUILD && + data.get_build_type() != VERTEX_NONE) || + (data.get_mode() == MODE_MOVE)) && + MathUtils::distance(current->x, current->y, 0, + cursor_x, cursor_y, 0) < range && + MathUtils::distance(current->x, current->y, 0, + cursor_x, cursor_y, 0) > current->r * 2) + { + GameVertex v(cursor_x, cursor_y, 0, current->r,current->colour, 0, + VERTEX_NONE, current->player); + + DrawUtils::draw_line(display, v.x, v.y, current->x, current->y, 2, + v.colour); + v.render(display); + } + + // Draw each node for (list::iterator cursor = vertices.begin(); cursor != vertices.end(); cursor++) @@ -151,6 +172,7 @@ void Game::render() dynamic_cast(*cursor)->render(display); } + // draw the rest of the bottom menu draw_menu_bars(); draw_player_info(); diff --git a/gamedata.cpp b/gamedata.cpp index a3364fc..91fbe29 100644 --- a/gamedata.cpp +++ b/gamedata.cpp @@ -143,22 +143,8 @@ bool GameData::select_vertex(int x, int y) bool GameData::add_vertex(int x, int y, int z, int r, int colour) { - 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: - if (num_vertices_by_type(build_type, turn) == 0) energy_cost = 0; - else energy_cost = 25 << (num_vertices_by_type(build_type, turn) - 1); - break; - } + int energy_cost = get_energy_cost(build_type); + if (!can_build(build_type)) return false; GameVertex* v = new GameVertex(x, y, z, r, colour, 10, build_type, turn); @@ -169,12 +155,9 @@ bool GameData::add_vertex(int x, int y, int z, int r, int colour) { if (Graph::add_vertex(v)) { - if (turn->spend_energy(energy_cost)) - { - toggle_turn(); - return true; - } - else remove_vertex(v); + turn->spend_energy(energy_cost); + toggle_turn(); + return true; } } @@ -199,7 +182,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); + turn->spend_energy(energy_cost); mode = MODE_SELECT; return true; } @@ -314,3 +297,41 @@ void GameData::produce_energy() int amount = 25 * num_vertices_by_type(VERTEX_PRODUCER, turn); turn->add_energy(amount); } + + +// Checks the energy cost, and returns true only if +// it is currently possible to build the selected +bool GameData::can_build(VertexType type) +{ + int cost = get_energy_cost(type); + if (cost > turn->get_energy() || cost == -1) return false; + return true; +} + + +int GameData::get_energy_cost(VertexType type) +{ + int energy_cost = -1; + + switch (type) + { + case VERTEX_ATTACKER: + energy_cost = 50; + break; + case VERTEX_DEFENDER: + energy_cost = 25; + break; + case VERTEX_PRODUCER: + if (num_vertices_by_type(type, turn) == 0) energy_cost = 0; + else energy_cost = 25 << (num_vertices_by_type(type, turn) - 1); + break; + } + + return energy_cost; +} + + +void GameData::set_build_type(VertexType type) +{ + if (can_build(type)) build_type = type; +} diff --git a/gamedata.h b/gamedata.h index 22521cf..31be14f 100644 --- a/gamedata.h +++ b/gamedata.h @@ -32,7 +32,7 @@ class GameData : public Graph void set_mode(Mode m); VertexType get_build_type() const { return build_type; } - void set_build_type(VertexType type) { build_type = type; } + void set_build_type(VertexType type); // returns the move/attack range for the specified node // (or the selected node if node == NULL) @@ -43,6 +43,10 @@ class GameData : public Graph void toggle_turn(); Player* get_turn() const { return turn; } + bool can_build(VertexType type); + int get_energy_cost(VertexType type); + + private: int num_vertices_by_type(VertexType type, Player* player = NULL); void produce_energy(); @@ -57,9 +61,8 @@ class GameData : public Graph static int PLAYER1_COLOUR; static int PLAYER2_COLOUR; - - static int BASE_BUILD_RADIUS; static int NODE_RADIUS; + static int BASE_BUILD_RADIUS; }; #endif diff --git a/gamevertex.cpp b/gamevertex.cpp index f690bf2..686d272 100644 --- a/gamevertex.cpp +++ b/gamevertex.cpp @@ -160,12 +160,14 @@ void GameVertex::render(SDL_Surface* display) case VERTEX_PRODUCER: icon = producer_icon; break; + default: + icon = NULL; } DrawUtils::draw_circle_filled(display, x, y, r, colour); - DrawUtils::draw_text(display, itos(score), x, y, font, - 0x00ff00, true, true); + if (score > 0) DrawUtils::draw_text(display, itos(score), x, y, font, + 0x00ff00, true, true); - DrawUtils::draw(display, icon, x + 5, y + 5); + if (icon != NULL) DrawUtils::draw(display, icon, x + 5, y + 5); }