Fixed exception thats thrown on destruction of game. Made game objects shared pointers
This commit is contained in:
parent
88f19ae5e4
commit
d3e6e35c9b
12 changed files with 45 additions and 56 deletions
|
@ -3,9 +3,9 @@
|
|||
|
||||
TrackingView::TrackingView(TrackingViewOptions options) : options(options),
|
||||
view(nullptr),
|
||||
hasViewChanged(false)
|
||||
{
|
||||
trackables = std::vector<ITrackable *>();
|
||||
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<ITrackable>& 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<ITrackable> trackable)
|
||||
{
|
||||
return trackable->getTrackableState() == TrackableState::END_TRACKING;
|
||||
});
|
||||
|
|
|
@ -22,7 +22,7 @@ public:
|
|||
|
||||
void lateUpdate() override;
|
||||
|
||||
void addTrackable(ITrackable *trackable);
|
||||
void addTrackable(const std::shared_ptr<ITrackable>& trackable);
|
||||
|
||||
sf::Vector2f getSize() const;
|
||||
|
||||
|
@ -32,7 +32,7 @@ private:
|
|||
sf::View *view{};
|
||||
bool hasViewChanged{};
|
||||
TrackingViewOptions options;
|
||||
std::vector<ITrackable *> trackables;
|
||||
std::vector<std::shared_ptr<ITrackable>> trackables;
|
||||
|
||||
CircleObject *marker;
|
||||
|
||||
|
|
|
@ -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<sf::RenderWindow> window) : window(std::move(window)),
|
||||
gameObjects(),
|
||||
views(),
|
||||
loadedLevelConfig()
|
||||
Game::Game(std::shared_ptr<sf::RenderWindow> 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>& 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();
|
||||
}
|
||||
|
||||
|
|
|
@ -19,8 +19,6 @@ public:
|
|||
static std::shared_ptr<Game> getInstance();
|
||||
explicit Game(std::shared_ptr<sf::RenderWindow> 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>& gameObject);
|
||||
|
||||
void registerView(TrackingView *view);
|
||||
|
||||
std::shared_ptr<sf::RenderWindow> window;
|
||||
std::vector<TrackingView*> views;
|
||||
std::vector<TrackingView*> views = {};
|
||||
private:
|
||||
static inline std::shared_ptr<Game> singletonInstance = nullptr;
|
||||
std::vector<GameObject *> gameObjects;
|
||||
std::vector<std::shared_ptr<GameObject>> gameObjects = {};
|
||||
std::vector<std::shared_ptr<GameObject>> gameObjectsBuffer = {};
|
||||
|
||||
LevelConfig loadedLevelConfig;
|
||||
LevelConfig loadedLevelConfig = {};
|
||||
|
||||
void drawFrame();
|
||||
|
||||
|
|
|
@ -24,7 +24,7 @@ public:
|
|||
void addChildScreenOffset(const std::shared_ptr<GameObject> &child, IsometricCoordinates offset = {0, 0});
|
||||
void addChildWorldOffset(const std::shared_ptr<GameObject> &child, WorldCoordinates offset);
|
||||
void addChild(const std::shared_ptr<GameObject> &child);
|
||||
std::vector<std::shared_ptr<GameObject>> getChildren() const { return children; }
|
||||
[[nodiscard]] std::vector<std::shared_ptr<GameObject>> getChildren() const { return children; }
|
||||
|
||||
std::shared_ptr<TranslatedCoordinates> coordinates;
|
||||
|
||||
|
|
|
@ -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<GridDebugLayer>(0, 50, 0, 50));
|
||||
}
|
||||
|
||||
game->addGameObject(new TrackingView());
|
||||
game->addGameObject(new PlayerSpawner(levelConfig.playerSpawnPoints));
|
||||
game->addGameObject(std::make_shared<TrackingView>());
|
||||
game->addGameObject(std::make_shared<PlayerSpawner>(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)
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -3,17 +3,19 @@
|
|||
|
||||
|
||||
#include <box2d/box2d.h>
|
||||
|
||||
#include <utility>
|
||||
#include "../../player/player.hpp"
|
||||
#include "../../../config.h"
|
||||
|
||||
class MapPlayer
|
||||
{
|
||||
public:
|
||||
Player *player;
|
||||
std::shared_ptr<Player> player;
|
||||
b2Body *body;
|
||||
float shapeRadius = 0;
|
||||
|
||||
MapPlayer(Player *player, b2Body *body) : player(player), body(body)
|
||||
MapPlayer(std::shared_ptr<Player> player, b2Body *body) : player(std::move(player)), body(body)
|
||||
{
|
||||
updateShape();
|
||||
}
|
||||
|
|
|
@ -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>& player)
|
||||
{
|
||||
b2BodyDef bodyDef;
|
||||
bodyDef.type = b2_dynamicBody;
|
||||
|
|
|
@ -18,7 +18,7 @@ public:
|
|||
|
||||
static std::shared_ptr<MapSimulation> getInstance();
|
||||
|
||||
void addPlayer(Player *player);
|
||||
void addPlayer(const std::shared_ptr<Player>& player);
|
||||
|
||||
|
||||
private:
|
||||
|
|
|
@ -13,23 +13,21 @@ public:
|
|||
Player(std::shared_ptr<InputIdentity> 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<InputIdentity> input;
|
||||
|
|
|
@ -29,7 +29,7 @@ void PlayerSpawner::spawnPlayer(const std::shared_ptr<InputIdentity> &inputIdent
|
|||
auto spawn = spawnPoints[nextSpawnPointIndex];
|
||||
nextSpawnPointIndex = static_cast<int>((nextSpawnPointIndex + 1) % spawnPoints.size());
|
||||
|
||||
auto player = new Player(inputIdentity, PLAYER_SKIN, spawn);
|
||||
auto player = std::make_shared<Player>(inputIdentity, PLAYER_SKIN, spawn);
|
||||
|
||||
MapSimulation::getInstance()->addPlayer(player);
|
||||
Game::getInstance()->addGameObject(player);
|
||||
|
|
Loading…
Reference in a new issue