From fd723a968975c96223d73faca01908be9e50d8fe Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Tue, 30 May 2023 02:13:10 +0200 Subject: [PATCH] Implemented parent/child structure for game objects --- src/debug/grid_debug_layer.cpp | 16 +++------------- src/debug/grid_debug_layer.h | 4 ---- src/game/game.cpp | 6 +++--- src/game/game_object.cpp | 18 ++++++++++++++++++ src/game/game_object.h | 9 +++++++++ src/game/player/player.cpp | 4 ++-- 6 files changed, 35 insertions(+), 22 deletions(-) diff --git a/src/debug/grid_debug_layer.cpp b/src/debug/grid_debug_layer.cpp index b1649fe..1722188 100644 --- a/src/debug/grid_debug_layer.cpp +++ b/src/debug/grid_debug_layer.cpp @@ -22,25 +22,15 @@ GridDebugLayer::GridDebugLayer(int minX, int maxX, int minY, int maxY) color = sf::Color::Blue; } - auto *gameObject = new CircleObject(DB_ISOPLANE_CORNER_RADIUS, color); - gameObject->coordinates->setParent(coordinates); - gameObject->coordinates->set(WorldCoordinates(x, y, 0)); - marker.push_back(gameObject); + auto gameObject = std::make_shared(DB_ISOPLANE_CORNER_RADIUS, color); + addChild(gameObject, WorldCoordinates(x, y)); } } } -GridDebugLayer::~GridDebugLayer() -{ - for (auto &gameObject: marker) - { - delete gameObject; - } -} - void GridDebugLayer::draw(sf::RenderWindow *window) const { - for (auto &gameObject: marker) + for (auto &gameObject: getChildren()) { gameObject->draw(window); } diff --git a/src/debug/grid_debug_layer.h b/src/debug/grid_debug_layer.h index b452f2e..320c6da 100644 --- a/src/debug/grid_debug_layer.h +++ b/src/debug/grid_debug_layer.h @@ -8,12 +8,8 @@ class GridDebugLayer : public GameObject { public: GridDebugLayer(int minX, int maxX, int minY, int maxY); - ~GridDebugLayer(); void draw(sf::RenderWindow *window) const override; - -private: - std::vector marker; }; diff --git a/src/game/game.cpp b/src/game/game.cpp index d151fe4..f98db94 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -50,7 +50,7 @@ void Game::drawFrame() for (auto &gameObject: gameObjects) { - if (gameObject->isActive) + if (gameObject->getActive()) { gameObject->draw(window.get()); } @@ -69,7 +69,7 @@ void Game::update() // Basic Updates for (auto &gameObject: gameObjects) { - if (gameObject->isActive) + if (gameObject->getActive()) { gameObject->update(); } @@ -78,7 +78,7 @@ void Game::update() // Late updates for (auto &gameObject: gameObjects) { - if (gameObject->isActive) + if (gameObject->getActive()) { gameObject->lateUpdate(); } diff --git a/src/game/game_object.cpp b/src/game/game_object.cpp index 9afff28..d27258f 100644 --- a/src/game/game_object.cpp +++ b/src/game/game_object.cpp @@ -3,4 +3,22 @@ GameObject::GameObject() { coordinates = std::make_shared(WorldCoordinates(0, 0)); + children = std::vector>(); +} + +void GameObject::addChild(const std::shared_ptr& child, WorldCoordinates offset) +{ + children.push_back(child); + child->coordinates->setParent(coordinates, offset); +} + +void GameObject::setActive(bool active) +{ + // Careful: potential infinite loop! + isActive = active; + + for (auto &child: children) + { + child->setActive(active); + } } diff --git a/src/game/game_object.h b/src/game/game_object.h index 2321fd4..1714b30 100644 --- a/src/game/game_object.h +++ b/src/game/game_object.h @@ -21,7 +21,16 @@ public: virtual void lateUpdate() {} + void setActive(bool active); + bool getActive() const { return isActive; } + + void addChild(const std::shared_ptr &child, WorldCoordinates offset = {0, 0}); + std::vector> getChildren() const { return children; } + std::shared_ptr coordinates; + +private: + std::vector> children; bool isActive = true; }; diff --git a/src/game/player/player.cpp b/src/game/player/player.cpp index 85f5873..c750517 100644 --- a/src/game/player/player.cpp +++ b/src/game/player/player.cpp @@ -17,7 +17,7 @@ void Player::update() { if (!input->isActive) { - isActive = false; + setActive(false); return; } @@ -48,7 +48,7 @@ Player::Player(std::shared_ptr assignedInput, const sf::Color col TrackableState Player::getTrackableState() const { - if (isActive) + if (getActive()) { return TrackableState::TRACKING; } else