From fc0c0f26fdee9b81aebb493dc96420c9877cacb8 Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Sun, 3 Jul 2011 15:16:00 -0400 Subject: [PATCH] Added ability to actually build different unit types, as well as icons to distinguish them --- drawutils.cpp | 5 +- game.cpp | 70 +++++++++++++++-------- game.h | 4 ++ gamedata.cpp | 30 ++++++---- gamedata.h | 2 +- res/attacker_icon.bmp | Bin 0 -> 822 bytes background.bmp => res/background.bmp | Bin res/defender_icon.bmp | Bin 0 -> 822 bytes res/producer_icon.bmp | Bin 0 -> 822 bytes title_banner.bmp => res/title_banner.bmp | Bin 10 files changed, 75 insertions(+), 36 deletions(-) create mode 100644 res/attacker_icon.bmp rename background.bmp => res/background.bmp (100%) create mode 100644 res/defender_icon.bmp create mode 100644 res/producer_icon.bmp rename title_banner.bmp => res/title_banner.bmp (100%) diff --git a/drawutils.cpp b/drawutils.cpp index 2559368..56cc926 100644 --- a/drawutils.cpp +++ b/drawutils.cpp @@ -7,7 +7,10 @@ SDL_Surface* DrawUtils::load(string file) SDL_Surface* raw = NULL; SDL_Surface* cooked = NULL; - raw = SDL_LoadBMP(file.c_str()); + string full_path = "res/"; + full_path += file; + + raw = SDL_LoadBMP(full_path.c_str()); if (raw == NULL) return NULL; cooked = SDL_DisplayFormat(raw); diff --git a/game.cpp b/game.cpp index 9bb6c6e..bfe7aaa 100644 --- a/game.cpp +++ b/game.cpp @@ -11,6 +11,10 @@ Game::Game(stack* state_stack, SDL_Surface* display) { background = NULL; font = NULL; + + attacker_icon = NULL; + defender_icon = NULL; + producer_icon = NULL; } @@ -32,21 +36,22 @@ Game::~Game() bool Game::init() { background = DrawUtils::load("background.bmp"); - - if (background == NULL) - { - debug("Game::init(): error: Couldn't load background image"); - return false; - } - font = TTF_OpenFont("LiberationSans-Regular.ttf", 12); + attacker_icon = DrawUtils::load("attacker_icon.bmp"); + defender_icon = DrawUtils::load("defender_icon.bmp"); + producer_icon = DrawUtils::load("producer_icon.bmp"); - if (font == NULL) + if (background == NULL || font == NULL || attacker_icon == NULL || + defender_icon == NULL || producer_icon == NULL) { - debug("Game::init(): error: Couldn't load font"); + debug("Game::init(): error: Couldn't load some resource(s)"); return false; } + DrawUtils::transpare(attacker_icon); + DrawUtils::transpare(defender_icon); + DrawUtils::transpare(producer_icon); + int row1 = display->h - 95; int row2 = display->h - 50; int col1 = 155; @@ -102,13 +107,11 @@ void Game::render() range_colour); } - // Now paint each vertex, and any edges that it needs + // First draw the edges, so that they don't obscure any data for (list::iterator cursor = vertices.begin(); cursor != vertices.end(); cursor++) { Vertex* v = *cursor; - DrawUtils::draw_circle_filled(display, v->x, v->y, v->r, - v->colour); for (list::iterator subcursor = v->neighbors.begin(); subcursor != v->neighbors.end(); subcursor++) @@ -119,16 +122,6 @@ void Game::render() } } - // Add hit points on top of the nodes - for (list::iterator cursor = vertices.begin(); - cursor != vertices.end(); cursor++) - { - Vertex* v = *cursor; - - DrawUtils::draw_text(display, itos(v->score), v->x, v->y, font, - 0x00ff00, true, true); - } - // ring the selected vertex and write info about it if (data.get_current_vertex() != NULL) { @@ -137,6 +130,14 @@ void Game::render() draw_stats(v); } + // Draw each node + for (list::iterator cursor = vertices.begin(); + cursor != vertices.end(); cursor++) + { + Vertex* v = *cursor; + draw_node(v); + } + // draw the rest of the bottom menu // horizontal line across the whole thing @@ -190,6 +191,29 @@ void Game::draw_button(MenuButton* button) } +void Game::draw_node(Vertex* v) +{ + DrawUtils::draw_circle_filled(display, v->x, v->y, v->r, + v->colour); + + DrawUtils::draw_text(display, itos(v->score), v->x, v->y, font, + 0x00ff00, true, true); + + switch (dynamic_cast(v)->type) + { + case VERTEX_ATTACKER: + DrawUtils::draw(display, attacker_icon, v->x + 5, v-> y + 5); + break; + case VERTEX_DEFENDER: + DrawUtils::draw(display, defender_icon, v->x + 5, v-> y + 5); + break; + case VERTEX_PRODUCER: + DrawUtils::draw(display, producer_icon, v->x + 5, v-> y + 5); + break; + } +} + + void Game::handle_button_press(ButtonAction action) { switch (action) @@ -225,7 +249,7 @@ void Game::draw_stats(Vertex* v) DrawUtils::draw_text(display, "player:", x, y, font); DrawUtils::draw_text(display, dynamic_cast(v)->player->get_name(), x + 50, y, font); - DrawUtils::draw_text(display, "str:", x, y + 14, font); + DrawUtils::draw_text(display, "atk:", x, y + 14, font); DrawUtils::draw_text(display, itos(data.calculate_strength(v)), x + 50, y + 14, font); diff --git a/game.h b/game.h index 6e2b8ba..f341399 100644 --- a/game.h +++ b/game.h @@ -40,6 +40,7 @@ class Game : public GameState private: void draw_stats(Vertex* v); void draw_button(MenuButton* button); + void draw_node(Vertex* v); void handle_button_press(ButtonAction action); @@ -52,6 +53,9 @@ class Game : public GameState // surfaces containing textures to draw SDL_Surface* background; TTF_Font* font; + SDL_Surface* attacker_icon; + SDL_Surface* defender_icon; + SDL_Surface* producer_icon; // menu buttons list buttons; diff --git a/gamedata.cpp b/gamedata.cpp index 98fa5df..48e7ba2 100644 --- a/gamedata.cpp +++ b/gamedata.cpp @@ -31,7 +31,7 @@ GameData::GameData() player1 = Player("player 1", PLAYER1_COLOUR); player2 = Player("player 2", PLAYER2_COLOUR); turn = &player1; - build_type = VERTEX_NONE; + build_type = VERTEX_PRODUCER; // first vertex is always a producer } GameData::~GameData() { } @@ -67,8 +67,16 @@ void GameData::toggle_turn() if (turn == &player1) turn = &player2; else if (turn == &player2) turn = &player1; - if (!turn->has_played()) mode = MODE_BUILD; - else mode = MODE_SELECT; + if (!turn->has_played()) + { + mode = MODE_BUILD; + build_type = VERTEX_PRODUCER; + } + else + { + mode = MODE_SELECT; + build_type = VERTEX_NONE; + } } current = NULL; @@ -125,8 +133,9 @@ bool GameData::select_vertex(int x, int y) bool GameData::add_vertex(int x, int y, int z, int r, int colour) { - GameVertex* v = new GameVertex(x, y, z, r, colour, 10, VERTEX_ATTACKER, - turn); + if (build_type == VERTEX_NONE) return false; + + GameVertex* v = new GameVertex(x, y, z, r, colour, 10, build_type, turn); if (current == NULL) { @@ -182,7 +191,9 @@ 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 armor = str / 10; + if (armor < 1) armor = 1; + return armor; } @@ -287,16 +298,13 @@ int GameData::get_range(Vertex* node) void GameData::attack_vertex(Vertex* target) { float atk = calculate_strength(current); - float armor = calculate_armor(current); - + float armor = calculate_armor(target); int damage = (int)(atk / armor); - target->score -= damage; - if (target->score <= 0) remove_vertex(target); #ifdef DEBUG - fprintf(stderr, "debug: GameData::attack_vertex(): atk=%.2f, armor=%.2f, armor=%.2f, damage=%d\n", atk, armor, damage); + fprintf(stderr, "debug: GameData::attack_vertex(): atk=%.2f, armor=%.2f, damage=%d\n", atk, armor, damage); #endif toggle_turn(); diff --git a/gamedata.h b/gamedata.h index 0d2c3da..5b543e3 100644 --- a/gamedata.h +++ b/gamedata.h @@ -17,7 +17,7 @@ class GameVertex : public Vertex { public: GameVertex(int x, int y, int z, int r, int colour = 0, int score = 0, - VertexType type = VERTEX_ATTACKER, Player* player = NULL); + VertexType type = VERTEX_NONE, Player* player = NULL); VertexType type; Player* player; diff --git a/res/attacker_icon.bmp b/res/attacker_icon.bmp new file mode 100644 index 0000000000000000000000000000000000000000..7a7149b12254364699532c4741afcc495ceb25f8 GIT binary patch literal 822 zcmZ?rHDhJ~12Z700mK4O%*Y@C7H0s;3v)v-M1X