Let gamedata store information about whether we are attacking or moving

This commit is contained in:
Anna Rose 2011-06-24 12:02:53 -04:00
parent a71ab74a9c
commit 931ced321a
3 changed files with 39 additions and 17 deletions

View File

@ -3,7 +3,6 @@
#include "debug.h"
#include <SDL.h>
int GameCore::MAX_MOVE_DISTANCE = 100;
int GameCore::NODE_RADIUS = 12;
GameCore::GameCore()
@ -22,7 +21,7 @@ int GameCore::execute()
while(SDL_PollEvent(&event))
handle_event(&event);
// iterate();
renderer.render(data, MAX_MOVE_DISTANCE);
renderer.render(data);
}
cleanup();
@ -54,15 +53,6 @@ void GameCore::on_exit()
void GameCore::on_lbutton_down(int x, int y)
{
Vertex* cv = data.get_current_vertex();
if (cv != NULL &&
(MathUtils::distance(cv->x, cv->y, x, y)
> MAX_MOVE_DISTANCE))
{
data.select_vertex(x, y);
return;
}
data.do_vertex(x, y, NODE_RADIUS);
}
@ -76,4 +66,6 @@ void GameCore::on_rbutton_down(int mX, int mY)
void GameCore::on_key_down(SDLKey sym, SDLMod mod, Uint16 unicode)
{
if (sym == SDLK_q && mod & KMOD_CTRL) is_running = false;
if (sym == SDLK_a) data.set_mode(MODE_ATTACK);
if (sym == SDLK_m) data.set_mode(MODE_MOVE);
}

View File

@ -15,6 +15,7 @@ GameData::GameData()
{
current = NULL;
player = PLAYER1;
mode = MODE_MOVE;
}
GameData::~GameData() { }
@ -23,17 +24,29 @@ void GameData::toggle_turn()
{
if (player == PLAYER1) player = PLAYER2;
else if (player == PLAYER2) player = PLAYER1;
mode = MODE_MOVE;
}
void GameData::do_vertex(int x, int y, int r)
{
if (current != NULL &&
(MathUtils::distance(current->x, current->y, x, y)
> get_move_radius()))
{
select_vertex(x, y);
return;
}
if (mode == MODE_MOVE)
{
int colour;
if (player == PLAYER1) colour = PLAYER1_COLOUR;
if (player == PLAYER2) colour = PLAYER2_COLOUR;
if (point_in_vertex(x, y, r)) select_vertex(x, y);
else add_vertex(x, y, r, colour);
}
}
@ -57,6 +70,8 @@ void GameData::select_vertex(int x, int y)
bool GameData::add_vertex(int x, int y, int r, int colour)
{
if (mode == MODE_ATTACK) return false;
// this is the special case for adding the first vertex for each player
if (current == NULL)
{
@ -152,3 +167,10 @@ float GameData::calculate_strength_r(Vertex* node, unsigned int depth, list<Vert
return modscore;
}
int GameData::get_move_radius(Vertex* node)
{
if (current == NULL) return 0;
else return 100;
}

View File

@ -9,7 +9,7 @@
#include "graph.h"
enum Turn {PLAYER1, PLAYER2, WIN1, WIN2};
enum Mode {MODE_MOVE, MODE_ATTACK};
class GameData : public Graph
{
@ -28,12 +28,20 @@ class GameData : public Graph
bool add_vertex(int x, int y, int r, int colour);
Mode get_mode() const { return mode; }
Mode set_mode(Mode m) { mode = m; }
// returns the move/attack radius (based on mode) for the specified node
// (or the selected node if node == NULL)
int get_move_radius(Vertex* node = NULL);
private:
float calculate_strength(Vertex* node);
float calculate_strength_r(Vertex* node, unsigned int depth, list<Vertex*>& visited);
Vertex* current;
Turn player;
Mode mode;
bool player1_played;
bool player2_played;