#include "map_simulation.hpp" #include "../../../config.h" #include "../../player/player_collection.hpp" MapSimulation::MapSimulation() { mapPlayersById = std::map>(); } std::shared_ptr MapSimulation::getInstance() { if (singletonInstance == nullptr) { singletonInstance = std::make_shared(); } return singletonInstance; } void MapSimulation::physicsUpdate() { // Update simulation positions for (auto &mapPlayer: mapPlayersById) { mapPlayer.second->updateSimulationPosition(); } world->Step(FRAME_TIME.asSeconds(), MAPSIM_VELOCITY_ITERATIONS, MAPSIM_POSITION_ITERATIONS); // Update player positions for (auto &mapPlayer: mapPlayersById) { mapPlayer.second->updatePlayerPosition(); } } void MapSimulation::resetMap(sf::Vector2 worldMapSize) { // Clear all players for (auto &mapPlayer: mapPlayersById) { removePlayer(mapPlayer.second->player); } // No gravity, since this a top-down view of the map world = std::make_shared(b2Vec2(0.0f, 0.0f)); // Create map borders constructSquareObstacle(-MAPSIM_WALL_THICKNESS, -MAPSIM_WALL_THICKNESS, 0, worldMapSize.y + MAPSIM_WALL_THICKNESS); // Bottom left constructSquareObstacle(0, -MAPSIM_WALL_THICKNESS, worldMapSize.x, 0); // Bottom right constructSquareObstacle(worldMapSize.x, -MAPSIM_WALL_THICKNESS, worldMapSize.x + MAPSIM_WALL_THICKNESS, worldMapSize.y + MAPSIM_WALL_THICKNESS); // Top right constructSquareObstacle(0, worldMapSize.y, worldMapSize.x, worldMapSize.y + MAPSIM_WALL_THICKNESS); // Top left } void MapSimulation::constructSquareObstacle(float minX, float minY, float maxX, float maxY) { b2BodyDef bodyDef; bodyDef.type = b2_staticBody; bodyDef.position.Set((maxX + minX) / 2.f, (maxY + minY) / 2.f); b2Body *body = world->CreateBody(&bodyDef); b2PolygonShape shape; shape.SetAsBox((maxX - minX) / 2.f, (maxY - minY) / 2.f); b2FixtureDef fixtureDef; fixtureDef.shape = &shape; fixtureDef.density = 1.0f; body->CreateFixture(&fixtureDef); } void MapSimulation::addPlayer(const std::shared_ptr &player) { b2BodyDef bodyDef; bodyDef.type = b2_dynamicBody; bodyDef.position.Set(player->spawnPosition.world().x, player->spawnPosition.world().y); b2Body *body = world->CreateBody(&bodyDef); mapPlayersById[player->getPlayerId()] = std::make_shared(player, body); } void MapSimulation::update() { // Update players from player collection // New player for (auto &player: PlayerCollection::getInstance()->getNewPlayers()) { addPlayer(player); } // Removed players for (auto &player: PlayerCollection::getInstance()->getRemovedPlayers()) { removePlayer(player); } } void MapSimulation::removePlayer(std::shared_ptr &player) { // Remove body from simulation int playerId = player->getPlayerId(); world->DestroyBody(mapPlayersById[playerId]->body); mapPlayersById.erase(playerId); }