diff --git a/src/core/level.cpp b/src/core/level.cpp index 1b1526f..fde5b59 100644 --- a/src/core/level.cpp +++ b/src/core/level.cpp @@ -5,6 +5,12 @@ #include namespace ce { +void Level::instantiate() { + std::unique_ptr constructed{this->construct()}; + constructed->set_is_inside_tree(true); + this->root = std::move(constructed); + this->root->propagate_added(); +} void Level::deinstantiate() { this->root.reset(); } diff --git a/src/core/level.hpp b/src/core/level.hpp index d7fd0d3..0410aa3 100644 --- a/src/core/level.hpp +++ b/src/core/level.hpp @@ -14,7 +14,8 @@ protected: }; public: virtual ~Level() = default; - virtual void instantiate() = 0; + void instantiate(); + virtual Node::OwnedPtr construct() = 0; void deinstantiate(); void propagate_tick(double const &delta_time); void propagate_draw(SDL_Renderer *render); diff --git a/src/core/node.cpp b/src/core/node.cpp index cef6de2..2e9d965 100644 --- a/src/core/node.cpp +++ b/src/core/node.cpp @@ -17,8 +17,8 @@ void Node::add_child(Node::OwnedPtr &child) { Node *added{child.get()}; this->children.push_back({child->get_name(), std::move(child)}); added->parent = this; - this->child_added.invoke(added); added->set_is_inside_tree(this->inside_tree); + this->child_added.invoke(added); added->propagate_added(); } // specialize to skip dynamic_cast @@ -44,7 +44,9 @@ std::string const &Node::get_name() const { } void Node::set_name(std::string const &name) { - if(this->parent != nullptr) + if(this->parent == nullptr) + this->name = name; + else this->parent->rename_child(this->get_name(), name); } @@ -86,16 +88,14 @@ void Node::set_level(ce::Level *level) { // parent level needs to match assert(this->parent == nullptr || this->parent->get_level() == level); this->level = level; - for(ChildrenVector::value_type &pair : this->children) { + for(ChildrenVector::value_type &pair : this->children) pair.second->set_level(level); - } } void Node::set_is_inside_tree(bool value) { this->inside_tree = value; - for(ChildrenVector::value_type &pair : this->children) { + for(ChildrenVector::value_type &pair : this->children) pair.second->set_is_inside_tree(value); - } } std::optional Node::remove_child(Node *node) {