Implemented parent/child structure for game objects

This commit is contained in:
Maximilian Giller 2023-05-30 02:13:10 +02:00
parent 36f6a5e22d
commit fd723a9689
6 changed files with 35 additions and 22 deletions

View file

@ -22,25 +22,15 @@ GridDebugLayer::GridDebugLayer(int minX, int maxX, int minY, int maxY)
color = sf::Color::Blue; color = sf::Color::Blue;
} }
auto *gameObject = new CircleObject(DB_ISOPLANE_CORNER_RADIUS, color); auto gameObject = std::make_shared<CircleObject>(DB_ISOPLANE_CORNER_RADIUS, color);
gameObject->coordinates->setParent(coordinates); addChild(gameObject, WorldCoordinates(x, y));
gameObject->coordinates->set(WorldCoordinates(x, y, 0));
marker.push_back(gameObject);
} }
} }
} }
GridDebugLayer::~GridDebugLayer()
{
for (auto &gameObject: marker)
{
delete gameObject;
}
}
void GridDebugLayer::draw(sf::RenderWindow *window) const void GridDebugLayer::draw(sf::RenderWindow *window) const
{ {
for (auto &gameObject: marker) for (auto &gameObject: getChildren())
{ {
gameObject->draw(window); gameObject->draw(window);
} }

View file

@ -8,12 +8,8 @@ class GridDebugLayer : public GameObject
{ {
public: public:
GridDebugLayer(int minX, int maxX, int minY, int maxY); GridDebugLayer(int minX, int maxX, int minY, int maxY);
~GridDebugLayer();
void draw(sf::RenderWindow *window) const override; void draw(sf::RenderWindow *window) const override;
private:
std::vector<GameObject*> marker;
}; };

View file

@ -50,7 +50,7 @@ void Game::drawFrame()
for (auto &gameObject: gameObjects) for (auto &gameObject: gameObjects)
{ {
if (gameObject->isActive) if (gameObject->getActive())
{ {
gameObject->draw(window.get()); gameObject->draw(window.get());
} }
@ -69,7 +69,7 @@ void Game::update()
// Basic Updates // Basic Updates
for (auto &gameObject: gameObjects) for (auto &gameObject: gameObjects)
{ {
if (gameObject->isActive) if (gameObject->getActive())
{ {
gameObject->update(); gameObject->update();
} }
@ -78,7 +78,7 @@ void Game::update()
// Late updates // Late updates
for (auto &gameObject: gameObjects) for (auto &gameObject: gameObjects)
{ {
if (gameObject->isActive) if (gameObject->getActive())
{ {
gameObject->lateUpdate(); gameObject->lateUpdate();
} }

View file

@ -3,4 +3,22 @@
GameObject::GameObject() GameObject::GameObject()
{ {
coordinates = std::make_shared<TranslatedCoordinates>(WorldCoordinates(0, 0)); coordinates = std::make_shared<TranslatedCoordinates>(WorldCoordinates(0, 0));
children = std::vector<std::shared_ptr<GameObject>>();
}
void GameObject::addChild(const std::shared_ptr<GameObject>& 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);
}
} }

View file

@ -21,7 +21,16 @@ public:
virtual void lateUpdate() virtual void lateUpdate()
{} {}
void setActive(bool active);
bool getActive() const { return isActive; }
void addChild(const std::shared_ptr<GameObject> &child, WorldCoordinates offset = {0, 0});
std::vector<std::shared_ptr<GameObject>> getChildren() const { return children; }
std::shared_ptr<TranslatedCoordinates> coordinates; std::shared_ptr<TranslatedCoordinates> coordinates;
private:
std::vector<std::shared_ptr<GameObject>> children;
bool isActive = true; bool isActive = true;
}; };

View file

@ -17,7 +17,7 @@ void Player::update()
{ {
if (!input->isActive) if (!input->isActive)
{ {
isActive = false; setActive(false);
return; return;
} }
@ -48,7 +48,7 @@ Player::Player(std::shared_ptr<InputIdentity> assignedInput, const sf::Color col
TrackableState Player::getTrackableState() const TrackableState Player::getTrackableState() const
{ {
if (isActive) if (getActive())
{ {
return TrackableState::TRACKING; return TrackableState::TRACKING;
} else } else