Added surrendering.

Fixed an issue concerning the box of the UpgradeBox.
This commit is contained in:
Tim Schubert 2016-01-30 03:58:53 +01:00
parent 63d4e42b80
commit 230408e2fc
6 changed files with 129 additions and 42 deletions

View file

@ -205,18 +205,7 @@ void Game::command(std::string input)
{ {
if (this->started) if (this->started)
{ {
Player *last_player = Player::current_player; this->next_turn();
this->turn = (this->turn + 1) % players.size();
Player::current_player = players[turn];
if (this->turn == 0)
{
trigger_event(BOB_NEXTTURNEVENT, 0, (void *) last_player, (void *) Player::current_player);
trigger_event(BOB_NEXTROUNDEVENT, 0, (void *) last_player, (void *) Player::current_player);
}
else
{
trigger_event(BOB_NEXTTURNEVENT, 0, (void *) last_player, (void *) Player::current_player);
}
prompt << "Next player is: " << (Player::current_player)->get_name(); prompt << "Next player is: " << (Player::current_player)->get_name();
} }
else else
@ -226,7 +215,17 @@ void Game::command(std::string input)
} }
else if (input == "surrender") else if (input == "surrender")
{ {
//Player::current_player->surrender(); this->grid->surrender(Player::current_player);
prompt << "Player " << Player::current_player->get_name() << " surrendered!\n";
for (int i = 0; i < this->players.size(); i++)
{
if (this->players[i] == Player::current_player)
{
delete this->players[i];
this->players.erase(this->players.begin() + i);
}
}
this->next_turn();
} }
else if (input.substr(0, 10) == "add player") else if (input.substr(0, 10) == "add player")
{ {
@ -278,6 +277,32 @@ void Game::start()
trigger_event(BOB_NEXTTURNEVENT, 0, nullptr, (void *) Player::current_player); trigger_event(BOB_NEXTTURNEVENT, 0, nullptr, (void *) Player::current_player);
} }
void Game::next_turn()
{
if (!this->players.empty())
{
Player *last_player = Player::current_player;
this->turn = (this->turn + 1) % players.size();
Player::current_player = players[turn];
if (this->turn == 0)
{
trigger_event(BOB_NEXTTURNEVENT, 0, (void *) last_player, (void *) Player::current_player);
trigger_event(BOB_NEXTROUNDEVENT, 0, (void *) last_player, (void *) Player::current_player);
}
else
{
trigger_event(BOB_NEXTTURNEVENT, 0, (void *) last_player, (void *) Player::current_player);
}
}
else
{
Player *def = new Player();
Player::current_player = def;
this->players.push_back(def);
this->started = false;
}
}
int Game::game_loop() int Game::game_loop()
{ {
this->frame_timer->start_timer(); this->frame_timer->start_timer();

View file

@ -94,6 +94,8 @@ public:
int game_loop(); int game_loop();
void next_turn();
private: private:
bool started; bool started;
Player *adding; Player *adding;

View file

@ -272,7 +272,7 @@ bool Player::fight(FieldMeta *field)
Cluster defenders_cluster = field->get_grid()->get_cluster(field); Cluster defenders_cluster = field->get_grid()->get_cluster(field);
Resource defenders_cluster_res = field->get_grid()->get_resources_of_cluster(&defenders_cluster); Resource defenders_cluster_res = field->get_grid()->get_resources_of_cluster(&defenders_cluster);
Cluster attackers_cluster; Cluster attackers_cluster;
// defending player's defense against attacking player's offense // defending player's Defense against attacking player's offense
int power_level = field->get_defense(); // it's over 9000 int power_level = field->get_defense(); // it's over 9000
for (Uint8 i = 0; i < 6; i++) for (Uint8 i = 0; i < 6; i++)
{ {
@ -295,7 +295,8 @@ bool Player::fight(FieldMeta *field)
// else: ignore, field / player not part of the fight (e.g. default player) // else: ignore, field / player not part of the fight (e.g. default player)
} }
Resource costs = {(Uint32) std::abs(power_level), (Uint32) std::abs(power_level), (Uint32) std::abs(power_level)}; Resource costs = {(Uint32) std::abs(power_level), (Uint32) std::abs(power_level), (Uint32) std::abs(power_level)};
if (power_level < 1 && is_neighbor) // attacking player has won if (power_level < 2 && is_neighbor &&
costs <= field->get_grid()->get_resources_of_cluster(&attackers_cluster)) // attacking player has won
{ {
field->get_grid()->consume_resources_of_cluster(&attackers_cluster, costs); field->get_grid()->consume_resources_of_cluster(&attackers_cluster, costs);
field->get_grid()->consume_resources_of_cluster(&defenders_cluster, costs); field->get_grid()->consume_resources_of_cluster(&defenders_cluster, costs);
@ -499,9 +500,12 @@ void HexagonGrid::handle_event(SDL_Event *event)
trigger_event(BOB_FIELDSELECTEDEVENT, 0, (void *) this->marker, nullptr); trigger_event(BOB_FIELDSELECTEDEVENT, 0, (void *) this->marker, nullptr);
this->placing = false; this->placing = false;
} }
else if (this->attack_marker != nullptr && this->attack_marker == this->marker) else if (this->attack_marker != nullptr)
{ {
Player::current_player->fight(this->attack_marker); if (this->attack_marker == this->marker)
{
Player::current_player->fight(this->attack_marker);
}
this->attack_marker = nullptr; this->attack_marker = nullptr;
} }
else if (this->attack_marker == nullptr) else if (this->attack_marker == nullptr)
@ -640,10 +644,10 @@ Point HexagonGrid::field_to_point(FieldMeta *field)
return field->get_field().field_to_point(this->layout); return field->get_field().field_to_point(this->layout);
} }
bool inside_target(const SDL_Rect *target, const SDL_Point *position) bool inside_target(const SDL_Rect *box, const SDL_Point *position)
{ {
return target->x < position->x && target->x + target->w > position->x && target->y < position->y && return box->x < position->x && box->x + box->w > position->x && box->y < position->y &&
target->y + target->h > position->y; box->y + box->h > position->y;
} }
bool HexagonGrid::place(Player *player, FieldMeta *center) bool HexagonGrid::place(Player *player, FieldMeta *center)
@ -688,3 +692,18 @@ void Player::handle_event(SDL_Event *event)
// nothing atm // nothing atm
} }
} }
void HexagonGrid::surrender(Player *player)
{
for (std::pair<Field, FieldMeta *> pair : this->fields)
{
if (pair.second != nullptr && *(pair.second->get_owner()) == *player)
{
Field old_field = pair.second->get_field();
delete pair.second;
this->fields.erase(pair.first);
FieldMeta *new_field = new FieldMeta(this, old_field, this->default_player);
this->fields.insert({old_field, new_field});
}
}
}

