Let gamedata store information about whether we are attacking or moving
This commit is contained in:
parent
a71ab74a9c
commit
931ced321a
14
gamecore.cpp
14
gamecore.cpp
|
@ -3,7 +3,6 @@
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
|
|
||||||
int GameCore::MAX_MOVE_DISTANCE = 100;
|
|
||||||
int GameCore::NODE_RADIUS = 12;
|
int GameCore::NODE_RADIUS = 12;
|
||||||
|
|
||||||
GameCore::GameCore()
|
GameCore::GameCore()
|
||||||
|
@ -22,7 +21,7 @@ int GameCore::execute()
|
||||||
while(SDL_PollEvent(&event))
|
while(SDL_PollEvent(&event))
|
||||||
handle_event(&event);
|
handle_event(&event);
|
||||||
// iterate();
|
// iterate();
|
||||||
renderer.render(data, MAX_MOVE_DISTANCE);
|
renderer.render(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
cleanup();
|
cleanup();
|
||||||
|
@ -54,15 +53,6 @@ void GameCore::on_exit()
|
||||||
|
|
||||||
void GameCore::on_lbutton_down(int x, int y)
|
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);
|
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)
|
void GameCore::on_key_down(SDLKey sym, SDLMod mod, Uint16 unicode)
|
||||||
{
|
{
|
||||||
if (sym == SDLK_q && mod & KMOD_CTRL) is_running = false;
|
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);
|
||||||
}
|
}
|
||||||
|
|
22
gamedata.cpp
22
gamedata.cpp
|
@ -15,6 +15,7 @@ GameData::GameData()
|
||||||
{
|
{
|
||||||
current = NULL;
|
current = NULL;
|
||||||
player = PLAYER1;
|
player = PLAYER1;
|
||||||
|
mode = MODE_MOVE;
|
||||||
}
|
}
|
||||||
|
|
||||||
GameData::~GameData() { }
|
GameData::~GameData() { }
|
||||||
|
@ -23,10 +24,21 @@ void GameData::toggle_turn()
|
||||||
{
|
{
|
||||||
if (player == PLAYER1) player = PLAYER2;
|
if (player == PLAYER1) player = PLAYER2;
|
||||||
else if (player == PLAYER2) player = PLAYER1;
|
else if (player == PLAYER2) player = PLAYER1;
|
||||||
|
mode = MODE_MOVE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void GameData::do_vertex(int x, int y, int r)
|
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;
|
int colour;
|
||||||
if (player == PLAYER1) colour = PLAYER1_COLOUR;
|
if (player == PLAYER1) colour = PLAYER1_COLOUR;
|
||||||
|
@ -35,6 +47,7 @@ void GameData::do_vertex(int x, int y, int r)
|
||||||
if (point_in_vertex(x, y, r)) select_vertex(x, y);
|
if (point_in_vertex(x, y, r)) select_vertex(x, y);
|
||||||
else add_vertex(x, y, r, colour);
|
else add_vertex(x, y, r, colour);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void GameData::select_vertex(int x, int y)
|
void GameData::select_vertex(int x, int y)
|
||||||
|
@ -57,6 +70,8 @@ void GameData::select_vertex(int x, int y)
|
||||||
|
|
||||||
bool GameData::add_vertex(int x, int y, int r, int colour)
|
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
|
// this is the special case for adding the first vertex for each player
|
||||||
if (current == NULL)
|
if (current == NULL)
|
||||||
{
|
{
|
||||||
|
@ -152,3 +167,10 @@ float GameData::calculate_strength_r(Vertex* node, unsigned int depth, list<Vert
|
||||||
|
|
||||||
return modscore;
|
return modscore;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int GameData::get_move_radius(Vertex* node)
|
||||||
|
{
|
||||||
|
if (current == NULL) return 0;
|
||||||
|
else return 100;
|
||||||
|
}
|
||||||
|
|
10
gamedata.h
10
gamedata.h
|
@ -9,7 +9,7 @@
|
||||||
#include "graph.h"
|
#include "graph.h"
|
||||||
|
|
||||||
enum Turn {PLAYER1, PLAYER2, WIN1, WIN2};
|
enum Turn {PLAYER1, PLAYER2, WIN1, WIN2};
|
||||||
|
enum Mode {MODE_MOVE, MODE_ATTACK};
|
||||||
|
|
||||||
class GameData : public Graph
|
class GameData : public Graph
|
||||||
{
|
{
|
||||||
|
@ -28,12 +28,20 @@ class GameData : public Graph
|
||||||
|
|
||||||
bool add_vertex(int x, int y, int r, int colour);
|
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:
|
private:
|
||||||
float calculate_strength(Vertex* node);
|
float calculate_strength(Vertex* node);
|
||||||
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;
|
||||||
Turn player;
|
Turn player;
|
||||||
|
Mode mode;
|
||||||
bool player1_played;
|
bool player1_played;
|
||||||
bool player2_played;
|
bool player2_played;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user