Fixed game flow from winning to restarting

This commit is contained in:
Maximilian Giller 2023-07-10 00:18:38 +02:00
parent 15c9632941
commit 7703128749
12 changed files with 74 additions and 35 deletions

View file

@ -24,7 +24,7 @@
// FPS // FPS
#define FRAME_RATE 60 #define FRAME_RATE 60
#define FRAME_TIME sf::Time(sf::seconds(1.0f / FRAME_RATE)) #define FRAME_TIME sf::Time(sf::seconds(1.0f / FRAME_RATE))
#define FRAME_LIMIT_ENABLED false #define FRAME_LIMIT_ENABLED true
// Window settings // Window settings
#define ANTIALIASINGLEVEL 8 #define ANTIALIASINGLEVEL 8
@ -38,7 +38,6 @@
#define MASKED_HOLE_BORDER_TRANSITION_SIZE 0.2f #define MASKED_HOLE_BORDER_TRANSITION_SIZE 0.2f
#define MASKED_HOLE_DARKNESS_LIMIT 0.5f #define MASKED_HOLE_DARKNESS_LIMIT 0.5f
#define COLLECTABLE_SCALE 5.f #define COLLECTABLE_SCALE 5.f
#define MASKED_SPRITE_LOWER_BOUND -3.f
// Tracking view defaults // Tracking view defaults
#define DEF_TV_IS_ABSOLUTE_FREE_MOVE_THRESHOLD false #define DEF_TV_IS_ABSOLUTE_FREE_MOVE_THRESHOLD false

View file

@ -152,3 +152,8 @@ MultiplayerView::MultiplayerView()
{ {
} }
void MultiplayerView::clear()
{
viewsForPlayers.clear();
}

View file

@ -19,6 +19,8 @@ public:
void update() override; void update() override;
void clear();
void addPlayer(const std::shared_ptr<Player>& player); void addPlayer(const std::shared_ptr<Player>& player);
void removePlayer(const std::shared_ptr<Player>& player); void removePlayer(const std::shared_ptr<Player>& player);

View file

@ -211,10 +211,15 @@ void Game::handleWinning()
void Game::showJoinScreen() void Game::showJoinScreen()
{ {
GlobalLayer::getInstance()->clear();
clearGameObjects();
InputMapper::getInstance()->allowNewInputIdentities = true; InputMapper::getInstance()->allowNewInputIdentities = true;
PlayerCollection::getInstance()->deactivatePlayers();
auto joinScreen = std::make_shared<JoinScreen>(); auto joinScreen = std::make_shared<JoinScreen>();
GlobalLayer::getInstance()->add(joinScreen); GlobalLayer::getInstance()->add(joinScreen);
addGameObject(GlobalLayer::getInstance()); addGameObject(GlobalLayer::getInstance());
PlayerCollection::getInstance()->deactivatePlayers();
addGameObject(PlayerCollection::getInstance()); addGameObject(PlayerCollection::getInstance());
} }

View file

@ -22,12 +22,12 @@ void LevelLoader::loadLevel(const LevelConfig &levelConfig)
game->setLevel(levelConfig); game->setLevel(levelConfig);
InputMapper::getInstance()->allowNewInputIdentities = false; InputMapper::getInstance()->allowNewInputIdentities = false;
PlayerCollection::getInstance()->activatePlayers();
MapSimulation::getInstance()->resetMap(levelConfig.worldMapSize); MapSimulation::getInstance()->resetMap(levelConfig.worldMapSize);
HolesSimulation::getInstance()->clear(); HolesSimulation::getInstance()->clear();
GlobalLayer::getInstance()->clear(); GlobalLayer::getInstance()->clear();
HoleLayout::getInstance()->clear(); HoleLayout::getInstance()->clear();
MultiplayerView::getInstance()->clear();
// Add views // Add views
@ -47,6 +47,8 @@ void LevelLoader::loadLevel(const LevelConfig &levelConfig)
levelRenderer->addChild(PlayerCollection::getInstance()); levelRenderer->addChild(PlayerCollection::getInstance());
PlayerCollection::getInstance()->setSpawnPoints(levelConfig.playerSpawnPoints); PlayerCollection::getInstance()->setSpawnPoints(levelConfig.playerSpawnPoints);
PlayerCollection::getInstance()->resetPlayers();
PlayerCollection::getInstance()->activatePlayers();
// Prepare collectables framework // Prepare collectables framework
auto maxDepth = (int) levelConfig.worldMapSize.x * 2; auto maxDepth = (int) levelConfig.worldMapSize.x * 2;
@ -100,7 +102,7 @@ void LevelLoader::cleanUp()
game->clearGameObjects(); game->clearGameObjects();
game->setLevel(LevelConfig()); game->setLevel(LevelConfig());
HolesSimulation::getInstance()->clear(); HolesSimulation::getInstance()->clear();
PlayerCollection::getInstance()->clear(); PlayerCollection::getInstance()->resetPlayers();
GlobalLayer::getInstance()->clear(); GlobalLayer::getInstance()->clear();
HoleLayout::getInstance()->clear(); HoleLayout::getInstance()->clear();

