#include "fmt/format.h"
#include "game/game.h"
#include "nodes/node.h"

Node::Node() {
    // Initialization things here
    name = fmt::format("{}-{}", __func__, Util::generateUuid());
    scene = Game::scene;
}

Node::~Node() = default;

void Node::update() {
    if (parent != nullptr) {
        // If a parent exists for a Node, have its position follow its parent
        worldPosition = parent->worldPosition + localPosition;
    }

    updateChildren();
}

void Node::pollInputs() {
    pollChildInputs();
}

void Node::updateChildren() {
    for (Node *node: children) {
        node->update();
    }
}

void Node::pollChildInputs() {
    for (Node *node: children) {
        node->pollInputs();
    }
}

void Node::addChild(Node *child) {
    child->parent = this;
    children.push_back(child);
}

void Node::free() {
    for (Node *child: children) {
        child->free();
    }
    parent = nullptr;  // remove reference to parent
    scene = nullptr;   // remove reference to scene
}