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
#define FRAME_RATE 60
#define FRAME_TIME sf::Time(sf::seconds(1.0f / FRAME_RATE))
#define FRAME_LIMIT_ENABLED false
#define FRAME_LIMIT_ENABLED true
// Window settings
#define ANTIALIASINGLEVEL 8
@ -38,7 +38,6 @@
#define MASKED_HOLE_BORDER_TRANSITION_SIZE 0.2f
#define MASKED_HOLE_DARKNESS_LIMIT 0.5f
#define COLLECTABLE_SCALE 5.f
#define MASKED_SPRITE_LOWER_BOUND -3.f
// Tracking view defaults
#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 clear();
void addPlayer(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()
{
GlobalLayer::getInstance()->clear();
clearGameObjects();
InputMapper::getInstance()->allowNewInputIdentities = true;
PlayerCollection::getInstance()->deactivatePlayers();
auto joinScreen = std::make_shared<JoinScreen>();
GlobalLayer::getInstance()->add(joinScreen);
addGameObject(GlobalLayer::getInstance());
PlayerCollection::getInstance()->deactivatePlayers();
addGameObject(PlayerCollection::getInstance());
}

View file

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

View file

@ -36,10 +36,11 @@ void MapSimulation::physicsUpdate()
void MapSimulation::resetMap(sf::Vector2<int> worldMapSize)
{
// 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
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
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);
addDetachedChild(player);
updateInputIdentityAllowance();
}
void PlayerCollection::clear()
@ -40,6 +39,8 @@ void PlayerCollection::clear()
newPlayerBuffer.clear();
removedPlayerBuffer.clear();
nextSpawnPointIndex = 0;
// Fill in removed players
for (auto &child: getChildren())
{
@ -51,7 +52,6 @@ void PlayerCollection::clear()
}
clearChildren();
updateInputIdentityAllowance();
}
void PlayerCollection::lateUpdate()
@ -82,7 +82,6 @@ void PlayerCollection::removePlayer(const std::shared_ptr<Player> &player)
{
removedPlayerBuffer.push_back(player);
removeChild(player);
updateInputIdentityAllowance();
}
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);
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
auto spawn = spawnPoints[nextSpawnPointIndex];
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);
}
@ -152,11 +146,6 @@ int PlayerCollection::getMaxPlayerCount() const
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> closestPlayer = nullptr;
@ -216,7 +205,6 @@ void PlayerCollection::activatePlayers()
for (auto player: getPlayers())
{
player->setPassiveMode(false);
newPlayerBuffer.push_back(player);
}
}
@ -227,3 +215,15 @@ void PlayerCollection::deactivatePlayers()
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 resetPlayers();
void activatePlayers();
void deactivatePlayers();
@ -46,15 +48,13 @@ private:
std::vector<GridCoordinates> spawnPoints;
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::string getNextSkin();
int maxPlayerCount;
void updateInputIdentityAllowance() const;
};

View file

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

View file

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

View file

@ -2,6 +2,7 @@
#include "../typography/font_manager.hpp"
#include "../texture_config.h"
#include "../game/game.h"
WinnerScreen::WinnerScreen(const std::shared_ptr<Player> &winner)
: winner(winner)
@ -29,6 +30,28 @@ WinnerScreen::WinnerScreen(const std::shared_ptr<Player> &winner)
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);
points->setPosition(REFERENCE_SIZE.x / 2 - points->getGlobalBounds().width / 2, REFERENCE_SIZE.y - 400);
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:
WinnerScreen(const std::shared_ptr<Player> &winner);
void update() override;
private:
std::shared_ptr<Player> winner;
};