Implemented basic winner screen

This commit is contained in:
Maximilian Giller 2023-07-09 21:56:14 +02:00
parent 241d8119ce
commit 24cc3cfd6b
11 changed files with 174 additions and 3 deletions

View file

@ -136,7 +136,9 @@ set(SOURCES
src/game/layer/global_layer.cpp src/game/layer/global_layer.cpp
src/game/layer/global_layer.hpp src/game/layer/global_layer.hpp
src/typography/font_manager.cpp src/typography/font_manager.cpp
src/typography/font_manager.hpp) src/typography/font_manager.hpp
src/screens/winner_screen.cpp
src/screens/winner_screen.hpp src/screens/screen.cpp src/screens/screen.hpp)
set(PHYSICS_00_SOURCES set(PHYSICS_00_SOURCES
src/prototypes/physics_00.cpp) src/prototypes/physics_00.cpp)

View file

@ -26,6 +26,7 @@
// Window settings // Window settings
#define ANTIALIASINGLEVEL 8 #define ANTIALIASINGLEVEL 8
#define KEY_REPEAT_ENABLED false #define KEY_REPEAT_ENABLED false
#define REFERENCE_SIZE sf::Vector2f(1920, 1080)
// Graphic settings // Graphic settings
#define ISOMETRIC_SKEW (16.f/32.f) #define ISOMETRIC_SKEW (16.f/32.f)

View file

@ -7,6 +7,8 @@
#include "physics/map/map_simulation.hpp" #include "physics/map/map_simulation.hpp"
#include "../logging/easylogging++.h" #include "../logging/easylogging++.h"
#include "layer/global_layer.hpp" #include "layer/global_layer.hpp"
#include "player/player_collection.hpp"
#include "../screens/winner_screen.hpp"
Game::Game(std::shared_ptr<sf::RenderWindow> window) : window(std::move(window)) Game::Game(std::shared_ptr<sf::RenderWindow> window) : window(std::move(window))
{ {
@ -65,6 +67,8 @@ void Game::addGameObject(const std::shared_ptr<GameObject> &gameObject)
void Game::update() void Game::update()
{ {
handleWinning();
// Add new game objects // Add new game objects
for (const auto &gameObject: gameObjectsBuffer) for (const auto &gameObject: gameObjectsBuffer)
{ {
@ -152,3 +156,54 @@ void Game::startCountdown(int durationInSeconds)
countdown = std::make_shared<Countdown>(durationInSeconds); countdown = std::make_shared<Countdown>(durationInSeconds);
GlobalLayer::getInstance()->add(countdown); GlobalLayer::getInstance()->add(countdown);
} }
std::shared_ptr<Player> Game::checkForWinner()
{
std::vector<std::shared_ptr<Player>> players = PlayerCollection::getInstance()->getPlayers();
// Has timer run out or is only one player left?
if (!countdown->isFinished() && players.size() > 1)
{
return nullptr;
}
// Return player with highest score as winner
std::shared_ptr<Player> winner = nullptr;
for (const auto &player: players)
{
if (winner == nullptr || player->getPoints() > winner->getPoints())
{
winner = player;
}
}
return winner;
}
void Game::handleWinning()
{
if (!isLevelLoaded())
{
return;
}
// Any players in game?
if (PlayerCollection::getInstance()->getPlayers().empty())
{
return;
}
std::shared_ptr<Player> winner = checkForWinner();
if (winner == nullptr)
{
return;
}
LOG(INFO) << "Player " << winner->getPlayerId() << " won the game with " << winner->getPoints() << " points.";
// Stop game and show winner
LevelLoader::cleanUp();
auto winnerScreen = std::make_shared<WinnerScreen>(winner);
GlobalLayer::getInstance()->add(winnerScreen);
addGameObject(GlobalLayer::getInstance());
}

View file

@ -51,6 +51,10 @@ private:
void drawFrame(); void drawFrame();
void update(); void update();
std::shared_ptr<Player> checkForWinner();
void handleWinning();
}; };

View file

@ -25,8 +25,8 @@ GlobalLayer::GlobalLayer()
{ {
// Reference screen size of 1920x1080 // Reference screen size of 1920x1080
TrackingViewOptions options = { TrackingViewOptions options = {
.minViewSize = sf::Vector2f{10, 1080}, .minViewSize = sf::Vector2f{10, REFERENCE_SIZE.y},
.initialCenter = sf::Vector2f{1920, 1080} / 2.0f, .initialCenter = REFERENCE_SIZE / 2.0f,
}; };
view = std::make_shared<TrackingView>(options); view = std::make_shared<TrackingView>(options);

View file

@ -90,3 +90,16 @@ void LevelLoader::spawnCollectable(const CollectableInLevel &collectableInfo)
CollectablesCollection::getInstance()->add(collectable); CollectablesCollection::getInstance()->add(collectable);
HolesSimulation::getInstance()->addCollectable(collectable); HolesSimulation::getInstance()->addCollectable(collectable);
} }
void LevelLoader::cleanUp()
{
auto game = Game::getInstance();
game->clearGameObjects();
game->setLevel(LevelConfig());
HolesSimulation::getInstance()->clear();
PlayerCollection::getInstance()->clear();
GlobalLayer::getInstance()->clear();
HoleLayout::getInstance()->clear();
LOG(INFO) << "Cleaned up level.";
}

View file

@ -12,6 +12,8 @@ public:
static void loadLevel(const std::string &levelName); static void loadLevel(const std::string &levelName);
static void cleanUp();
static void spawnCollectable(const CollectableInLevel &collectableInfo); static void spawnCollectable(const CollectableInLevel &collectableInfo);
}; };