View file

@ -36,10 +36,11 @@ void MapSimulation::physicsUpdate()
void MapSimulation::resetMap(sf::Vector2<int> worldMapSize) void MapSimulation::resetMap(sf::Vector2<int> worldMapSize)
{ {
// Clear all players // Clear all players
for (auto &mapPlayer: mapPlayersById) for (auto &[id, _]: mapPlayersById)
{ {
removePlayer(mapPlayer.second->player); world->DestroyBody(mapPlayersById[id]->body);
} }
mapPlayersById.clear();
// No gravity, since this a top-down view of the map // No gravity, since this a top-down view of the map
world = std::make_shared<b2World>(b2Vec2(0.0f, 0.0f)); world = std::make_shared<b2World>(b2Vec2(0.0f, 0.0f));

View file

@ -13,7 +13,7 @@ PlayerCollection::PlayerCollection(int maxPlayerCount)
// Create player for existing input identities // Create player for existing input identities
for (auto &inputIdentity: InputMapper::getInstance()->getInputIdentities()) for (auto &inputIdentity: InputMapper::getInstance()->getInputIdentities())
{ {
spawnPlayer(inputIdentity); spawnPlayer(inputIdentity, nullptr);
} }
} }
@ -30,7 +30,6 @@ void PlayerCollection::addPlayer(const std::shared_ptr<Player> &player)
{ {
newPlayerBuffer.push_back(player); newPlayerBuffer.push_back(player);
addDetachedChild(player); addDetachedChild(player);
updateInputIdentityAllowance();
} }
void PlayerCollection::clear() void PlayerCollection::clear()
@ -40,6 +39,8 @@ void PlayerCollection::clear()
newPlayerBuffer.clear(); newPlayerBuffer.clear();
removedPlayerBuffer.clear(); removedPlayerBuffer.clear();
nextSpawnPointIndex = 0;
// Fill in removed players // Fill in removed players
for (auto &child: getChildren()) for (auto &child: getChildren())
{ {
@ -51,7 +52,6 @@ void PlayerCollection::clear()
} }
clearChildren(); clearChildren();
updateInputIdentityAllowance();
} }
void PlayerCollection::lateUpdate() void PlayerCollection::lateUpdate()
@ -82,7 +82,6 @@ void PlayerCollection::removePlayer(const std::shared_ptr<Player> &player)
{ {
removedPlayerBuffer.push_back(player); removedPlayerBuffer.push_back(player);
removeChild(player); removeChild(player);
updateInputIdentityAllowance();
} }
std::vector<std::shared_ptr<Player>> PlayerCollection::getNewPlayers() const std::vector<std::shared_ptr<Player>> PlayerCollection::getNewPlayers() const
@ -99,25 +98,20 @@ void PlayerCollection::setSpawnPoints(std::vector<GridCoordinates> newSpawnPoint
{ {
this->spawnPoints = std::move(newSpawnPoints); this->spawnPoints = std::move(newSpawnPoints);
nextSpawnPointIndex = 0; nextSpawnPointIndex = 0;
// Go through all players and update their spawn points
for (auto &player: getPlayers())
{
auto spawn = spawnPoints[nextSpawnPointIndex];
nextSpawnPointIndex = static_cast<int>((nextSpawnPointIndex + 1) % spawnPoints.size());
player->spawnPosition = TranslatedCoordinates(spawn);
player->coordinates->setGrid(spawn);
}
} }
void PlayerCollection::spawnPlayer(const std::shared_ptr<InputIdentity> &inputIdentity) void PlayerCollection::spawnPlayer(const std::shared_ptr<InputIdentity> &inputIdentity, std::string skin)
{ {
// Get proper Spawn point, if available // Get proper Spawn point, if available
auto spawn = spawnPoints[nextSpawnPointIndex]; auto spawn = spawnPoints[nextSpawnPointIndex];
nextSpawnPointIndex = static_cast<int>((nextSpawnPointIndex + 1) % spawnPoints.size()); nextSpawnPointIndex = static_cast<int>((nextSpawnPointIndex + 1) % spawnPoints.size());
auto player = std::make_shared<Player>(inputIdentity, getNextSkin(), spawn); if (skin.empty())
{
skin = getNextSkin();
}
auto player = std::make_shared<Player>(inputIdentity, skin, spawn);
addPlayer(player); addPlayer(player);
} }
@ -152,11 +146,6 @@ int PlayerCollection::getMaxPlayerCount() const
return maxPlayerCount; return maxPlayerCount;
} }
void PlayerCollection::updateInputIdentityAllowance() const
{
InputMapper::getInstance()->allowNewInputIdentities = getPlayers().size() < maxPlayerCount;
}
std::shared_ptr<Player> PlayerCollection::getClosestPlayer(const TranslatedCoordinates &point) const std::shared_ptr<Player> PlayerCollection::getClosestPlayer(const TranslatedCoordinates &point) const
{ {
std::shared_ptr<Player> closestPlayer = nullptr; std::shared_ptr<Player> closestPlayer = nullptr;
@ -216,7 +205,6 @@ void PlayerCollection::activatePlayers()
for (auto player: getPlayers()) for (auto player: getPlayers())
{ {
player->setPassiveMode(false); player->setPassiveMode(false);
newPlayerBuffer.push_back(player);
} }
} }
@ -227,3 +215,15 @@ void PlayerCollection::deactivatePlayers()
player->setPassiveMode(true); player->setPassiveMode(true);
} }
} }
void PlayerCollection::resetPlayers()
{
// Recreate players
auto oldPlayers = getPlayers();
clear();
removedPlayerBuffer.clear();
for (auto &player: oldPlayers)
{
spawnPlayer(player->getInput(), player->getSkinName());
}
}

