2011-06-28 02:07:52 +00:00
|
|
|
#include "game.h"
|
|
|
|
#include "mathutils.h"
|
2011-06-28 02:35:52 +00:00
|
|
|
#include "drawutils.h"
|
2011-06-28 02:07:52 +00:00
|
|
|
#include "debug.h"
|
2011-07-01 17:15:30 +00:00
|
|
|
#include "itos.h"
|
2011-06-28 02:07:52 +00:00
|
|
|
#include <SDL.h>
|
|
|
|
|
2011-06-28 02:35:52 +00:00
|
|
|
|
2011-06-30 21:21:46 +00:00
|
|
|
Game::Game(stack<GameState*>* state_stack, SDL_Surface* display)
|
|
|
|
: GameState(state_stack, display)
|
2011-06-28 02:35:52 +00:00
|
|
|
{
|
|
|
|
background = NULL;
|
2011-07-01 17:15:30 +00:00
|
|
|
font = NULL;
|
2011-06-28 02:35:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-28 02:07:52 +00:00
|
|
|
Game::~Game()
|
|
|
|
{
|
2011-06-29 01:44:28 +00:00
|
|
|
if (background != NULL)
|
|
|
|
SDL_FreeSurface(background);
|
2011-07-01 17:15:30 +00:00
|
|
|
if (font != NULL)
|
|
|
|
TTF_CloseFont(font);
|
2011-07-03 14:12:47 +00:00
|
|
|
|
|
|
|
for (list<MenuButton*>::iterator cursor = buttons.begin();
|
|
|
|
cursor != buttons.end(); cursor++)
|
|
|
|
{
|
|
|
|
delete *cursor;
|
|
|
|
}
|
2011-06-28 02:07:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Game::init()
|
|
|
|
{
|
2011-06-28 02:35:52 +00:00
|
|
|
background = DrawUtils::load("background.bmp");
|
|
|
|
|
|
|
|
if (background == NULL)
|
|
|
|
{
|
2011-06-29 02:14:55 +00:00
|
|
|
debug("Game::init(): error: Couldn't load background image");
|
2011-06-28 02:35:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-07-01 17:15:30 +00:00
|
|
|
font = TTF_OpenFont("LiberationSans-Regular.ttf", 12);
|
|
|
|
|
|
|
|
if (font == NULL)
|
|
|
|
{
|
|
|
|
debug("Game::init(): error: Couldn't load font");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-07-03 14:12:47 +00:00
|
|
|
buttons.push_back(new MenuButton("Move", font, 155, display->h - 95,
|
|
|
|
BUTTON_MOVE));
|
|
|
|
buttons.push_back(new MenuButton("Attack", font, 260, display->h - 95,
|
|
|
|
BUTTON_ATTACK));
|
|
|
|
buttons.push_back(new MenuButton("Build", font, 155, display->h - 50,
|
|
|
|
BUTTON_BUILD));
|
2011-07-02 21:34:11 +00:00
|
|
|
|
|
|
|
|
2011-07-01 15:40:38 +00:00
|
|
|
return GameState::init();
|
2011-06-28 02:35:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Game::render()
|
|
|
|
{
|
|
|
|
int range = data.get_range();
|
|
|
|
int range_colour = 0x888888;
|
|
|
|
|
|
|
|
switch(data.get_mode())
|
|
|
|
{
|
2011-07-01 18:10:45 +00:00
|
|
|
case MODE_BUILD:
|
2011-06-28 02:35:52 +00:00
|
|
|
range_colour = 0x0000ff;
|
|
|
|
break;
|
2011-07-01 18:10:45 +00:00
|
|
|
case MODE_MOVE:
|
|
|
|
range_colour = 0x00ff00;
|
|
|
|
break;
|
2011-06-28 02:35:52 +00:00
|
|
|
case MODE_ATTACK:
|
|
|
|
range_colour = 0xff0000;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Background image first
|
|
|
|
DrawUtils::draw(display, background, 0, 0);
|
|
|
|
|
|
|
|
list<Vertex*> vertices = data.get_vertices();
|
|
|
|
|
|
|
|
// Now paint on the targeting circle
|
2011-07-01 18:10:45 +00:00
|
|
|
if (data.get_current_vertex(true) != NULL)
|
2011-06-28 02:35:52 +00:00
|
|
|
{
|
|
|
|
Vertex* v = data.get_current_vertex();
|
|
|
|
DrawUtils::draw_circle_filled(display, v->x, v->y, range,
|
|
|
|
range_colour);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now paint each vertex, and any edges that it needs
|
|
|
|
for (list<Vertex*>::iterator cursor = vertices.begin();
|
|
|
|
cursor != vertices.end(); cursor++)
|
|
|
|
{
|
|
|
|
Vertex* v = *cursor;
|
|
|
|
DrawUtils::draw_circle_filled(display, v->x, v->y, v->r,
|
|
|
|
v->colour);
|
2011-07-01 17:15:30 +00:00
|
|
|
|
2011-06-28 02:35:52 +00:00
|
|
|
for (list<Vertex*>::iterator subcursor = v->neighbors.begin();
|
|
|
|
subcursor != v->neighbors.end(); subcursor++)
|
|
|
|
{
|
|
|
|
Vertex* v1 = *subcursor;
|
|
|
|
DrawUtils::draw_line(display, v->x, v->y, v1->x, v1->y, 2,
|
|
|
|
v->colour);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-02 02:23:16 +00:00
|
|
|
// Add hit points on top of the nodes
|
|
|
|
for (list<Vertex*>::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);
|
|
|
|
}
|
|
|
|
|
2011-07-02 03:47:24 +00:00
|
|
|
// ring the selected vertex and write info about it
|
2011-07-01 21:44:43 +00:00
|
|
|
if (data.get_current_vertex() != NULL)
|
|
|
|
{
|
|
|
|
Vertex* v = data.get_current_vertex();
|
|
|
|
DrawUtils::draw_circle(display, v->x, v->y, v->r + 5, 0x000000);
|
2011-07-01 22:04:15 +00:00
|
|
|
draw_stats(v);
|
2011-07-01 21:44:43 +00:00
|
|
|
}
|
|
|
|
|
2011-07-02 03:47:24 +00:00
|
|
|
// draw the rest of the bottom menu
|
2011-07-02 21:34:11 +00:00
|
|
|
|
|
|
|
// horizontal line across the whole thing
|
2011-07-02 03:47:24 +00:00
|
|
|
DrawUtils::draw_line(display, 0, display->h - 100,
|
|
|
|
display->w, display->h - 100, 2, 0x000000);
|
2011-07-02 21:34:11 +00:00
|
|
|
|
|
|
|
// vertical line to separate info pane from button pane
|
2011-07-02 03:47:24 +00:00
|
|
|
DrawUtils::draw_line(display, 150, display->h - 100, 150, display->h, 2,
|
|
|
|
0x000000);
|
|
|
|
|
2011-07-02 21:34:11 +00:00
|
|
|
if (data.get_current_vertex() != NULL)
|
|
|
|
{
|
2011-07-03 14:12:47 +00:00
|
|
|
for (list<MenuButton*>::iterator cursor = buttons.begin();
|
|
|
|
cursor != buttons.end(); cursor++)
|
|
|
|
{
|
|
|
|
MenuButton* button = *cursor;
|
|
|
|
draw_button(button);
|
|
|
|
}
|
2011-07-02 21:34:11 +00:00
|
|
|
}
|
|
|
|
|
2011-06-28 02:35:52 +00:00
|
|
|
SDL_Flip(display);
|
2011-06-28 02:07:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-02 21:34:11 +00:00
|
|
|
void Game::draw_button(MenuButton* button)
|
|
|
|
{
|
|
|
|
int colour = 0x000000;
|
|
|
|
ButtonAction action = button->get_action();
|
|
|
|
Mode mode = data.get_mode();
|
|
|
|
|
|
|
|
// fixme - there's really got to be a better way...
|
|
|
|
if ((action == BUTTON_BUILD && mode == MODE_BUILD) ||
|
|
|
|
(action == BUTTON_ATTACK && mode == MODE_ATTACK) ||
|
|
|
|
(action == BUTTON_MOVE && mode == MODE_MOVE))
|
|
|
|
colour = 0x0000ff;
|
|
|
|
else if (button->is_at(cursor_x, cursor_y)) colour = 0xff0000;
|
|
|
|
|
|
|
|
button->draw(display, colour);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-03 14:12:47 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-01 22:04:15 +00:00
|
|
|
void Game::draw_stats(Vertex* v)
|
|
|
|
{
|
|
|
|
int num_lines = 4;
|
|
|
|
int x = 20;
|
2011-07-02 03:47:24 +00:00
|
|
|
int y = display->h - 76;
|
2011-07-01 22:04:15 +00:00
|
|
|
|
|
|
|
DrawUtils::draw_text(display, "player:", x, y, font);
|
2011-07-02 03:24:41 +00:00
|
|
|
DrawUtils::draw_text(display, dynamic_cast<GameVertex*>(v)->player->get_name(), x + 50, y, font);
|
2011-07-01 22:04:15 +00:00
|
|
|
|
|
|
|
DrawUtils::draw_text(display, "str:", x, y + 14, font);
|
|
|
|
DrawUtils::draw_text(display, itos(data.calculate_strength(v)),
|
|
|
|
x + 50, y + 14, font);
|
|
|
|
|
2011-07-02 00:00:19 +00:00
|
|
|
DrawUtils::draw_text(display, "armor:", x, y + 28, font);
|
|
|
|
DrawUtils::draw_text(display, itos(data.calculate_armor(v)),
|
2011-07-01 22:04:15 +00:00
|
|
|
x + 50, y + 28, font);
|
|
|
|
|
|
|
|
DrawUtils::draw_text(display, "hp:", x, y + 42, font);
|
|
|
|
DrawUtils::draw_text(display, itos(v->score), x + 50, y + 42, font);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-28 02:07:52 +00:00
|
|
|
void Game::on_lbutton_down(int x, int y)
|
|
|
|
{
|
2011-07-03 14:12:47 +00:00
|
|
|
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());
|
|
|
|
}
|
2011-07-02 03:47:24 +00:00
|
|
|
|
2011-07-03 14:12:47 +00:00
|
|
|
if (y > display->h - 110) return;
|
2011-07-01 18:10:45 +00:00
|
|
|
if (!data.endgame()) data.handle_click(x, y);
|
2011-06-28 02:07:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Game::on_rbutton_down(int mX, int mY)
|
|
|
|
{
|
|
|
|
data.clear_current_vertex();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Game::on_key_down(SDLKey sym, SDLMod mod, Uint16 unicode)
|
|
|
|
{
|
|
|
|
if (sym == SDLK_q && mod & KMOD_CTRL) throw StateExit();
|
2011-07-02 00:00:19 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2011-07-02 21:34:11 +00:00
|
|
|
|
|
|
|
void Game::on_mouse_move(int mX, int mY, int relX, int relY, bool left, bool right, bool middle)
|
|
|
|
{
|
|
|
|
cursor_x = mX;
|
|
|
|
cursor_y = mY;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-02 00:00:19 +00:00
|
|
|
#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());
|
2011-06-28 02:07:52 +00:00
|
|
|
}
|
2011-07-02 00:00:19 +00:00
|
|
|
#endif
|