21
src/screens/screen.cpp Normal file
View file

@ -0,0 +1,21 @@
#include "screen.hpp"
void Screen::draw(sf::RenderWindow *window)
{
GameObject::draw(window);
for(auto &drawable : drawables)
{
window->draw(*drawable);
}
}
void Screen::add(const std::shared_ptr<sf::Drawable>& drawable)
{
drawables.push_back(drawable);
}
void Screen::add(const std::shared_ptr<GameObject>& gameObject)
{
addDetachedChild(gameObject);
}

20
src/screens/screen.hpp Normal file
View file

@ -0,0 +1,20 @@
#ifndef HOLESOME_SCREEN_HPP
#define HOLESOME_SCREEN_HPP
#include "../game/game_object.h"
class Screen : public GameObject
{
public:
void draw(sf::RenderWindow *window) override;
protected:
std::vector<std::shared_ptr<sf::Drawable>> drawables;
void add(const std::shared_ptr<sf::Drawable>& drawable);
void add(const std::shared_ptr<GameObject>& gameObject);
};
#endif //HOLESOME_SCREEN_HPP

View file

@ -0,0 +1,34 @@
#include "winner_screen.hpp"
#include "../typography/font_manager.hpp"
#include "../texture_config.h"
WinnerScreen::WinnerScreen(const std::shared_ptr<Player> &winner)
: winner(winner)
{
// Winner title
auto winnerTitle = std::make_shared<sf::Text>();
winnerTitle->setFont(*FontManager::getInstance()->getDefaultFont());
winnerTitle->setString("Winner");
winnerTitle->setCharacterSize(50);
winnerTitle->setFillColor(sf::Color::Yellow);
winnerTitle->setStyle(sf::Text::Bold);
winnerTitle->setPosition(REFERENCE_SIZE.x / 2 - winnerTitle->getGlobalBounds().width / 2, 100);
add(winnerTitle);
// Add graphic of the winner
auto winnerGraphic = std::make_shared<VersatileSprite>(PLAYER_SKIN);
auto center = REFERENCE_SIZE / 2.f;
auto winnerPosition = center - winnerGraphic->getSize() / 2.f;
winnerGraphic->coordinates->setIsometric(IsometricCoordinates(winnerPosition));
add(winnerGraphic);
// Show points below
auto points = std::make_shared<sf::Text>();
points->setFont(*FontManager::getInstance()->getDefaultFont());
points->setString("Points: " + std::to_string(winner->getPoints()));
points->setCharacterSize(20);
points->setFillColor(sf::Color::White);
points->setPosition(REFERENCE_SIZE.x / 2 - points->getGlobalBounds().width / 2, REFERENCE_SIZE.y - 300);
add(points);
}

View file

@ -0,0 +1,19 @@
#ifndef HOLESOME_WINNER_SCREEN_HPP
#define HOLESOME_WINNER_SCREEN_HPP
#include <memory>
#include "../game/player/player.hpp"
#include "screen.hpp"
class WinnerScreen : public Screen
{
public:
WinnerScreen(const std::shared_ptr<Player> &winner);
private:
std::shared_ptr<Player> winner;
};
#endif //HOLESOME_WINNER_SCREEN_HPP