From bf151a928acd86b7ebde164df67ce97a07bdb7f0 Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Fri, 1 Jul 2011 18:23:01 -0400 Subject: [PATCH] Continuing to refactor code to make room for larger features. Currently, attacking doesn't work --- game.cpp | 2 +- gamedata.cpp | 35 ++++++++++++++++++++++++----------- gamedata.h | 2 +- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/game.cpp b/game.cpp index 3afcf5a..dc93b6b 100644 --- a/game.cpp +++ b/game.cpp @@ -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); } diff --git a/gamedata.cpp b/gamedata.cpp index 0da795c..023f50d 100644 --- a/gamedata.cpp +++ b/gamedata.cpp @@ -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 +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; } diff --git a/gamedata.h b/gamedata.h index a80143b..25a4144 100644 --- a/gamedata.h +++ b/gamedata.h @@ -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)