Continuing to refactor code to make room for larger features. Currently, attacking doesn't work

This commit is contained in:
Anna Rose 2011-07-01 18:23:01 -04:00
parent c49fdb8169
commit bf151a928a
3 changed files with 26 additions and 13 deletions

View File

@ -151,5 +151,5 @@ void Game::on_key_down(SDLKey sym, SDLMod mod, Uint16 unicode)
if (sym == SDLK_a) data.set_mode(MODE_ATTACK);
if (sym == SDLK_m) data.set_mode(MODE_MOVE);
if (sym == SDLK_b) data.set_mode(MODE_BUILD);
if (sym == SDLK_s) data.set_mode(MODE_SELECT);
if (sym == SDLK_s || sym == SDLK_ESCAPE) data.set_mode(MODE_SELECT);
}

View File

@ -42,19 +42,18 @@ Vertex* GameData::get_current_vertex(bool only_mine) const
void GameData::toggle_turn()
{
if (!turn->has_played())
{
set_mode(MODE_BUILD);
turn->set_played();
}
else set_mode(MODE_SELECT);
if (!turn->has_played()) turn->set_played();
if (!endgame())
{
if (turn == &player1) turn = &player2;
else if (turn == &player2) turn = &player1;
if (!turn->has_played()) mode = MODE_BUILD;
else mode = MODE_SELECT;
}
current = NULL;
}
@ -67,12 +66,12 @@ void GameData::handle_click(int x, int y)
if (mode == MODE_SELECT)
{
if (point_in_vertex(x, y, 0)) select_vertex(x, y);
// select_vertex handles making sure a point exists at (x,y)
select_vertex(x, y);
}
else if (mode == MODE_BUILD)
{
if (point_in_vertex(x, y, 0)) select_vertex(x, y, true);
else add_vertex(x, y, 0, r, colour);
add_vertex(x, y, 0, r, colour);
}
else if (mode == MODE_ATTACK)
{
@ -116,11 +115,18 @@ bool GameData::add_vertex(int x, int y, int z, int r, int colour)
return true;
}
}
// really, we shouldn't be able to get here. return false just in case
return false;
}
// same here - just a logic check
if (current->colour != turn->get_colour()) return false;
// This is the range check...
if (MathUtils::distance(current->x, current->y, 0, x, y, 0) > get_range())
return false;
if (Graph::add_vertex(x, y, z, r, colour, 10, current))
{
#ifdef DEBUG
@ -191,9 +197,16 @@ float GameData::calculate_strength_r(Vertex* node, unsigned int depth, list<Vert
}
Mode GameData::set_mode(Mode m)
// This class contains logic checks to keep the mode aligned with
// what is reasonable. Special cases inside the GameData class should just
// do mode = MODE_<whatever>
void GameData::set_mode(Mode m)
{
mode = m;
// Stay in MODE_SELECT (or maybe MODE_BUILD) when current is null
if (current == NULL) return;
// The other modes all require current to match the player
if (current->colour == turn->get_colour()) mode = m;
}

View File

@ -38,7 +38,7 @@ class GameData : public Graph
bool add_vertex(int x, int y, int z, int r, int colour);
Mode get_mode() const { return mode; }
Mode set_mode(Mode m);
void set_mode(Mode m);
// returns the move/attack range for the specified node
// (or the selected node if node == NULL)