Added player names and some debug stuff
This commit is contained in:
parent
bf151a928a
commit
e221f1990c
24
game.cpp
24
game.cpp
|
@ -123,8 +123,8 @@ void Game::draw_stats(Vertex* v)
|
||||||
DrawUtils::draw_text(display, itos(data.calculate_strength(v)),
|
DrawUtils::draw_text(display, itos(data.calculate_strength(v)),
|
||||||
x + 50, y + 14, font);
|
x + 50, y + 14, font);
|
||||||
|
|
||||||
DrawUtils::draw_text(display, "def:", x, y + 28, font);
|
DrawUtils::draw_text(display, "armor:", x, y + 28, font);
|
||||||
DrawUtils::draw_text(display, itos(data.calculate_strength(v)),
|
DrawUtils::draw_text(display, itos(data.calculate_armor(v)),
|
||||||
x + 50, y + 28, font);
|
x + 50, y + 28, font);
|
||||||
|
|
||||||
DrawUtils::draw_text(display, "hp:", x, y + 42, font);
|
DrawUtils::draw_text(display, "hp:", x, y + 42, font);
|
||||||
|
@ -148,8 +148,20 @@ void Game::on_rbutton_down(int mX, int mY)
|
||||||
void Game::on_key_down(SDLKey sym, SDLMod mod, Uint16 unicode)
|
void Game::on_key_down(SDLKey sym, SDLMod mod, Uint16 unicode)
|
||||||
{
|
{
|
||||||
if (sym == SDLK_q && mod & KMOD_CTRL) throw StateExit();
|
if (sym == SDLK_q && mod & KMOD_CTRL) throw StateExit();
|
||||||
if (sym == SDLK_a) data.set_mode(MODE_ATTACK);
|
else if (sym == SDLK_a) data.set_mode(MODE_ATTACK);
|
||||||
if (sym == SDLK_m) data.set_mode(MODE_MOVE);
|
else if (sym == SDLK_m) data.set_mode(MODE_MOVE);
|
||||||
if (sym == SDLK_b) data.set_mode(MODE_BUILD);
|
else if (sym == SDLK_b) data.set_mode(MODE_BUILD);
|
||||||
if (sym == SDLK_s || sym == SDLK_ESCAPE) data.set_mode(MODE_SELECT);
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#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());
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
4
game.h
4
game.h
|
@ -43,6 +43,10 @@ class Game : public GameState
|
||||||
// surfaces containing textures to draw
|
// surfaces containing textures to draw
|
||||||
SDL_Surface* background;
|
SDL_Surface* background;
|
||||||
TTF_Font* font;
|
TTF_Font* font;
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
|
void print_debug_info();
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
35
gamedata.cpp
35
gamedata.cpp
|
@ -13,13 +13,14 @@ int GameData::PLAYER2_COLOUR = 0x090c7a;
|
||||||
int GameData::BASE_BUILD_RADIUS = 75;
|
int GameData::BASE_BUILD_RADIUS = 75;
|
||||||
int GameData::NODE_RADIUS = 10;
|
int GameData::NODE_RADIUS = 10;
|
||||||
|
|
||||||
|
|
||||||
GameData::GameData()
|
GameData::GameData()
|
||||||
: Graph(true)
|
: Graph(true)
|
||||||
{
|
{
|
||||||
current = NULL;
|
current = NULL;
|
||||||
mode = MODE_BUILD;
|
mode = MODE_BUILD;
|
||||||
player1 = Player(PLAYER1_COLOUR);
|
player1 = Player("player 1", PLAYER1_COLOUR);
|
||||||
player2 = Player(PLAYER2_COLOUR);
|
player2 = Player("player 2", PLAYER2_COLOUR);
|
||||||
turn = &player1;
|
turn = &player1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,7 +31,7 @@ Vertex* GameData::get_current_vertex(bool only_mine) const
|
||||||
{
|
{
|
||||||
if (only_mine)
|
if (only_mine)
|
||||||
{
|
{
|
||||||
if (current != NULL &&
|
if (current != NULL &&
|
||||||
current->colour == turn->get_colour()) return current;
|
current->colour == turn->get_colour()) return current;
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -40,6 +41,13 @@ Vertex* GameData::get_current_vertex(bool only_mine) const
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void GameData::clear_current_vertex()
|
||||||
|
{
|
||||||
|
mode = MODE_SELECT;
|
||||||
|
current = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void GameData::toggle_turn()
|
void GameData::toggle_turn()
|
||||||
{
|
{
|
||||||
if (!turn->has_played()) turn->set_played();
|
if (!turn->has_played()) turn->set_played();
|
||||||
|
@ -75,9 +83,8 @@ void GameData::handle_click(int x, int y)
|
||||||
}
|
}
|
||||||
else if (mode == MODE_ATTACK)
|
else if (mode == MODE_ATTACK)
|
||||||
{
|
{
|
||||||
if (select_vertex(x, y, true)) return;
|
|
||||||
Vertex* v = vertex_at(x, y, 0);
|
Vertex* v = vertex_at(x, y, 0);
|
||||||
if (v == NULL) return;
|
if (v == NULL || v->colour == turn->get_colour()) return;
|
||||||
if (v->colour != colour) attack_vertex(v);
|
if (v->colour != colour) attack_vertex(v);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -141,6 +148,13 @@ bool GameData::add_vertex(int x, int y, int z, int r, int colour)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
float GameData::calculate_armor(Vertex* node)
|
||||||
|
{
|
||||||
|
float str = calculate_strength(node);
|
||||||
|
return str / 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
float GameData::calculate_strength(Vertex* node)
|
float GameData::calculate_strength(Vertex* node)
|
||||||
{
|
{
|
||||||
list<Vertex*> visited;
|
list<Vertex*> visited;
|
||||||
|
@ -204,7 +218,7 @@ void GameData::set_mode(Mode m)
|
||||||
{
|
{
|
||||||
// Stay in MODE_SELECT (or maybe MODE_BUILD) when current is null
|
// Stay in MODE_SELECT (or maybe MODE_BUILD) when current is null
|
||||||
if (current == NULL) return;
|
if (current == NULL) return;
|
||||||
|
|
||||||
// The other modes all require current to match the player
|
// The other modes all require current to match the player
|
||||||
if (current->colour == turn->get_colour()) mode = m;
|
if (current->colour == turn->get_colour()) mode = m;
|
||||||
}
|
}
|
||||||
|
@ -238,18 +252,17 @@ int GameData::get_range(Vertex* node)
|
||||||
|
|
||||||
void GameData::attack_vertex(Vertex* target)
|
void GameData::attack_vertex(Vertex* target)
|
||||||
{
|
{
|
||||||
float atk_str = calculate_strength(current);
|
float atk = calculate_strength(current);
|
||||||
float def_str = calculate_strength(target);
|
float armor = calculate_armor(current);
|
||||||
float armor = def_str / 10; // how much energy it takes to deal 1 damage
|
|
||||||
|
|
||||||
int damage = (int)(atk_str / armor);
|
int damage = (int)(atk / armor);
|
||||||
|
|
||||||
target->score -= damage;
|
target->score -= damage;
|
||||||
|
|
||||||
if (target->score <= 0) remove_vertex(target);
|
if (target->score <= 0) remove_vertex(target);
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
fprintf(stderr, "debug: GameData::attack_vertex(): atk_str=%.2f, def_str=%.2f, armor=%.2f, damage=%d\n", atk_str, def_str, armor, damage);
|
fprintf(stderr, "debug: GameData::attack_vertex(): atk=%.2f, armor=%.2f, armor=%.2f, damage=%d\n", atk, armor, damage);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
toggle_turn();
|
toggle_turn();
|
||||||
|
|
12
gamedata.h
12
gamedata.h
|
@ -10,14 +10,7 @@
|
||||||
#include "player.h"
|
#include "player.h"
|
||||||
|
|
||||||
enum Mode {MODE_MOVE, MODE_ATTACK, MODE_BUILD, MODE_SELECT};
|
enum Mode {MODE_MOVE, MODE_ATTACK, MODE_BUILD, MODE_SELECT};
|
||||||
enum VertexType {ATTACKER, DEFENDER, PRODUCER};
|
enum VertexType {VERTEX_ATTACKER, VERTEX_DEFENDER, VERTEX_PRODUCER};
|
||||||
|
|
||||||
class GameVertex : public Vertex
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
VertexType type;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
class GameData : public Graph
|
class GameData : public Graph
|
||||||
{
|
{
|
||||||
|
@ -26,7 +19,7 @@ class GameData : public Graph
|
||||||
~GameData();
|
~GameData();
|
||||||
|
|
||||||
Vertex* get_current_vertex(bool only_mine = false) const;
|
Vertex* get_current_vertex(bool only_mine = false) const;
|
||||||
void clear_current_vertex() { current = NULL; }
|
void clear_current_vertex();
|
||||||
|
|
||||||
void toggle_turn();
|
void toggle_turn();
|
||||||
|
|
||||||
|
@ -48,6 +41,7 @@ class GameData : public Graph
|
||||||
bool endgame();
|
bool endgame();
|
||||||
Player* get_turn() const { return turn; }
|
Player* get_turn() const { return turn; }
|
||||||
float calculate_strength(Vertex* node);
|
float calculate_strength(Vertex* node);
|
||||||
|
float calculate_armor(Vertex* node);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
float calculate_strength_r(Vertex* node, unsigned int depth, list<Vertex*>& visited);
|
float calculate_strength_r(Vertex* node, unsigned int depth, list<Vertex*>& visited);
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
#include "player.h"
|
#include "player.h"
|
||||||
|
|
||||||
Player::Player(unsigned int colour)
|
Player::Player(string name, unsigned int colour)
|
||||||
{
|
{
|
||||||
|
this->name = name;
|
||||||
this->colour = colour;
|
this->colour = colour;
|
||||||
energy = 50;
|
energy = 50;
|
||||||
}
|
}
|
||||||
|
|
7
player.h
7
player.h
|
@ -4,13 +4,17 @@
|
||||||
#ifndef _PLAYER_H_
|
#ifndef _PLAYER_H_
|
||||||
#define _PLAYER_H_
|
#define _PLAYER_H_
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
using std::string;
|
||||||
|
|
||||||
class Player
|
class Player
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Player(unsigned int colour = 0x000000);
|
Player(string name = "player", unsigned int colour = 0x000000);
|
||||||
|
|
||||||
unsigned int get_energy() const { return energy; }
|
unsigned int get_energy() const { return energy; }
|
||||||
unsigned int get_colour() const { return colour; }
|
unsigned int get_colour() const { return colour; }
|
||||||
|
string get_name() const { return name; }
|
||||||
|
|
||||||
void add_energy(unsigned int amount);
|
void add_energy(unsigned int amount);
|
||||||
bool spend_energy(unsigned int amount);
|
bool spend_energy(unsigned int amount);
|
||||||
|
@ -22,6 +26,7 @@ class Player
|
||||||
unsigned int energy;
|
unsigned int energy;
|
||||||
unsigned int colour;
|
unsigned int colour;
|
||||||
bool played;
|
bool played;
|
||||||
|
string name;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue
Block a user