feat: formatting and invoking of child_added reordered

main
Sara 2025-01-09 21:55:00 +01:00
parent 36637b1f8a
commit d4a436da2f
3 changed files with 14 additions and 7 deletions

View File

@ -5,6 +5,12 @@
#include <cmath>
namespace ce {
void Level::instantiate() {
std::unique_ptr<Node> constructed{this->construct()};
constructed->set_is_inside_tree(true);
this->root = std::move(constructed);
this->root->propagate_added();
}
void Level::deinstantiate() {
this->root.reset();
}

View File

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

View File

@ -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,17 +88,15 @@ 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::OwnedPtr> Node::remove_child(Node *node) {
if(this != node->get_parent())