From e221f1990c7a6f6dd4a40c0dc8c6a5198d061156 Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Fri, 1 Jul 2011 20:00:19 -0400 Subject: [PATCH] Added player names and some debug stuff --- game.cpp | 24 ++++++++++++++++++------ game.h | 4 ++++ gamedata.cpp | 35 ++++++++++++++++++++++++----------- gamedata.h | 12 +++--------- player.cpp | 3 ++- player.h | 7 ++++++- 6 files changed, 57 insertions(+), 28 deletions(-) diff --git a/game.cpp b/game.cpp index dc93b6b..6162b65 100644 --- a/game.cpp +++ b/game.cpp @@ -123,8 +123,8 @@ void Game::draw_stats(Vertex* v) DrawUtils::draw_text(display, itos(data.calculate_strength(v)), x + 50, y + 14, font); - DrawUtils::draw_text(display, "def:", x, y + 28, font); - DrawUtils::draw_text(display, itos(data.calculate_strength(v)), + DrawUtils::draw_text(display, "armor:", x, y + 28, font); + DrawUtils::draw_text(display, itos(data.calculate_armor(v)), x + 50, y + 28, font); DrawUtils::draw_text(display, "hp:", x, y + 42, font); @@ -148,8 +148,20 @@ void Game::on_rbutton_down(int mX, int mY) void Game::on_key_down(SDLKey sym, SDLMod mod, Uint16 unicode) { if (sym == SDLK_q && mod & KMOD_CTRL) throw StateExit(); - 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 || sym == SDLK_ESCAPE) data.set_mode(MODE_SELECT); + else if (sym == SDLK_a) data.set_mode(MODE_ATTACK); + else if (sym == SDLK_m) data.set_mode(MODE_MOVE); + else if (sym == SDLK_b) data.set_mode(MODE_BUILD); + else if (sym == SDLK_s || sym == SDLK_ESCAPE) data.set_mode(MODE_SELECT); + +#ifdef DEBUG + if (sym == SDLK_d && mod & (KMOD_ALT | KMOD_CTRL)) print_debug_info(); +#endif } + +#ifdef DEBUG +void Game::print_debug_info() +{ + fprintf(stderr, "Mode: %d\n", data.get_mode()); + fprintf(stderr, "Turn: %s\n", data.get_turn()->get_name().c_str()); +} +#endif diff --git a/game.h b/game.h index fd5e3eb..b6b53d1 100644 --- a/game.h +++ b/game.h @@ -43,6 +43,10 @@ class Game : public GameState // surfaces containing textures to draw SDL_Surface* background; TTF_Font* font; + +#ifdef DEBUG + void print_debug_info(); +#endif }; #endif diff --git a/gamedata.cpp b/gamedata.cpp index 023f50d..a8d7250 100644 --- a/gamedata.cpp +++ b/gamedata.cpp @@ -13,13 +13,14 @@ int GameData::PLAYER2_COLOUR = 0x090c7a; int GameData::BASE_BUILD_RADIUS = 75; int GameData::NODE_RADIUS = 10; + GameData::GameData() : Graph(true) { current = NULL; mode = MODE_BUILD; - player1 = Player(PLAYER1_COLOUR); - player2 = Player(PLAYER2_COLOUR); + player1 = Player("player 1", PLAYER1_COLOUR); + player2 = Player("player 2", PLAYER2_COLOUR); turn = &player1; } @@ -30,7 +31,7 @@ Vertex* GameData::get_current_vertex(bool only_mine) const { if (only_mine) { - if (current != NULL && + if (current != NULL && current->colour == turn->get_colour()) return current; return NULL; @@ -40,6 +41,13 @@ Vertex* GameData::get_current_vertex(bool only_mine) const } +void GameData::clear_current_vertex() +{ + mode = MODE_SELECT; + current = NULL; +} + + void GameData::toggle_turn() { if (!turn->has_played()) turn->set_played(); @@ -75,9 +83,8 @@ void GameData::handle_click(int x, int y) } else if (mode == MODE_ATTACK) { - if (select_vertex(x, y, true)) return; Vertex* v = vertex_at(x, y, 0); - if (v == NULL) return; + if (v == NULL || v->colour == turn->get_colour()) return; if (v->colour != colour) attack_vertex(v); } } @@ -141,6 +148,13 @@ bool GameData::add_vertex(int x, int y, int z, int r, int colour) } +float GameData::calculate_armor(Vertex* node) +{ + float str = calculate_strength(node); + return str / 10; +} + + float GameData::calculate_strength(Vertex* node) { list visited; @@ -204,7 +218,7 @@ void GameData::set_mode(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; } @@ -238,18 +252,17 @@ int GameData::get_range(Vertex* node) void GameData::attack_vertex(Vertex* target) { - float atk_str = calculate_strength(current); - float def_str = calculate_strength(target); - float armor = def_str / 10; // how much energy it takes to deal 1 damage + float atk = calculate_strength(current); + float armor = calculate_armor(current); - int damage = (int)(atk_str / armor); + int damage = (int)(atk / armor); target->score -= damage; if (target->score <= 0) remove_vertex(target); #ifdef DEBUG - fprintf(stderr, "debug: GameData::attack_vertex(): atk_str=%.2f, def_str=%.2f, armor=%.2f, damage=%d\n", atk_str, def_str, armor, damage); + fprintf(stderr, "debug: GameData::attack_vertex(): atk=%.2f, armor=%.2f, armor=%.2f, damage=%d\n", atk, armor, damage); #endif toggle_turn(); diff --git a/gamedata.h b/gamedata.h index 25a4144..98ab615 100644 --- a/gamedata.h +++ b/gamedata.h @@ -10,14 +10,7 @@ #include "player.h" enum Mode {MODE_MOVE, MODE_ATTACK, MODE_BUILD, MODE_SELECT}; -enum VertexType {ATTACKER, DEFENDER, PRODUCER}; - -class GameVertex : public Vertex -{ - public: - VertexType type; -}; - +enum VertexType {VERTEX_ATTACKER, VERTEX_DEFENDER, VERTEX_PRODUCER}; class GameData : public Graph { @@ -26,7 +19,7 @@ class GameData : public Graph ~GameData(); Vertex* get_current_vertex(bool only_mine = false) const; - void clear_current_vertex() { current = NULL; } + void clear_current_vertex(); void toggle_turn(); @@ -48,6 +41,7 @@ class GameData : public Graph bool endgame(); Player* get_turn() const { return turn; } float calculate_strength(Vertex* node); + float calculate_armor(Vertex* node); private: float calculate_strength_r(Vertex* node, unsigned int depth, list& visited); diff --git a/player.cpp b/player.cpp index 5224d29..cd13d4c 100644 --- a/player.cpp +++ b/player.cpp @@ -1,7 +1,8 @@ #include "player.h" -Player::Player(unsigned int colour) +Player::Player(string name, unsigned int colour) { + this->name = name; this->colour = colour; energy = 50; } diff --git a/player.h b/player.h index a5d769c..44d8b26 100644 --- a/player.h +++ b/player.h @@ -4,13 +4,17 @@ #ifndef _PLAYER_H_ #define _PLAYER_H_ +#include +using std::string; + class Player { public: - Player(unsigned int colour = 0x000000); + Player(string name = "player", unsigned int colour = 0x000000); unsigned int get_energy() const { return energy; } unsigned int get_colour() const { return colour; } + string get_name() const { return name; } void add_energy(unsigned int amount); bool spend_energy(unsigned int amount); @@ -22,6 +26,7 @@ class Player unsigned int energy; unsigned int colour; bool played; + string name; }; #endif