diff --git a/src/game/camera/tracking_view.cpp b/src/game/camera/tracking_view.cpp index f80d969..5c0ef1e 100644 --- a/src/game/camera/tracking_view.cpp +++ b/src/game/camera/tracking_view.cpp @@ -3,9 +3,9 @@ TrackingView::TrackingView(TrackingViewOptions options) : options(options), view(nullptr), - hasViewChanged(false) -{ - trackables = std::vector(); + hasViewChanged(false), + trackables({}) +{; marker = new CircleObject(DB_CIRCLE_RADIUS, sf::Color::Yellow); Game::getInstance()->registerView(this); } @@ -142,7 +142,7 @@ void TrackingView::draw(sf::RenderWindow *window) marker->draw(window); } -void TrackingView::addTrackable(ITrackable *trackable) +void TrackingView::addTrackable(const std::shared_ptr& trackable) { trackables.push_back(trackable); } @@ -150,7 +150,7 @@ void TrackingView::addTrackable(ITrackable *trackable) void TrackingView::processTrackableStates() { // Remove trackables that have ended tracking - std::remove_if(trackables.begin(), trackables.end(), [](ITrackable *trackable) + std::remove_if(trackables.begin(), trackables.end(), [](std::shared_ptr trackable) { return trackable->getTrackableState() == TrackableState::END_TRACKING; }); diff --git a/src/game/camera/tracking_view.h b/src/game/camera/tracking_view.h index ff3f908..b64529a 100644 --- a/src/game/camera/tracking_view.h +++ b/src/game/camera/tracking_view.h @@ -22,7 +22,7 @@ public: void lateUpdate() override; - void addTrackable(ITrackable *trackable); + void addTrackable(const std::shared_ptr& trackable); sf::Vector2f getSize() const; @@ -32,7 +32,7 @@ private: sf::View *view{}; bool hasViewChanged{}; TrackingViewOptions options; - std::vector trackables; + std::vector> trackables; CircleObject *marker; diff --git a/src/game/game.cpp b/src/game/game.cpp index 4478493..4069532 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -5,24 +5,11 @@ #include "game.h" #include "level/level_loader.hpp" #include "physics/map/map_simulation.hpp" -#include "../debug/grid_debug_layer.h" -#include "player/player_spawner.hpp" -Game::Game(std::shared_ptr window) : window(std::move(window)), - gameObjects(), - views(), - loadedLevelConfig() +Game::Game(std::shared_ptr window) : window(std::move(window)) { } -Game::~Game() -{ - for (auto &gameObject: gameObjects) - { - delete gameObject; - } -} - void Game::run() { sf::Clock clock; @@ -64,15 +51,22 @@ void Game::drawFrame() window->display(); } -void Game::addGameObject(GameObject *gameObject) +void Game::addGameObject(const std::shared_ptr& gameObject) { - gameObjects.push_back(gameObject); + gameObjectsBuffer.push_back(gameObject); } void Game::update() { + // Add new game objects + for (const auto& gameObject: gameObjectsBuffer) + { + gameObjects.push_back(gameObject); + } + gameObjectsBuffer.clear(); + // Basic Updates - for (auto &gameObject: gameObjects) + for (const auto& gameObject: gameObjects) { if (gameObject->getActive()) { @@ -81,7 +75,7 @@ void Game::update() } // Late updates - for (auto &gameObject: gameObjects) + for (const auto &gameObject: gameObjects) { if (gameObject->getActive()) { @@ -126,10 +120,6 @@ void Game::registerView(TrackingView *view) void Game::clearGameObjects() { - for (auto &gameObject: gameObjects) - { - delete gameObject; - } gameObjects.clear(); } diff --git a/src/game/game.h b/src/game/game.h index e4cb97f..6472be1 100644 --- a/src/game/game.h +++ b/src/game/game.h @@ -19,8 +19,6 @@ public: static std::shared_ptr getInstance(); explicit Game(std::shared_ptr window); - ~Game(); - void run(); void exit(); @@ -29,19 +27,20 @@ public: void setLevel(LevelConfig levelConfig); - bool isLevelLoaded() const; + [[nodiscard]] bool isLevelLoaded() const; - void addGameObject(GameObject *gameObject); + void addGameObject(const std::shared_ptr& gameObject); void registerView(TrackingView *view); std::shared_ptr window; - std::vector views; + std::vector views = {}; private: static inline std::shared_ptr singletonInstance = nullptr; - std::vector gameObjects; + std::vector> gameObjects = {}; + std::vector> gameObjectsBuffer = {}; - LevelConfig loadedLevelConfig; + LevelConfig loadedLevelConfig = {}; void drawFrame(); diff --git a/src/game/game_object.h b/src/game/game_object.h index a4f53a8..9d69baf 100644 --- a/src/game/game_object.h +++ b/src/game/game_object.h @@ -24,7 +24,7 @@ public: void addChildScreenOffset(const std::shared_ptr &child, IsometricCoordinates offset = {0, 0}); void addChildWorldOffset(const std::shared_ptr &child, WorldCoordinates offset); void addChild(const std::shared_ptr &child); - std::vector> getChildren() const { return children; } + [[nodiscard]] std::vector> getChildren() const { return children; } std::shared_ptr coordinates; diff --git a/src/game/level/level_loader.cpp b/src/game/level/level_loader.cpp index 643148c..4dc71ed 100644 --- a/src/game/level/level_loader.cpp +++ b/src/game/level/level_loader.cpp @@ -7,7 +7,7 @@ #include "../collectables/collection/collectables_collection.hpp" #include "../collectables/collectable_factory.hpp" -void LevelLoader::loadLevel(LevelConfig levelConfig) +void LevelLoader::loadLevel(const LevelConfig& levelConfig) { auto game = Game::getInstance(); game->clearGameObjects(); @@ -20,16 +20,16 @@ void LevelLoader::loadLevel(LevelConfig levelConfig) // Add basic game objects if (DEVELOPER_MODE) { - game->addGameObject(new GridDebugLayer(0, 50, 0, 50)); + game->addGameObject(std::make_shared(0, 50, 0, 50)); } - game->addGameObject(new TrackingView()); - game->addGameObject(new PlayerSpawner(levelConfig.playerSpawnPoints)); + game->addGameObject(std::make_shared()); + game->addGameObject(std::make_shared(levelConfig.playerSpawnPoints)); // Prepare collectables framework auto maxDepth = levelConfig.worldMapSize.x * 2; CollectablesCollection::getInstance()->createEmpty(maxDepth); - game->addGameObject(CollectablesCollection::getInstance().get()); + game->addGameObject(CollectablesCollection::getInstance()); // Spawn collectibles for (auto const &collectableInfo: levelConfig.collectables) diff --git a/src/game/level/level_loader.hpp b/src/game/level/level_loader.hpp index bd32a92..0dce671 100644 --- a/src/game/level/level_loader.hpp +++ b/src/game/level/level_loader.hpp @@ -8,7 +8,7 @@ class LevelLoader { public: - static void loadLevel(LevelConfig levelConfig); + static void loadLevel(const LevelConfig& levelConfig); static void loadLevel(const std::string &levelName); diff --git a/src/game/physics/map/map_player.hpp b/src/game/physics/map/map_player.hpp index 6d1ad92..a469dcc 100644 --- a/src/game/physics/map/map_player.hpp +++ b/src/game/physics/map/map_player.hpp @@ -3,17 +3,19 @@ #include + +#include #include "../../player/player.hpp" #include "../../../config.h" class MapPlayer { public: - Player *player; + std::shared_ptr player; b2Body *body; float shapeRadius = 0; - MapPlayer(Player *player, b2Body *body) : player(player), body(body) + MapPlayer(std::shared_ptr player, b2Body *body) : player(std::move(player)), body(body) { updateShape(); } diff --git a/src/game/physics/map/map_simulation.cpp b/src/game/physics/map/map_simulation.cpp index e588bac..881e24f 100644 --- a/src/game/physics/map/map_simulation.cpp +++ b/src/game/physics/map/map_simulation.cpp @@ -62,7 +62,7 @@ void MapSimulation::constructSquareObstacle(float minX, float minY, float maxX, body->CreateFixture(&fixtureDef); } -void MapSimulation::addPlayer(Player *player) +void MapSimulation::addPlayer(const std::shared_ptr& player) { b2BodyDef bodyDef; bodyDef.type = b2_dynamicBody; diff --git a/src/game/physics/map/map_simulation.hpp b/src/game/physics/map/map_simulation.hpp index e0abd6c..24e4f54 100644 --- a/src/game/physics/map/map_simulation.hpp +++ b/src/game/physics/map/map_simulation.hpp @@ -18,7 +18,7 @@ public: static std::shared_ptr getInstance(); - void addPlayer(Player *player); + void addPlayer(const std::shared_ptr& player); private: diff --git a/src/game/player/player.hpp b/src/game/player/player.hpp index f79c159..33964a1 100644 --- a/src/game/player/player.hpp +++ b/src/game/player/player.hpp @@ -13,23 +13,21 @@ public: Player(std::shared_ptr assignedInput, const std::string &skinRessourceName, GridCoordinates initCoordinates); - ~Player(); - void update() override; - sf::Vector2f getTrackablePosition() const override; + [[nodiscard]] sf::Vector2f getTrackablePosition() const override; - sf::Vector2f getTrackableSize() const override; + [[nodiscard]] sf::Vector2f getTrackableSize() const override; - TrackableState getTrackableState() const override; + [[nodiscard]] TrackableState getTrackableState() const override; float speed = DEFAULT_PLAYER_SPEED; - sf::Vector2f getIsoSize() const; + [[nodiscard]] sf::Vector2f getIsoSize() const; - int getPlayerId() const; + [[nodiscard]] int getPlayerId() const; - float getWorldRadius() const; + [[nodiscard]] float getWorldRadius() const; private: std::shared_ptr input; diff --git a/src/game/player/player_spawner.cpp b/src/game/player/player_spawner.cpp index 111c5c3..b25e6a7 100644 --- a/src/game/player/player_spawner.cpp +++ b/src/game/player/player_spawner.cpp @@ -29,7 +29,7 @@ void PlayerSpawner::spawnPlayer(const std::shared_ptr &inputIdent auto spawn = spawnPoints[nextSpawnPointIndex]; nextSpawnPointIndex = static_cast((nextSpawnPointIndex + 1) % spawnPoints.size()); - auto player = new Player(inputIdentity, PLAYER_SKIN, spawn); + auto player = std::make_shared(inputIdentity, PLAYER_SKIN, spawn); MapSimulation::getInstance()->addPlayer(player); Game::getInstance()->addGameObject(player);