View file

@ -445,13 +445,20 @@ enum Upgrade
Regeneration_3, Regeneration_3,
Reproduction_1, Reproduction_1,
Reproduction_2, Reproduction_2,
Reproduction_3 Reproduction_3,
Offense_1,
Offense_2,
Offense_3,
Defense_1,
Defense_2,
Defense_3,
}; };
const std::vector<Upgrade> UPGRADES = {Regeneration_1, Regeneration_2, Regeneration_3, Reproduction_1, Reproduction_2, const std::vector<Upgrade> UPGRADES = {Regeneration_1, Regeneration_2, Regeneration_3, Reproduction_1, Reproduction_2,
Reproduction_3}; Reproduction_3, Offense_1, Offense_2, Offense_3, Defense_1, Defense_2, Defense_3
};
const int NUM_UPGRADES = 6; const int NUM_UPGRADES = 12;
typedef std::bitset<NUM_UPGRADES> UpgradeFlags; typedef std::bitset<NUM_UPGRADES> UpgradeFlags;
@ -476,7 +483,13 @@ const std::unordered_map<Upgrade, Resource> UPGRADE_COSTS(
{Regeneration_3, {16, 16, 16}}, {Regeneration_3, {16, 16, 16}},
{Reproduction_1, {4, 4, 4}}, {Reproduction_1, {4, 4, 4}},
{Reproduction_2, {8, 8, 8}}, {Reproduction_2, {8, 8, 8}},
{Reproduction_3, {16, 16, 16}} {Reproduction_3, {16, 16, 16}},
{Offense_1, {4, 4, 4}},
{Offense_2, {8, 8, 8}},
{Offense_3, {16, 16, 16}},
{Defense_1, {4, 4, 4}},
{Defense_2, {8, 8, 8}},
{Defense_3, {16, 16, 16}}
} }
); );
@ -487,10 +500,17 @@ const std::unordered_map<Upgrade, std::string> UPGRADE_NAMES(
{Regeneration_3, "Regeneration 3"}, {Regeneration_3, "Regeneration 3"},
{Reproduction_1, "Reproduction 1"}, {Reproduction_1, "Reproduction 1"},
{Reproduction_2, "Reproduction 2"}, {Reproduction_2, "Reproduction 2"},
{Reproduction_3, "Reproduction 3"} {Reproduction_3, "Reproduction 3"},
{Offense_1, "Offense 1"},
{Offense_2, "Offense 2"},
{Offense_3, "Offense 3"},
{Defense_1, "Defense 1"},
{Defense_2, "Defense 2"},
{Defense_3, "Offense 3"}
} }
); );
const std::unordered_map<Upgrade, std::string> UPGRADE_TEXTS( const std::unordered_map<Upgrade, std::string> UPGRADE_TEXTS(
{ {
{Regeneration_1, "Resources yield 2x their base resources per turn."}, {Regeneration_1, "Resources yield 2x their base resources per turn."},
@ -498,7 +518,13 @@ const std::unordered_map<Upgrade, std::string> UPGRADE_TEXTS(
{Regeneration_3, "Resources yield 8x their base resources per turn."}, {Regeneration_3, "Resources yield 8x their base resources per turn."},
{Reproduction_1, "Increase the chance for expanding to a new field at the end of the turn by 25%."}, {Reproduction_1, "Increase the chance for expanding to a new field at the end of the turn by 25%."},
{Reproduction_2, "Increase the chance for expanding to a new field at the end of the turn by 50%."}, {Reproduction_2, "Increase the chance for expanding to a new field at the end of the turn by 50%."},
{Reproduction_3, "Increase the chance for expanding to a new field at the end of the turn by 75%."} {Reproduction_3, "Increase the chance for expanding to a new field at the end of the turn by 75%."},
{Offense_1, "Double your offense."},
{Offense_2, "Double your offense."},
{Offense_3, "Double your offense."},
{Defense_1, "Double your defense."},
{Defense_2, "Double your defense."},
{Defense_3, "Double your defense."}
} }
); );
@ -580,9 +606,29 @@ public:
HexagonGrid *get_grid() { return this->grid; } HexagonGrid *get_grid() { return this->grid; }
int get_offense() { return this->offense; } int get_offense()
{
int factor = 1;
if (this->get_upgrades()[Offense_1])
factor *= 2;
if (this->get_upgrades()[Offense_2])
factor *= 2;
if (this->get_upgrades()[Offense_3])
factor *= 2;
return this->offense * factor;
}
int get_defense() { return this->defense; } int get_defense()
{
int factor = 1;
if (this->upgrades[Defense_1])
factor *= 2;
if (this->get_upgrades()[Defense_2])
factor *= 2;
if (this->get_upgrades()[Defense_3])
factor *= 2;
return this->offense * factor;
}
void set_offense(int off) { this->offense = off; } void set_offense(int off) { this->offense = off; }
@ -700,6 +746,8 @@ public:
FieldMeta *get_attack_marker() { return this->attack_marker; } FieldMeta *get_attack_marker() { return this->attack_marker; }
void surrender(Player *player);
private: private:
bool changed; bool changed;
bool placing; bool placing;

View file

@ -152,10 +152,7 @@ void UpgradeBox::handle_event(const SDL_Event *event)
Timer::MOUSE_LOCKED = false; Timer::MOUSE_LOCKED = false;
this->set_visible(false); this->set_visible(false);
} }
else this->marked_upgrade->handle_event(event);
{
this->marked_upgrade->handle_event(event);
}
changed = true; changed = true;
} }
else if (event->type == SDL_MOUSEMOTION && this->visible) else if (event->type == SDL_MOUSEMOTION && this->visible)
@ -230,10 +227,8 @@ void UpgradeBox::render(Renderer *ext_renderer)
box->render(ext_renderer); box->render(ext_renderer);
} }
this->changed = false; this->changed = false;
/*SDL_Rect dim = this->upgrades[0]->get_dimensions(); this->dimensions = this->upgrades[0]->get_dimensions();
this->dimensions.w = dim.w; this->dimensions.h *= this->upgrades.size();
this->dimensions.h = dim.h * (int) this->upgrades.size();
*/
} }
void Container::render(Renderer *renderer) void Container::render(Renderer *renderer)
@ -438,5 +433,5 @@ void TextInputBox::update_dimensions(SDL_Rect rect)
bool TextInputBox::get_active() bool TextInputBox::get_active()
{ {
return SDL_IsTextInputActive() == SDL_TRUE; return SDL_IsTextInputActive() == true;
} }

View file

@ -174,13 +174,11 @@ public:
UpgradeBox(Renderer *renderer, SDL_Rect dimensions, SDL_Color color, TTF_Font *font, FieldMeta *field_) UpgradeBox(Renderer *renderer, SDL_Rect dimensions, SDL_Color color, TTF_Font *font, FieldMeta *field_)
: Box(renderer, dimensions, color), field(field_) : Box(renderer, dimensions, color), field(field_)
{ {
int y = dimensions.y;
for (Upgrade upgrade : UPGRADES) for (Upgrade upgrade : UPGRADES)
{ {
UpgradeButtonBox *box = new UpgradeButtonBox(renderer, {0, y, dimensions.w, 20}, color, font, this, UpgradeButtonBox *box = new UpgradeButtonBox(renderer, {0, dimensions.y, dimensions.w, 20}, color, font,
upgrade); this, upgrade);
box->load_text(UPGRADE_NAMES.at(upgrade)); box->load_text(UPGRADE_NAMES.at(upgrade));
y += 20;
this->marked_upgrade = box; this->marked_upgrade = box;
this->upgrades.push_back(box); this->upgrades.push_back(box);
} }