#include "containernode.h"
#include <iostream>

ContainerNode::~ContainerNode()
{
    for(SceneNode *child : m_children)
        delete child;
}

// Container Node
void ContainerNode::update()
{
    for(SceneNode* sn : m_children)
        sn->update();
}

void ContainerNode::addChild(SceneNode* node)
{
    if(node != nullptr)
    {
        node->setSceneTree(m_scene);
        m_children.push_back(node);
        node->m_parent = this;
    }
}

void ContainerNode::removeChild(SceneNode* node)
{
    if(node != nullptr)
    {
        for(auto it = m_children.begin(); it != m_children.end();)
        {
            if(*it == node)
            {
                it = m_children.erase(it);
                node->m_parent = nullptr;
                node->setSceneTree(nullptr);
            }
            else
                ++it;
        }
    }
}

void ContainerNode::setSceneTree(SceneTree *tree)
{
    SceneNode::setSceneTree(tree);
    for(auto child : m_children)
        child->setSceneTree(tree);
}
