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),
|
TrackingView::TrackingView(TrackingViewOptions options) : options(options),
|
||||||
view(nullptr),
|
view(nullptr),
|
||||||
hasViewChanged(false)
|
hasViewChanged(false),
|
||||||
{
|
trackables({})
|
||||||
trackables = std::vector<ITrackable *>();
|
{;
|
||||||
marker = new CircleObject(DB_CIRCLE_RADIUS, sf::Color::Yellow);
|
marker = new CircleObject(DB_CIRCLE_RADIUS, sf::Color::Yellow);
|
||||||
Game::getInstance()->registerView(this);
|
Game::getInstance()->registerView(this);
|
||||||
}
|
}
|
||||||
|
@ -142,7 +142,7 @@ void TrackingView::draw(sf::RenderWindow *window)
|
||||||
marker->draw(window);
|
marker->draw(window);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TrackingView::addTrackable(ITrackable *trackable)
|
void TrackingView::addTrackable(const std::shared_ptr<ITrackable>& trackable)
|
||||||
{
|
{
|
||||||
trackables.push_back(trackable);
|
trackables.push_back(trackable);
|
||||||
}
|
}
|
||||||
|
@ -150,7 +150,7 @@ void TrackingView::addTrackable(ITrackable *trackable)
|
||||||
void TrackingView::processTrackableStates()
|
void TrackingView::processTrackableStates()
|
||||||
{
|
{
|
||||||
// Remove trackables that have ended tracking
|
// 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;
|
return trackable->getTrackableState() == TrackableState::END_TRACKING;
|
||||||
});
|
});
|
||||||
|
|
|
@ -22,7 +22,7 @@ public:
|
||||||
|
|
||||||
void lateUpdate() override;
|
void lateUpdate() override;
|
||||||
|
|
||||||
void addTrackable(ITrackable *trackable);
|
void addTrackable(const std::shared_ptr<ITrackable>& trackable);
|
||||||
|
|
||||||
sf::Vector2f getSize() const;
|
sf::Vector2f getSize() const;
|
||||||
|
|
||||||
|
@ -32,7 +32,7 @@ private:
|
||||||
sf::View *view{};
|
sf::View *view{};
|
||||||
bool hasViewChanged{};
|
bool hasViewChanged{};
|
||||||
TrackingViewOptions options;
|
TrackingViewOptions options;
|
||||||
std::vector<ITrackable *> trackables;
|
std::vector<std::shared_ptr<ITrackable>> trackables;
|
||||||
|
|
||||||
CircleObject *marker;
|
CircleObject *marker;
|
||||||
|
|
||||||
|
|
|
@ -5,24 +5,11 @@
|
||||||
#include "game.h"
|
#include "game.h"
|
||||||
#include "level/level_loader.hpp"
|
#include "level/level_loader.hpp"
|
||||||
#include "physics/map/map_simulation.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)),
|
Game::Game(std::shared_ptr<sf::RenderWindow> window) : window(std::move(window))
|
||||||
gameObjects(),
|
|
||||||
views(),
|
|
||||||
loadedLevelConfig()
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
Game::~Game()
|
|
||||||
{
|
|
||||||
for (auto &gameObject: gameObjects)
|
|
||||||
{
|
|
||||||
delete gameObject;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Game::run()
|
void Game::run()
|
||||||
{
|
{
|
||||||
sf::Clock clock;
|
sf::Clock clock;
|
||||||
|
@ -64,15 +51,22 @@ void Game::drawFrame()
|
||||||
window->display();
|
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()
|
void Game::update()
|
||||||
{
|
{
|
||||||
|
// Add new game objects
|
||||||
|
for (const auto& gameObject: gameObjectsBuffer)
|
||||||
|
{
|
||||||
|
gameObjects.push_back(gameObject);
|
||||||
|
}
|
||||||
|
gameObjectsBuffer.clear();
|
||||||
|
|
||||||
// Basic Updates
|
// Basic Updates
|
||||||
for (auto &gameObject: gameObjects)
|
for (const auto& gameObject: gameObjects)
|
||||||
{
|
{
|
||||||
if (gameObject->getActive())
|
if (gameObject->getActive())
|
||||||
{
|
{
|
||||||
|
@ -81,7 +75,7 @@ void Game::update()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Late updates
|
// Late updates
|
||||||
for (auto &gameObject: gameObjects)
|
for (const auto &gameObject: gameObjects)
|
||||||
{
|
{
|
||||||
if (gameObject->getActive())
|
if (gameObject->getActive())
|
||||||
{
|
{
|
||||||
|
@ -126,10 +120,6 @@ void Game::registerView(TrackingView *view)
|
||||||
|
|
||||||
void Game::clearGameObjects()
|
void Game::clearGameObjects()
|
||||||
{
|
{
|
||||||
for (auto &gameObject: gameObjects)
|
|
||||||
{
|
|
||||||
delete gameObject;
|
|
||||||
}
|
|
||||||
gameObjects.clear();
|
gameObjects.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,8 +19,6 @@ public:
|
||||||
static std::shared_ptr<Game> getInstance();
|
static std::shared_ptr<Game> getInstance();
|
||||||
explicit Game(std::shared_ptr<sf::RenderWindow> window);
|
explicit Game(std::shared_ptr<sf::RenderWindow> window);
|
||||||
|
|
||||||
~Game();
|
|
||||||
|
|
||||||
void run();
|
void run();
|
||||||
|
|
||||||
void exit();
|
void exit();
|
||||||
|
@ -29,19 +27,20 @@ public:
|
||||||
|
|
||||||
void setLevel(LevelConfig levelConfig);
|
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);
|
void registerView(TrackingView *view);
|
||||||
|
|
||||||
std::shared_ptr<sf::RenderWindow> window;
|
std::shared_ptr<sf::RenderWindow> window;
|
||||||
std::vector<TrackingView*> views;
|
std::vector<TrackingView*> views = {};
|
||||||
private:
|
private:
|
||||||
static inline std::shared_ptr<Game> singletonInstance = nullptr;
|
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();
|
void drawFrame();
|
||||||
|
|
||||||
|
|
|
@ -24,7 +24,7 @@ public:
|
||||||
void addChildScreenOffset(const std::shared_ptr<GameObject> &child, IsometricCoordinates offset = {0, 0});
|
void addChildScreenOffset(const std::shared_ptr<GameObject> &child, IsometricCoordinates offset = {0, 0});
|
||||||
void addChildWorldOffset(const std::shared_ptr<GameObject> &child, WorldCoordinates offset);
|
void addChildWorldOffset(const std::shared_ptr<GameObject> &child, WorldCoordinates offset);
|
||||||
void addChild(const std::shared_ptr<GameObject> &child);
|
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;
|
std::shared_ptr<TranslatedCoordinates> coordinates;
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
#include "../collectables/collection/collectables_collection.hpp"
|
#include "../collectables/collection/collectables_collection.hpp"
|
||||||
#include "../collectables/collectable_factory.hpp"
|
#include "../collectables/collectable_factory.hpp"
|
||||||
|
|
||||||
void LevelLoader::loadLevel(LevelConfig levelConfig)
|
void LevelLoader::loadLevel(const LevelConfig& levelConfig)
|
||||||
{
|
{
|
||||||
auto game = Game::getInstance();
|
auto game = Game::getInstance();
|
||||||
game->clearGameObjects();
|
game->clearGameObjects();
|
||||||
|
@ -20,16 +20,16 @@ void LevelLoader::loadLevel(LevelConfig levelConfig)
|
||||||
// Add basic game objects
|
// Add basic game objects
|
||||||
if (DEVELOPER_MODE)
|
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(std::make_shared<TrackingView>());
|
||||||
game->addGameObject(new PlayerSpawner(levelConfig.playerSpawnPoints));
|
game->addGameObject(std::make_shared<PlayerSpawner>(levelConfig.playerSpawnPoints));
|
||||||
|
|
||||||
// Prepare collectables framework
|
// Prepare collectables framework
|
||||||
auto maxDepth = levelConfig.worldMapSize.x * 2;
|
auto maxDepth = levelConfig.worldMapSize.x * 2;
|
||||||
CollectablesCollection::getInstance()->createEmpty(maxDepth);
|
CollectablesCollection::getInstance()->createEmpty(maxDepth);
|
||||||
game->addGameObject(CollectablesCollection::getInstance().get());
|
game->addGameObject(CollectablesCollection::getInstance());
|
||||||
|
|
||||||
// Spawn collectibles
|
// Spawn collectibles
|
||||||
for (auto const &collectableInfo: levelConfig.collectables)
|
for (auto const &collectableInfo: levelConfig.collectables)
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
class LevelLoader
|
class LevelLoader
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static void loadLevel(LevelConfig levelConfig);
|
static void loadLevel(const LevelConfig& levelConfig);
|
||||||
|
|
||||||
static void loadLevel(const std::string &levelName);
|
static void loadLevel(const std::string &levelName);
|
||||||
|
|
||||||
|
|
|
@ -3,17 +3,19 @@
|
||||||
|
|
||||||
|
|
||||||
#include <box2d/box2d.h>
|
#include <box2d/box2d.h>
|
||||||
|
|
||||||
|
#include <utility>
|
||||||
#include "../../player/player.hpp"
|
#include "../../player/player.hpp"
|
||||||
#include "../../../config.h"
|
#include "../../../config.h"
|
||||||
|
|
||||||
class MapPlayer
|
class MapPlayer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Player *player;
|
std::shared_ptr<Player> player;
|
||||||
b2Body *body;
|
b2Body *body;
|
||||||
float shapeRadius = 0;
|
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();
|
updateShape();
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,7 +62,7 @@ void MapSimulation::constructSquareObstacle(float minX, float minY, float maxX,
|
||||||
body->CreateFixture(&fixtureDef);
|
body->CreateFixture(&fixtureDef);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MapSimulation::addPlayer(Player *player)
|
void MapSimulation::addPlayer(const std::shared_ptr<Player>& player)
|
||||||
{
|
{
|
||||||
b2BodyDef bodyDef;
|
b2BodyDef bodyDef;
|
||||||
bodyDef.type = b2_dynamicBody;
|
bodyDef.type = b2_dynamicBody;
|
||||||
|
|
|
@ -18,7 +18,7 @@ public:
|
||||||
|
|
||||||
static std::shared_ptr<MapSimulation> getInstance();
|
static std::shared_ptr<MapSimulation> getInstance();
|
||||||
|
|
||||||
void addPlayer(Player *player);
|
void addPlayer(const std::shared_ptr<Player>& player);
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -13,23 +13,21 @@ public:
|
||||||
Player(std::shared_ptr<InputIdentity> assignedInput, const std::string &skinRessourceName,
|
Player(std::shared_ptr<InputIdentity> assignedInput, const std::string &skinRessourceName,
|
||||||
GridCoordinates initCoordinates);
|
GridCoordinates initCoordinates);
|
||||||
|
|
||||||
~Player();
|
|
||||||
|
|
||||||
void update() override;
|
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;
|
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:
|
private:
|
||||||
std::shared_ptr<InputIdentity> input;
|
std::shared_ptr<InputIdentity> input;
|
||||||
|
|
|
@ -29,7 +29,7 @@ void PlayerSpawner::spawnPlayer(const std::shared_ptr<InputIdentity> &inputIdent
|
||||||
auto spawn = spawnPoints[nextSpawnPointIndex];
|
auto spawn = spawnPoints[nextSpawnPointIndex];
|
||||||
nextSpawnPointIndex = static_cast<int>((nextSpawnPointIndex + 1) % spawnPoints.size());
|
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);
|
MapSimulation::getInstance()->addPlayer(player);
|
||||||
Game::getInstance()->addGameObject(player);
|
Game::getInstance()->addGameObject(player);
|
||||||
|
|
Loading…
Reference in a new issue