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;
}
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<CircleObject>(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);
}

View file

@ -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<GameObject*> marker;
};

View file

@ -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();
}

View file

@ -3,4 +3,22 @@
GameObject::GameObject()
{
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()
{}
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;
private:
std::vector<std::shared_ptr<GameObject>> children;
bool isActive = true;
};

View file

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