Now the buttons actually do something - also added a build_type

This commit is contained in:
Anna Rose 2011-07-03 10:12:47 -04:00
parent d04cde49c8
commit 793d6bd9a2
4 changed files with 67 additions and 15 deletions

View File

@ -20,6 +20,12 @@ Game::~Game()
SDL_FreeSurface(background); SDL_FreeSurface(background);
if (font != NULL) if (font != NULL)
TTF_CloseFont(font); TTF_CloseFont(font);
for (list<MenuButton*>::iterator cursor = buttons.begin();
cursor != buttons.end(); cursor++)
{
delete *cursor;
}
} }
@ -41,11 +47,12 @@ bool Game::init()
return false; return false;
} }
move_button = MenuButton("Move", font, 155, display->h - 95, BUTTON_MOVE); buttons.push_back(new MenuButton("Move", font, 155, display->h - 95,
attack_button = MenuButton("Attack", font, 260, display->h - 95, BUTTON_MOVE));
BUTTON_ATTACK); buttons.push_back(new MenuButton("Attack", font, 260, display->h - 95,
build_button = MenuButton("Build", font, 155, display->h - 50, BUTTON_ATTACK));
BUTTON_BUILD); buttons.push_back(new MenuButton("Build", font, 155, display->h - 50,
BUTTON_BUILD));
return GameState::init(); return GameState::init();
@ -130,9 +137,12 @@ void Game::render()
if (data.get_current_vertex() != NULL) if (data.get_current_vertex() != NULL)
{ {
draw_button(&move_button); for (list<MenuButton*>::iterator cursor = buttons.begin();
draw_button(&build_button); cursor != buttons.end(); cursor++)
draw_button(&attack_button); {
MenuButton* button = *cursor;
draw_button(button);
}
} }
SDL_Flip(display); SDL_Flip(display);
@ -156,6 +166,32 @@ void Game::draw_button(MenuButton* button)
} }
void Game::handle_button_press(ButtonAction action)
{
switch (action)
{
case BUTTON_BUILD:
data.set_mode(MODE_BUILD);
break;
case BUTTON_ATTACK:
data.set_mode(MODE_ATTACK);
break;
case BUTTON_MOVE:
data.set_mode(MODE_MOVE);
break;
case BUTTON_BUILD_ATTACKER:
data.set_build_type(VERTEX_ATTACKER);
break;
case BUTTON_BUILD_DEFENDER:
data.set_build_type(VERTEX_DEFENDER);
break;
case BUTTON_BUILD_PRODUCER:
data.set_build_type(VERTEX_PRODUCER);
break;
}
}
void Game::draw_stats(Vertex* v) void Game::draw_stats(Vertex* v)
{ {
int num_lines = 4; int num_lines = 4;
@ -181,8 +217,14 @@ void Game::draw_stats(Vertex* v)
void Game::on_lbutton_down(int x, int y) void Game::on_lbutton_down(int x, int y)
{ {
if (y > display->h - 110) return; for (list<MenuButton*>::iterator cursor = buttons.begin();
cursor != buttons.end(); cursor++)
{
MenuButton* button = *cursor;
if (button->is_at(x, y)) handle_button_press(button->get_action());
}
if (y > display->h - 110) return;
if (!data.endgame()) data.handle_click(x, y); if (!data.endgame()) data.handle_click(x, y);
} }

10
game.h
View File

@ -10,9 +10,11 @@
#include "gamedata.h" #include "gamedata.h"
#include "gamestate.h" #include "gamestate.h"
#include "menubutton.h" #include "menubutton.h"
#include <stack>
#include <SDL_ttf.h> #include <SDL_ttf.h>
#include <list>
#include <stack>
using std::list;
using std::stack; using std::stack;
@ -39,6 +41,8 @@ class Game : public GameState
void draw_stats(Vertex* v); void draw_stats(Vertex* v);
void draw_button(MenuButton* button); void draw_button(MenuButton* button);
void handle_button_press(ButtonAction action);
// data // data
GameData data; GameData data;
// the x,y position of the mouse cursor // the x,y position of the mouse cursor
@ -50,9 +54,7 @@ class Game : public GameState
TTF_Font* font; TTF_Font* font;
// menu buttons // menu buttons
MenuButton move_button; list<MenuButton*> buttons;
MenuButton build_button;
MenuButton attack_button;
static int NODE_RADIUS; static int NODE_RADIUS;

View File

@ -31,6 +31,7 @@ GameData::GameData()
player1 = Player("player 1", PLAYER1_COLOUR); player1 = Player("player 1", PLAYER1_COLOUR);
player2 = Player("player 2", PLAYER2_COLOUR); player2 = Player("player 2", PLAYER2_COLOUR);
turn = &player1; turn = &player1;
build_type = VERTEX_NONE;
} }
GameData::~GameData() { } GameData::~GameData() { }
@ -236,6 +237,9 @@ void GameData::set_mode(Mode m)
// Stay in MODE_SELECT (or maybe MODE_BUILD) when current is null // Stay in MODE_SELECT (or maybe MODE_BUILD) when current is null
if (current == NULL) return; if (current == NULL) return;
// If we're leaving MODE_BUILD, we should set the build_type back to none
if (mode == MODE_ATTACK && m != mode) build_type = VERTEX_NONE;
// The other modes all require current to match the player // The other modes all require current to match the player
if (dynamic_cast<GameVertex*>(current)->player == turn) mode = m; if (dynamic_cast<GameVertex*>(current)->player == turn) mode = m;
} }

View File

@ -10,7 +10,7 @@
#include "player.h" #include "player.h"
enum Mode {MODE_MOVE, MODE_ATTACK, MODE_BUILD, MODE_SELECT}; enum Mode {MODE_MOVE, MODE_ATTACK, MODE_BUILD, MODE_SELECT};
enum VertexType {VERTEX_ATTACKER, VERTEX_DEFENDER, VERTEX_PRODUCER}; enum VertexType {VERTEX_ATTACKER, VERTEX_DEFENDER, VERTEX_PRODUCER, VERTEX_NONE};
class GameVertex : public Vertex class GameVertex : public Vertex
{ {
@ -43,6 +43,8 @@ class GameData : public Graph
Mode get_mode() const { return mode; } Mode get_mode() const { return mode; }
void set_mode(Mode m); void set_mode(Mode m);
void set_build_type(VertexType type) { build_type = 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)
int get_range(Vertex* node = NULL); int get_range(Vertex* node = NULL);
@ -57,10 +59,12 @@ class GameData : public Graph
float calculate_strength_r(Vertex* node, unsigned int depth, list<Vertex*>& visited); float calculate_strength_r(Vertex* node, unsigned int depth, list<Vertex*>& visited);
Vertex* current; Vertex* current;
Mode mode;
Player player1, player2; Player player1, player2;
Player* turn; Player* turn;
Mode mode;
VertexType build_type;
static int PLAYER1_COLOUR; static int PLAYER1_COLOUR;
static int PLAYER2_COLOUR; static int PLAYER2_COLOUR;