spacebox/src/Node.cpp

85 lines
1.3 KiB
C++
Raw Normal View History

2019-04-23 01:42:19 -04:00
#include "Node.hpp"
2019-05-02 20:11:45 -04:00
#include "Game.hpp"
2019-04-23 01:42:19 -04:00
2019-05-02 20:11:45 -04:00
Node::Node() : Node(NULL) {}
2019-05-02 06:45:41 -04:00
2019-06-16 03:05:02 -04:00
Node::Node(Node *parent, bool active) : parent(parent)
2019-05-02 06:45:41 -04:00
{
2019-06-16 03:05:02 -04:00
if (active)
{
activate();
}
2019-05-02 20:11:45 -04:00
std::cout << "Constructing ";
print_branch();
2019-05-02 06:45:41 -04:00
}
void Node::set_parent(Node* other)
{
parent = other;
}
2019-06-16 03:05:02 -04:00
void Node::activate()
{
active = true;
}
void Node::deactivate()
{
active = false;
}
bool Node::is_active() const
{
return active;
}
2019-05-04 03:25:35 -04:00
nlohmann::json& Node::get_configuration()
2019-05-02 06:45:41 -04:00
{
return get_root()->configuration.config;
2019-05-02 06:45:41 -04:00
}
2019-04-23 01:42:19 -04:00
Game* Node::get_root()
{
Node* current = this;
2019-04-23 01:42:19 -04:00
while (current->parent != NULL)
{
current = current->parent;
}
return static_cast<Game*>(current);
}
2019-05-02 20:11:45 -04:00
Delegate& Node::get_delegate()
2019-05-16 03:51:36 -04:00
{
return get_root()->delegate;
}
Display& Node::get_display()
2019-05-16 03:51:36 -04:00
{
return get_root()->display;
}
2019-05-02 20:11:45 -04:00
void Node::print_branch()
{
Node* current = this;
2019-05-02 20:11:45 -04:00
while (current != NULL)
{
std::cout << current->get_class_name() << " @ " << current;
if (current->parent != NULL)
{
std::cout << " -> ";
}
else
{
std::cout << std::endl;
}
current = current->parent;
}
}
Node::~Node()
{
std::cout << "Destructing ";
print_branch();
get_delegate().unsubscribe(this);
}