diff --git a/src/Bob.cpp b/src/Bob.cpp index 36694ba..58ca15b 100644 --- a/src/Bob.cpp +++ b/src/Bob.cpp @@ -205,18 +205,7 @@ void Game::command(std::string input) { if (this->started) { - 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); - } + this->next_turn(); prompt << "Next player is: " << (Player::current_player)->get_name(); } else @@ -226,7 +215,17 @@ void Game::command(std::string input) } 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") { @@ -278,6 +277,32 @@ void Game::start() 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() { this->frame_timer->start_timer(); diff --git a/src/Bob.hpp b/src/Bob.hpp index a710578..c1dfaa2 100644 --- a/src/Bob.hpp +++ b/src/Bob.hpp @@ -94,6 +94,8 @@ public: int game_loop(); + void next_turn(); + private: bool started; Player *adding; diff --git a/src/Gameplay.cpp b/src/Gameplay.cpp index 52e4784..4e800d1 100644 --- a/src/Gameplay.cpp +++ b/src/Gameplay.cpp @@ -272,7 +272,7 @@ bool Player::fight(FieldMeta *field) Cluster defenders_cluster = field->get_grid()->get_cluster(field); Resource defenders_cluster_res = field->get_grid()->get_resources_of_cluster(&defenders_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 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) } 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(&defenders_cluster, costs); @@ -499,9 +500,12 @@ void HexagonGrid::handle_event(SDL_Event *event) trigger_event(BOB_FIELDSELECTEDEVENT, 0, (void *) this->marker, nullptr); 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; } 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); } -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 && - target->y + target->h > position->y; + return box->x < position->x && box->x + box->w > position->x && box->y < position->y && + box->y + box->h > position->y; } bool HexagonGrid::place(Player *player, FieldMeta *center) @@ -687,4 +691,19 @@ void Player::handle_event(SDL_Event *event) { // nothing atm } -} \ No newline at end of file +} + +void HexagonGrid::surrender(Player *player) +{ + for (std::pair 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}); + } + } +} diff --git a/src/Gameplay.hpp b/src/Gameplay.hpp index 19e0e75..e68c6f9 100644 --- a/src/Gameplay.hpp +++ b/src/Gameplay.hpp @@ -445,13 +445,20 @@ enum Upgrade Regeneration_3, Reproduction_1, Reproduction_2, - Reproduction_3 + Reproduction_3, + Offense_1, + Offense_2, + Offense_3, + Defense_1, + Defense_2, + Defense_3, }; const std::vector 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 UpgradeFlags; @@ -476,7 +483,13 @@ const std::unordered_map UPGRADE_COSTS( {Regeneration_3, {16, 16, 16}}, {Reproduction_1, {4, 4, 4}}, {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_NAMES( {Regeneration_3, "Regeneration 3"}, {Reproduction_1, "Reproduction 1"}, {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_TEXTS( { {Regeneration_1, "Resources yield 2x their base resources per turn."}, @@ -498,7 +518,13 @@ const std::unordered_map UPGRADE_TEXTS( {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_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; } - 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; } @@ -700,6 +746,8 @@ public: FieldMeta *get_attack_marker() { return this->attack_marker; } + void surrender(Player *player); + private: bool changed; bool placing; diff --git a/src/Gui.cpp b/src/Gui.cpp index a9e082b..58680ae 100644 --- a/src/Gui.cpp +++ b/src/Gui.cpp @@ -152,10 +152,7 @@ void UpgradeBox::handle_event(const SDL_Event *event) Timer::MOUSE_LOCKED = false; this->set_visible(false); } - else - { - this->marked_upgrade->handle_event(event); - } + this->marked_upgrade->handle_event(event); changed = true; } else if (event->type == SDL_MOUSEMOTION && this->visible) @@ -230,10 +227,8 @@ void UpgradeBox::render(Renderer *ext_renderer) box->render(ext_renderer); } this->changed = false; - /*SDL_Rect dim = this->upgrades[0]->get_dimensions(); - this->dimensions.w = dim.w; - this->dimensions.h = dim.h * (int) this->upgrades.size(); - */ + this->dimensions = this->upgrades[0]->get_dimensions(); + this->dimensions.h *= this->upgrades.size(); } void Container::render(Renderer *renderer) @@ -438,5 +433,5 @@ void TextInputBox::update_dimensions(SDL_Rect rect) bool TextInputBox::get_active() { - return SDL_IsTextInputActive() == SDL_TRUE; + return SDL_IsTextInputActive() == true; } \ No newline at end of file diff --git a/src/Gui.hpp b/src/Gui.hpp index 6a47ed4..95f0c80 100644 --- a/src/Gui.hpp +++ b/src/Gui.hpp @@ -174,13 +174,11 @@ public: UpgradeBox(Renderer *renderer, SDL_Rect dimensions, SDL_Color color, TTF_Font *font, FieldMeta *field_) : Box(renderer, dimensions, color), field(field_) { - int y = dimensions.y; for (Upgrade upgrade : UPGRADES) { - UpgradeButtonBox *box = new UpgradeButtonBox(renderer, {0, y, dimensions.w, 20}, color, font, this, - upgrade); + UpgradeButtonBox *box = new UpgradeButtonBox(renderer, {0, dimensions.y, dimensions.w, 20}, color, font, + this, upgrade); box->load_text(UPGRADE_NAMES.at(upgrade)); - y += 20; this->marked_upgrade = box; this->upgrades.push_back(box); }