From ac435f8e4f62146ae9061723b4ff3a1bdeb7430f Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Mon, 10 Jul 2023 22:34:04 +0200 Subject: [PATCH] Attack refined --- src/config.h | 5 +- src/game/camera/multiplayer_view.cpp | 9 ---- src/game/game.cpp | 3 +- src/game/player/player.cpp | 38 +++++++++------ src/game/player/player.hpp | 5 ++ src/screens/winner_screen.cpp | 73 ++++++++++++++++++++++++++-- src/screens/winner_screen.hpp | 6 ++- 7 files changed, 107 insertions(+), 32 deletions(-) diff --git a/src/config.h b/src/config.h index 1ce18d6..805eed0 100644 --- a/src/config.h +++ b/src/config.h @@ -17,13 +17,13 @@ #define DEFAULT_MAX_PLAYER_NUMBER 4 #define MIN_PLAYER_COUNT 2 #define REDUCED_POINT_DELTA_FOR_ATTACK 20 -#define ATTACK_PER_SECOND 5.f +#define ATTACK_PER_SECOND 1.f #define PLAYER_ALIVE_THRESHOLD -10 // World #define WORLD_GRAVITY b2Vec2(0.f, -9.8f) #define SKY_HEIGHT_SCALE 5.f -#define CONSIDER_COLLECTABLE_DEPTH_MOVEMENT false // Might cost perfomance and is currently not in use +#define CONSIDER_COLLECTABLE_DEPTH_MOVEMENT false // Might cost performance and is currently not in use // FPS #define FRAME_RATE 60 @@ -53,7 +53,6 @@ #define DEF_TV_IS_ABSOLUTE_VIEW_SIZE_PADDING false #define DEF_TV_VIEW_SIZE_PADDING sf::Vector2f(2.f, 2.f) #define MP_VIEW_ADD_NEW_PLAYERS true -#define MP_VIEW_REMOVE_DISCONNECTED_PLAYERS true #define MP_VIEW_BORDER_COLOR sf::Color::Black // Simulations diff --git a/src/game/camera/multiplayer_view.cpp b/src/game/camera/multiplayer_view.cpp index 60d8592..1b116f6 100644 --- a/src/game/camera/multiplayer_view.cpp +++ b/src/game/camera/multiplayer_view.cpp @@ -27,15 +27,6 @@ void MultiplayerView::update() } } - // Remove old - if (MP_VIEW_REMOVE_DISCONNECTED_PLAYERS) - { - for (const auto &player: PlayerCollection::getInstance()->getRemovedPlayers()) - { - removePlayer(player); - } - } - GameObject::update(); } diff --git a/src/game/game.cpp b/src/game/game.cpp index 1e76cdf..d874424 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -205,8 +205,9 @@ void Game::handleWinning() LOG(INFO) << "Player " << winner->getPlayerId() << " won the game with " << winner->getPoints() << " points."; // Stop game and show winner + auto players = PlayerCollection::getInstance()->getPlayers(); LevelLoader::cleanUp(); - auto winnerScreen = std::make_shared(winner); + auto winnerScreen = std::make_shared(players); GlobalLayer::getInstance()->add(winnerScreen); addGameObject(GlobalLayer::getInstance()); } diff --git a/src/game/player/player.cpp b/src/game/player/player.cpp index 3b4a1a6..5778096 100644 --- a/src/game/player/player.cpp +++ b/src/game/player/player.cpp @@ -104,10 +104,8 @@ void Player::updateRadiusBasedOnPoints() void Player::consume(int consumedPoints) { - this->points += consumedPoints; + setPoints(getPoints() + consumedPoints); LOG(INFO) << "Player " << playerId << " consumed " << consumedPoints << " points. Total: " << this->points; - - updateRadiusBasedOnPoints(); } std::string Player::getSkinName() const @@ -143,7 +141,7 @@ void Player::setCollidingPlayers(const std::vector& collidingPlayerIds) void Player::handlePlayerCollisions() { - if (collidingPlayers.empty()) + if (collidingPlayers.empty() || !isAlive) { return; } @@ -159,19 +157,12 @@ void Player::handlePlayerCollisions() } // Reduce points if necessary - float damage = numAttackingPlayers * ATTACK_PER_SECOND * FRAME_TIME.asSeconds(); - points -= damage; - + float damage = numAttackingPlayers * ATTACK_PER_SECOND * FRAME_TIME.asSeconds() * (getPoints() + 1 + abs(PLAYER_ALIVE_THRESHOLD)); if (damage > 0) { - LOG(INFO) << "Player " << playerId << " lost " << damage << " points due to collisions. Total: " << points; + LOG(INFO) << "Player " << playerId << " lost " << damage << " points due to collisions. Total: " << points - damage; } - if (points <= PLAYER_ALIVE_THRESHOLD) - { - setAlive(false); - } - - updateRadiusBasedOnPoints(); + setPoints(points - damage); } bool Player::getAlive() const @@ -213,3 +204,22 @@ void Player::draw(sf::RenderWindow *window) coordinates->isometric().y - text.getLocalBounds().height / 2.f); window->draw(text); } + +void Player::setPoints(float newPoints) +{ + points = newPoints; + updateRadiusBasedOnPoints(); + + if (points > highestPoints) + { + highestPoints = points; + } else if (points <= PLAYER_ALIVE_THRESHOLD) + { + setAlive(false); + } +} + +int Player::getMaxPoints() const +{ + return highestPoints; +} diff --git a/src/game/player/player.hpp b/src/game/player/player.hpp index 694e029..73eb09d 100644 --- a/src/game/player/player.hpp +++ b/src/game/player/player.hpp @@ -31,6 +31,8 @@ public: [[nodiscard]] int getPoints() const; + int getMaxPoints() const; + void consume(int consumedPoints); std::string getSkinName() const; @@ -54,12 +56,15 @@ private: std::shared_ptr skinSprite; std::vector> collidingPlayers; + void setPoints(float newPoints); + bool passiveMode = true; bool isAlive = true; std::string skinName; float points = DEFAULT_PLAYER_POINTS; + float highestPoints = DEFAULT_PLAYER_POINTS; int playerId; static inline int playerCreationCounter = 0; diff --git a/src/screens/winner_screen.cpp b/src/screens/winner_screen.cpp index 67b0cae..59669c1 100644 --- a/src/screens/winner_screen.cpp +++ b/src/screens/winner_screen.cpp @@ -4,8 +4,27 @@ #include "../texture_config.h" #include "../game/game.h" -WinnerScreen::WinnerScreen(const std::shared_ptr &winner) - : winner(winner) +WinnerScreen::WinnerScreen(std::vector> players) +{ + // Sort players by points descending + std::sort(players.begin(), players.end(), [](const std::shared_ptr &a, const std::shared_ptr &b) + { + return a->getPoints() > b->getPoints(); + }); + + // Get winner + winner = players[0]; + createWinnerTribute(winner); + + if (players.size() > 1) + { + // Remove winner from list + players.erase(players.begin()); + listRemainingScores(players); + } +} + +void WinnerScreen::createWinnerTribute(const std::shared_ptr &winner) { // Winner title auto winnerTitle = std::make_shared(); @@ -18,7 +37,8 @@ WinnerScreen::WinnerScreen(const std::shared_ptr &winner) add(winnerTitle); // Add graphic of the winner - auto winnerGraphic = std::make_shared(winner->getSkinName(), sf::Vector2f(500, 500 * ISOMETRIC_SKEW)); + auto winnerGraphic = std::make_shared(winner->getSkinName(), + sf::Vector2f(500, 500 * ISOMETRIC_SKEW)); auto center = REFERENCE_SIZE / 2.f; auto winnerPosition = center - winnerGraphic->getSize() / 2.f; winnerGraphic->coordinates->setIsometric(IsometricCoordinates(winnerPosition)); @@ -33,6 +53,19 @@ WinnerScreen::WinnerScreen(const std::shared_ptr &winner) points->setPosition(REFERENCE_SIZE.x / 2 - points->getGlobalBounds().width / 2, REFERENCE_SIZE.y - 400); add(points); + // Show points below + if (winner->getMaxPoints() != winner->getPoints()) + { + auto highestPoints = std::make_shared(); + highestPoints->setFont(*FontManager::getInstance()->getDefaultFont()); + highestPoints->setString("Highest Points: " + std::to_string(winner->getPoints())); + highestPoints->setCharacterSize(20); + highestPoints->setFillColor(sf::Color::Yellow); + highestPoints->setPosition(REFERENCE_SIZE.x / 2 - highestPoints->getGlobalBounds().width / 2, + REFERENCE_SIZE.y - 360); + add(highestPoints); + } + // Show press button to continue auto input = winner->getInput(); auto confirmButton = DEVICE_GROUP_CONFIRM.at(input->deviceGroup); @@ -51,7 +84,39 @@ void WinnerScreen::update() GameObject::update(); auto input = winner->getInput(); - if (input->isPerformingAction(GameAction::CONFIRM)) { + if (input->isPerformingAction(GameAction::CONFIRM)) + { Game::getInstance()->showJoinScreen(); } } + +void WinnerScreen::listRemainingScores(const std::vector> &remainingPlayers) +{ + int leftPadding = 150; + int topPadding = 500; + int betweenPadding = 50; + int height = 64; + int index = 0; + for (const auto &player: remainingPlayers) + { + // Player skin + auto playerGraphic = std::make_shared(player->getSkinName(), + sf::Vector2f(height / ISOMETRIC_SKEW, height)); + playerGraphic->coordinates->setIsometric( + IsometricCoordinates(leftPadding, topPadding + index * (height + betweenPadding))); + add(playerGraphic); + + // Points + auto points = std::make_shared(); + points->setFont(*FontManager::getInstance()->getDefaultFont()); + points->setString(std::to_string(player->getMaxPoints())); + points->setCharacterSize(20); + points->setFillColor(sf::Color::White); + points->setPosition(leftPadding + playerGraphic->getSize().x + 10, + topPadding + index * (height + betweenPadding) + height / 2.f - + points->getGlobalBounds().height / 2.f); + add(points); + + index++; + } +} diff --git a/src/screens/winner_screen.hpp b/src/screens/winner_screen.hpp index b7c3b52..6c15d0c 100644 --- a/src/screens/winner_screen.hpp +++ b/src/screens/winner_screen.hpp @@ -9,12 +9,16 @@ class WinnerScreen : public Screen { public: - WinnerScreen(const std::shared_ptr &winner); + WinnerScreen(std::vector> players); void update() override; private: std::shared_ptr winner; + + void createWinnerTribute(const std::shared_ptr &winner); + + void listRemainingScores(const std::vector>& remainingPlayers); };