diff --git a/src/config.h b/src/config.h index b80cbcc..3438257 100644 --- a/src/config.h +++ b/src/config.h @@ -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 diff --git a/src/game/camera/multiplayer_view.cpp b/src/game/camera/multiplayer_view.cpp index bff02ed..60d8592 100644 --- a/src/game/camera/multiplayer_view.cpp +++ b/src/game/camera/multiplayer_view.cpp @@ -152,3 +152,8 @@ MultiplayerView::MultiplayerView() { } + +void MultiplayerView::clear() +{ + viewsForPlayers.clear(); +} diff --git a/src/game/camera/multiplayer_view.hpp b/src/game/camera/multiplayer_view.hpp index 6a608a8..9f7f364 100644 --- a/src/game/camera/multiplayer_view.hpp +++ b/src/game/camera/multiplayer_view.hpp @@ -19,6 +19,8 @@ public: void update() override; + void clear(); + void addPlayer(const std::shared_ptr& player); void removePlayer(const std::shared_ptr& player); diff --git a/src/game/game.cpp b/src/game/game.cpp index f8bd729..c99d070 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -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(); GlobalLayer::getInstance()->add(joinScreen); + addGameObject(GlobalLayer::getInstance()); - PlayerCollection::getInstance()->deactivatePlayers(); addGameObject(PlayerCollection::getInstance()); } diff --git a/src/game/level/level_loader.cpp b/src/game/level/level_loader.cpp index e4ed98a..bb30aa3 100644 --- a/src/game/level/level_loader.cpp +++ b/src/game/level/level_loader.cpp @@ -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(); diff --git a/src/game/physics/map/map_simulation.cpp b/src/game/physics/map/map_simulation.cpp index 39650a1..97fa30a 100644 --- a/src/game/physics/map/map_simulation.cpp +++ b/src/game/physics/map/map_simulation.cpp @@ -36,10 +36,11 @@ void MapSimulation::physicsUpdate() void MapSimulation::resetMap(sf::Vector2 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(b2Vec2(0.0f, 0.0f)); diff --git a/src/game/player/player_collection.cpp b/src/game/player/player_collection.cpp index 39f75be..c326942 100644 --- a/src/game/player/player_collection.cpp +++ b/src/game/player/player_collection.cpp @@ -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) { 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) { removedPlayerBuffer.push_back(player); removeChild(player); - updateInputIdentityAllowance(); } std::vector> PlayerCollection::getNewPlayers() const @@ -99,25 +98,20 @@ void PlayerCollection::setSpawnPoints(std::vector 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((nextSpawnPointIndex + 1) % spawnPoints.size()); - - player->spawnPosition = TranslatedCoordinates(spawn); - player->coordinates->setGrid(spawn); - } } -void PlayerCollection::spawnPlayer(const std::shared_ptr &inputIdentity) +void PlayerCollection::spawnPlayer(const std::shared_ptr &inputIdentity, std::string skin) { // Get proper Spawn point, if available auto spawn = spawnPoints[nextSpawnPointIndex]; nextSpawnPointIndex = static_cast((nextSpawnPointIndex + 1) % spawnPoints.size()); - auto player = std::make_shared(inputIdentity, getNextSkin(), spawn); + if (skin.empty()) + { + skin = getNextSkin(); + } + + auto player = std::make_shared(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 PlayerCollection::getClosestPlayer(const TranslatedCoordinates &point) const { std::shared_ptr 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()); + } +} diff --git a/src/game/player/player_collection.hpp b/src/game/player/player_collection.hpp index fd21d53..6c47771 100644 --- a/src/game/player/player_collection.hpp +++ b/src/game/player/player_collection.hpp @@ -34,6 +34,8 @@ public: void clear(); + void resetPlayers(); + void activatePlayers(); void deactivatePlayers(); @@ -46,15 +48,13 @@ private: std::vector spawnPoints; int nextSpawnPointIndex = 0; - void spawnPlayer(const std::shared_ptr &inputIdentity); + void spawnPlayer(const std::shared_ptr &inputIdentity, std::string skin = ""); std::vector takenSkinIndices = {}; std::string getNextSkin(); int maxPlayerCount; - - void updateInputIdentityAllowance() const; }; diff --git a/src/levels.hpp b/src/levels.hpp index 1a71fbb..18bc5db 100644 --- a/src/levels.hpp +++ b/src/levels.hpp @@ -9,7 +9,7 @@ std::map const all_levels = { {"default", LevelConfig("Default", - 30, + 5, { {0, 0}, {18, 18}, diff --git a/src/screens/join_screen.hpp b/src/screens/join_screen.hpp index 4caad06..a694256 100644 --- a/src/screens/join_screen.hpp +++ b/src/screens/join_screen.hpp @@ -16,7 +16,7 @@ public: void draw(sf::RenderWindow *window) override; private: - int lastPlayerCount = 0; + int lastPlayerCount = -1; std::vector> playerList = {}; std::vector> playerListObjects = {}; diff --git a/src/screens/winner_screen.cpp b/src/screens/winner_screen.cpp index 7f00710..67b0cae 100644 --- a/src/screens/winner_screen.cpp +++ b/src/screens/winner_screen.cpp @@ -2,6 +2,7 @@ #include "../typography/font_manager.hpp" #include "../texture_config.h" +#include "../game/game.h" WinnerScreen::WinnerScreen(const std::shared_ptr &winner) : winner(winner) @@ -29,6 +30,28 @@ WinnerScreen::WinnerScreen(const std::shared_ptr &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(); + 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(); + } } diff --git a/src/screens/winner_screen.hpp b/src/screens/winner_screen.hpp index 234c90f..b7c3b52 100644 --- a/src/screens/winner_screen.hpp +++ b/src/screens/winner_screen.hpp @@ -11,6 +11,8 @@ class WinnerScreen : public Screen public: WinnerScreen(const std::shared_ptr &winner); + void update() override; + private: std::shared_ptr winner; };