Laid the groundwork for a more complex game - introduced a couple of bugs (can attack from anywhere, can't select another of your own vertices in attack mode...)
This commit is contained in:
111
gamedata.cpp
111
gamedata.cpp
@ -10,102 +10,114 @@ using std::list;
|
||||
int GameData::PLAYER1_COLOUR = 0x4a483f;
|
||||
int GameData::PLAYER2_COLOUR = 0x090c7a;
|
||||
|
||||
int GameData::BASE_MOVE_RADIUS = 75;
|
||||
int GameData::BASE_BUILD_RADIUS = 75;
|
||||
int GameData::NODE_RADIUS = 10;
|
||||
|
||||
GameData::GameData()
|
||||
: Graph(true)
|
||||
{
|
||||
current = NULL;
|
||||
player = PLAYER1;
|
||||
mode = MODE_MOVE;
|
||||
mode = MODE_BUILD;
|
||||
player1 = Player(PLAYER1_COLOUR);
|
||||
player2 = Player(PLAYER2_COLOUR);
|
||||
turn = &player1;
|
||||
}
|
||||
|
||||
GameData::~GameData() { }
|
||||
|
||||
|
||||
Vertex* GameData::get_current_vertex(bool only_mine) const
|
||||
{
|
||||
if (only_mine)
|
||||
{
|
||||
if (current != NULL &&
|
||||
current->colour == turn->get_colour()) return current;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return current;
|
||||
}
|
||||
|
||||
|
||||
void GameData::toggle_turn()
|
||||
{
|
||||
mode = MODE_MOVE;
|
||||
current = NULL;
|
||||
if (!turn->has_played())
|
||||
{
|
||||
set_mode(MODE_BUILD);
|
||||
turn->set_played();
|
||||
}
|
||||
else set_mode(MODE_SELECT);
|
||||
|
||||
if (!endgame())
|
||||
{
|
||||
if (player == PLAYER1) player = PLAYER2;
|
||||
else if (player == PLAYER2) player = PLAYER1;
|
||||
if (turn == &player1) turn = &player2;
|
||||
else if (turn == &player2) turn = &player1;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void GameData::do_vertex(int x, int y, int r)
|
||||
void GameData::handle_click(int x, int y)
|
||||
{
|
||||
if (current != NULL &&
|
||||
(MathUtils::distance(current->x, current->y, current->z, x, y, 0)
|
||||
> get_range()))
|
||||
{
|
||||
select_vertex(x, y);
|
||||
return;
|
||||
}
|
||||
int r = 10;
|
||||
|
||||
int colour;
|
||||
if (player == PLAYER1) colour = PLAYER1_COLOUR;
|
||||
if (player == PLAYER2) colour = PLAYER2_COLOUR;
|
||||
colour = turn->get_colour();
|
||||
|
||||
if (mode == MODE_MOVE)
|
||||
if (mode == MODE_SELECT)
|
||||
{
|
||||
if (point_in_vertex(x, y, 0)) select_vertex(x, y);
|
||||
else add_vertex(x, y, 0, r, colour);
|
||||
}
|
||||
if (mode == MODE_ATTACK)
|
||||
else if (mode == MODE_BUILD)
|
||||
{
|
||||
if (point_in_vertex(x, y, 0)) select_vertex(x, y, true);
|
||||
if (!point_in_vertex(x, y, 0)) add_vertex(x, y, 0, r, colour);
|
||||
}
|
||||
else if (mode == MODE_ATTACK)
|
||||
{
|
||||
Vertex* v = vertex_at(x, y, 0);
|
||||
if (v == NULL) return;
|
||||
|
||||
if (v->colour == colour) select_vertex(x, y);
|
||||
else attack_vertex(v);
|
||||
if (v->colour != colour) attack_vertex(v);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void GameData::select_vertex(int x, int y)
|
||||
void GameData::select_vertex(int x, int y, bool only_mine)
|
||||
{
|
||||
Vertex * v = vertex_at(x, y, 0);
|
||||
if (v == NULL) return;
|
||||
|
||||
for (list<Vertex*>::iterator cursor = vertices.begin();
|
||||
cursor != vertices.end(); cursor++)
|
||||
if (only_mine && v->colour == turn->get_colour())
|
||||
{
|
||||
Vertex* v = *cursor;
|
||||
if ((MathUtils::distance(v->x, v->y, v->z, x, y, 0) <= v->r) &&
|
||||
(v->colour == PLAYER1_COLOUR && player == PLAYER1 ||
|
||||
v->colour == PLAYER2_COLOUR && player == PLAYER2))
|
||||
{
|
||||
current = v;
|
||||
return;
|
||||
}
|
||||
current = v;
|
||||
return;
|
||||
}
|
||||
|
||||
current = v;
|
||||
}
|
||||
|
||||
|
||||
bool GameData::add_vertex(int x, int y, int z, int r, int colour)
|
||||
{
|
||||
if (mode == MODE_ATTACK) return false;
|
||||
|
||||
if (current == NULL)
|
||||
{
|
||||
// this is the special case for adding the first vertex for each player
|
||||
if ((player == PLAYER1 && !player1_played) ||
|
||||
(player == PLAYER2 && !player2_played))
|
||||
if (!turn->has_played())
|
||||
{
|
||||
Graph::add_vertex(x, y, z, r, colour, 10);
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "debug: GameData::add_vertex(): strength=%2.f\n",
|
||||
calculate_strength(*(vertices.rbegin())));
|
||||
#endif
|
||||
if (player == PLAYER1) player1_played = true;
|
||||
if (player == PLAYER2) player2_played = true;
|
||||
toggle_turn();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (current->colour != turn->get_colour()) return false;
|
||||
|
||||
if (Graph::add_vertex(x, y, z, r, colour, 10, current))
|
||||
{
|
||||
#ifdef DEBUG
|
||||
@ -176,15 +188,22 @@ float GameData::calculate_strength_r(Vertex* node, unsigned int depth, list<Vert
|
||||
}
|
||||
|
||||
|
||||
Mode GameData::set_mode(Mode m)
|
||||
{
|
||||
mode = m;
|
||||
}
|
||||
|
||||
|
||||
int GameData::get_range(Vertex* node)
|
||||
{
|
||||
if (node == NULL) node = current;
|
||||
|
||||
if (node == NULL) return 0;
|
||||
else if (mode == MODE_MOVE) return BASE_MOVE_RADIUS;
|
||||
else if (mode == MODE_MOVE) return turn->get_energy();
|
||||
else if (mode == MODE_BUILD) return BASE_BUILD_RADIUS;
|
||||
else if (mode == MODE_ATTACK)
|
||||
{
|
||||
int range = BASE_MOVE_RADIUS;
|
||||
int range = BASE_BUILD_RADIUS;
|
||||
list<Vertex*> neighbors = node->neighbors;
|
||||
|
||||
for(list<Vertex*>::iterator cursor = neighbors.begin();
|
||||
@ -223,17 +242,15 @@ void GameData::attack_vertex(Vertex* target)
|
||||
|
||||
bool GameData::endgame()
|
||||
{
|
||||
if (!(player1_played && player2_played)) return false;
|
||||
if (!(player1.has_played() && player2.has_played())) return false;
|
||||
|
||||
if (get_colour(PLAYER1_COLOUR).empty())
|
||||
if (get_colour(player1.get_colour()).empty())
|
||||
{
|
||||
player = WIN2;
|
||||
debug("Gamedata::endgame(): player 2 wins\n");
|
||||
return true;
|
||||
}
|
||||
if (get_colour(PLAYER2_COLOUR).empty())
|
||||
if (get_colour(player2.get_colour()).empty())
|
||||
{
|
||||
player = WIN1;
|
||||
debug("Gamedata::endgame(): player 1 wins\n");
|
||||
return true;
|
||||
}
|
||||
|
Reference in New Issue
Block a user