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"
|
|
|
|
#include <SDL.h>
|
|
|
|
|
|
|
|
int Game::NODE_RADIUS = 12;
|
|
|
|
|
2011-06-28 02:35:52 +00:00
|
|
|
|
|
|
|
Game::Game(SDL_Surface* display)
|
|
|
|
: GameState(display)
|
|
|
|
{
|
|
|
|
background = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-28 02:07:52 +00:00
|
|
|
Game::~Game()
|
|
|
|
{
|
2011-06-28 02:35:52 +00:00
|
|
|
SDL_FreeSurface(background);
|
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)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
std::cerr << "debug: Game::init(): error: Couldn't load background image\n";
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Game::render()
|
|
|
|
{
|
|
|
|
int range = data.get_range();
|
|
|
|
int range_colour = 0x888888;
|
|
|
|
|
|
|
|
switch(data.get_mode())
|
|
|
|
{
|
|
|
|
case MODE_MOVE:
|
|
|
|
range_colour = 0x0000ff;
|
|
|
|
break;
|
|
|
|
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
|
|
|
|
if (data.get_current_vertex() != NULL)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Flip(display);
|
2011-06-28 02:07:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Game::on_exit()
|
|
|
|
{
|
|
|
|
throw StateExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Game::on_lbutton_down(int x, int y)
|
|
|
|
{
|
|
|
|
data.do_vertex(x, y, NODE_RADIUS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
if (sym == SDLK_a) data.set_mode(MODE_ATTACK);
|
|
|
|
if (sym == SDLK_m) data.set_mode(MODE_MOVE);
|
|
|
|
}
|