Added a 'ghost node' so players can see where their new nodes will go, and fixed some bugs in the process

This commit is contained in:
Anna Rose 2011-07-06 16:17:30 -04:00
parent 78409c80d7
commit eccc773457
4 changed files with 77 additions and 29 deletions

View File

@ -144,6 +144,27 @@ void Game::render()
draw_stats(v); 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 // Draw each node
for (list<Vertex*>::iterator cursor = vertices.begin(); for (list<Vertex*>::iterator cursor = vertices.begin();
cursor != vertices.end(); cursor++) cursor != vertices.end(); cursor++)
@ -151,6 +172,7 @@ void Game::render()
dynamic_cast<GameVertex*>(*cursor)->render(display); dynamic_cast<GameVertex*>(*cursor)->render(display);
} }
// draw the rest of the bottom menu // draw the rest of the bottom menu
draw_menu_bars(); draw_menu_bars();
draw_player_info(); draw_player_info();

View File

@ -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) bool GameData::add_vertex(int x, int y, int z, int r, int colour)
{ {
int energy_cost = 0; int energy_cost = get_energy_cost(build_type);
switch (build_type) if (!can_build(build_type)) return false;
{
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;
}
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);
@ -169,12 +155,9 @@ bool GameData::add_vertex(int x, int y, int z, int r, int colour)
{ {
if (Graph::add_vertex(v)) if (Graph::add_vertex(v))
{ {
if (turn->spend_energy(energy_cost)) turn->spend_energy(energy_cost);
{ toggle_turn();
toggle_turn(); return true;
return true;
}
else remove_vertex(v);
} }
} }
@ -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 (Graph::add_vertex(v, current))
{ {
if (!turn->spend_energy(energy_cost)) remove_vertex(v); turn->spend_energy(energy_cost);
mode = MODE_SELECT; mode = MODE_SELECT;
return true; return true;
} }
@ -314,3 +297,41 @@ void GameData::produce_energy()
int amount = 25 * num_vertices_by_type(VERTEX_PRODUCER, turn); int amount = 25 * num_vertices_by_type(VERTEX_PRODUCER, turn);
turn->add_energy(amount); 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;
}

View File

@ -32,7 +32,7 @@ class GameData : public Graph
void set_mode(Mode m); void set_mode(Mode m);
VertexType get_build_type() const { return build_type; } 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 // returns the move/attack range for the specified node
// (or the selected node if node == NULL) // (or the selected node if node == NULL)
@ -43,6 +43,10 @@ class GameData : public Graph
void toggle_turn(); void toggle_turn();
Player* get_turn() const { return turn; } Player* get_turn() const { return turn; }
bool can_build(VertexType type);
int get_energy_cost(VertexType type);
private: private:
int num_vertices_by_type(VertexType type, Player* player = NULL); int num_vertices_by_type(VertexType type, Player* player = NULL);
void produce_energy(); void produce_energy();
@ -57,9 +61,8 @@ class GameData : public Graph
static int PLAYER1_COLOUR; static int PLAYER1_COLOUR;
static int PLAYER2_COLOUR; static int PLAYER2_COLOUR;
static int BASE_BUILD_RADIUS;
static int NODE_RADIUS; static int NODE_RADIUS;
static int BASE_BUILD_RADIUS;
}; };
#endif #endif

View File

@ -160,12 +160,14 @@ void GameVertex::render(SDL_Surface* display)
case VERTEX_PRODUCER: case VERTEX_PRODUCER:
icon = producer_icon; icon = producer_icon;
break; break;
default:
icon = NULL;
} }
DrawUtils::draw_circle_filled(display, x, y, r, colour); DrawUtils::draw_circle_filled(display, x, y, r, colour);
DrawUtils::draw_text(display, itos(score), x, y, font, if (score > 0) DrawUtils::draw_text(display, itos(score), x, y, font,
0x00ff00, true, true); 0x00ff00, true, true);
DrawUtils::draw(display, icon, x + 5, y + 5); if (icon != NULL) DrawUtils::draw(display, icon, x + 5, y + 5);
} }