View file

@ -34,6 +34,8 @@ public:
void clear(); void clear();
void resetPlayers();
void activatePlayers(); void activatePlayers();
void deactivatePlayers(); void deactivatePlayers();
@ -46,15 +48,13 @@ private:
std::vector<GridCoordinates> spawnPoints; std::vector<GridCoordinates> spawnPoints;
int nextSpawnPointIndex = 0; int nextSpawnPointIndex = 0;
void spawnPlayer(const std::shared_ptr<InputIdentity> &inputIdentity); void spawnPlayer(const std::shared_ptr<InputIdentity> &inputIdentity, std::string skin = "");
std::vector<int> takenSkinIndices = {}; std::vector<int> takenSkinIndices = {};
std::string getNextSkin(); std::string getNextSkin();
int maxPlayerCount; int maxPlayerCount;
void updateInputIdentityAllowance() const;
}; };

View file

@ -9,7 +9,7 @@
std::map<std::string, LevelConfig> const all_levels = { std::map<std::string, LevelConfig> const all_levels = {
{"default", LevelConfig("Default", {"default", LevelConfig("Default",
30, 5,
{ {
{0, 0}, {0, 0},
{18, 18}, {18, 18},

View file

@ -16,7 +16,7 @@ public:
void draw(sf::RenderWindow *window) override; void draw(sf::RenderWindow *window) override;
private: private:
int lastPlayerCount = 0; int lastPlayerCount = -1;
std::vector<std::shared_ptr<sf::Drawable>> playerList = {}; std::vector<std::shared_ptr<sf::Drawable>> playerList = {};
std::vector<std::shared_ptr<GameObject>> playerListObjects = {}; std::vector<std::shared_ptr<GameObject>> playerListObjects = {};

View file

@ -2,6 +2,7 @@
#include "../typography/font_manager.hpp" #include "../typography/font_manager.hpp"
#include "../texture_config.h" #include "../texture_config.h"
#include "../game/game.h"
WinnerScreen::WinnerScreen(const std::shared_ptr<Player> &winner) WinnerScreen::WinnerScreen(const std::shared_ptr<Player> &winner)
: winner(winner) : winner(winner)
@ -29,6 +30,28 @@ WinnerScreen::WinnerScreen(const std::shared_ptr<Player> &winner)
points->setString("Points: " + std::to_string(winner->getPoints())); points->setString("Points: " + std::to_string(winner->getPoints()));
points->setCharacterSize(20); points->setCharacterSize(20);
points->setFillColor(sf::Color::White); points->setFillColor(sf::Color::White);
points->setPosition(REFERENCE_SIZE.x / 2 - points->getGlobalBounds().width / 2, REFERENCE_SIZE.y - 300); points->setPosition(REFERENCE_SIZE.x / 2 - points->getGlobalBounds().width / 2, REFERENCE_SIZE.y - 400);
add(points); add(points);
// Show press button to continue
auto input = winner->getInput();
auto confirmButton = DEVICE_GROUP_CONFIRM.at(input->deviceGroup);
auto confirmText = std::make_shared<sf::Text>();
confirmText->setFont(*FontManager::getInstance()->getDefaultFont());
confirmText->setString("Press " + confirmButton + " to continue");
confirmText->setCharacterSize(20);
confirmText->setFillColor(sf::Color::White);
confirmText->setStyle(sf::Text::Bold);
confirmText->setPosition(REFERENCE_SIZE.x / 2 - confirmText->getGlobalBounds().width / 2, REFERENCE_SIZE.y - 200);
add(confirmText);
}
void WinnerScreen::update()
{
GameObject::update();
auto input = winner->getInput();
if (input->isPerformingAction(GameAction::CONFIRM)) {
Game::getInstance()->showJoinScreen();
}
} }

View file

@ -11,6 +11,8 @@ class WinnerScreen : public Screen
public: public:
WinnerScreen(const std::shared_ptr<Player> &winner); WinnerScreen(const std::shared_ptr<Player> &winner);
void update() override;
private: private:
std::shared_ptr<Player> winner; std::shared_ptr<Player> winner;
}; };