From f76461666e3f164eb1dcacc729e61ba27aaa6a91 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Mon, 13 May 2019 21:31:35 +0700 Subject: [PATCH] node contains gobject instead entity now --- headers/enduro2d/high/node.hpp | 20 +++++----- headers/enduro2d/high/world.hpp | 2 +- samples/sources/sample_03/sample_03.cpp | 38 +++++++++---------- sources/enduro2d/high/node.cpp | 31 +++++++-------- .../render_system_drawer.cpp | 5 ++- sources/enduro2d/high/world.cpp | 17 ++++++++- 6 files changed, 63 insertions(+), 50 deletions(-) diff --git a/headers/enduro2d/high/node.hpp b/headers/enduro2d/high/node.hpp index 401a8ff6..3ae4a6b1 100644 --- a/headers/enduro2d/high/node.hpp +++ b/headers/enduro2d/high/node.hpp @@ -10,6 +10,8 @@ #include "_high.hpp" +#include "gobject.hpp" + namespace e2d { class node; @@ -29,14 +31,14 @@ namespace e2d public: virtual ~node() noexcept; - static node_iptr create(world& world); - static node_iptr create(world& world, const node_iptr& parent); + static node_iptr create(); + static node_iptr create(const node_iptr& parent); - static node_iptr create(const ecs::entity& entity); - static node_iptr create(const ecs::entity& entity, const node_iptr& parent); + static node_iptr create(const gobject_iptr& owner); + static node_iptr create(const gobject_iptr& owner, const node_iptr& parent); - ecs::entity entity() noexcept; - ecs::const_entity entity() const noexcept; + gobject_iptr owner() noexcept; + const_gobject_iptr owner() const noexcept; void transform(const t3f& transform) noexcept; const t3f& transform() const noexcept; @@ -129,8 +131,8 @@ namespace e2d template < typename Iter > std::size_t extract_all_nodes(Iter iter) const; protected: - node(world& world); - node(const ecs::entity& entity); + node() = default; + node(const gobject_iptr& owner); private: enum flag_masks : u32 { fm_dirty_local_matrix = 1u << 0, @@ -142,7 +144,7 @@ namespace e2d void update_world_matrix_() const noexcept; private: t3f transform_; - ecs::entity entity_; + gobject_iptr owner_; node* parent_{nullptr}; node_children children_; private: diff --git a/headers/enduro2d/high/world.hpp b/headers/enduro2d/high/world.hpp index c487a094..f7cea40b 100644 --- a/headers/enduro2d/high/world.hpp +++ b/headers/enduro2d/high/world.hpp @@ -37,7 +37,7 @@ namespace e2d gobject_iptr instantiate(); gobject_iptr instantiate(const prefab& prefab); - void destroy_instance(const gobject_iptr& inst) noexcept; + void destroy_instance(const gobject_iptr& inst, bool recursive) noexcept; private: ecs::registry registry_; hash_map gobjects_; diff --git a/samples/sources/sample_03/sample_03.cpp b/samples/sources/sample_03/sample_03.cpp index 4a307b8e..2d4cc320 100644 --- a/samples/sources/sample_03/sample_03.cpp +++ b/samples/sources/sample_03/sample_03.cpp @@ -82,50 +82,50 @@ namespace return false; } - ecs::entity scene_e = the().registry().create_entity(); + auto scene_i = the().instantiate(); - ecs::entity_filler(scene_e) + scene_i->entity_filler() .component() - .component(node::create(scene_e)); + .component(node::create(scene_i)); - node_iptr scene_r = scene_e.get_component().node(); + node_iptr scene_r = scene_i->get_component().get().node(); { - ecs::entity model_e = the().registry().create_entity(); + auto model_i = the().instantiate(); - ecs::entity_filler(model_e) + model_i->entity_filler() .component(rotator{v3f::unit_y()}) - .component(node::create(model_e, scene_r)) + .component(node::create(model_i, scene_r)) .component(renderer() .materials({model_mat})) .component(model_res); - node_iptr model_n = model_e.get_component().node(); + node_iptr model_n = model_i->get_component().get().node(); model_n->scale(v3f{20.f}); model_n->translation(v3f{0.f,50.f,0.f}); } { - ecs::entity sprite_e = the().registry().create_entity(); + auto sprite_i = the().instantiate(); - ecs::entity_filler(sprite_e) + sprite_i->entity_filler() .component(rotator{v3f::unit_z()}) - .component(node::create(sprite_e, scene_r)) + .component(node::create(sprite_i, scene_r)) .component(renderer() .materials({sprite_mat})) .component(sprite_res); - node_iptr sprite_n = sprite_e.get_component().node(); + node_iptr sprite_n = sprite_i->get_component().get().node(); sprite_n->translation(v3f{0,-50.f,0}); } { for ( std::size_t i = 0; i < 2; ++i ) for ( std::size_t j = 0; j < 5; ++j ) { - ecs::entity flipbook_e = the().registry().create_entity(); + auto flipbook_i = the().instantiate(); - ecs::entity_filler(flipbook_e) - .component(node::create(flipbook_e, scene_r)) + flipbook_i->entity_filler() + .component(node::create(flipbook_i, scene_r)) .component(renderer() .materials({sprite_mat})) .component(sprite_renderer() @@ -135,7 +135,7 @@ namespace .play("idle") .looped(true)); - node_iptr flipbook_n = flipbook_e.get_component().node(); + node_iptr flipbook_n = flipbook_i->get_component().get().node(); flipbook_n->scale(v3f(2.f,2.f,1.f)); flipbook_n->translation(v3f{-80.f + j * 40.f, -200.f + i * 40.f, 0}); } @@ -145,11 +145,11 @@ namespace } bool create_camera() { - ecs::entity camera_e = the().registry().create_entity(); - ecs::entity_filler(camera_e) + auto camera_i = the().instantiate(); + camera_i->entity_filler() .component(camera() .background({1.f, 0.4f, 0.f, 1.f})) - .component(node::create(camera_e)); + .component(node::create(camera_i)); return true; } diff --git a/sources/enduro2d/high/node.cpp b/sources/enduro2d/high/node.cpp index b773eaf7..d14a1d78 100644 --- a/sources/enduro2d/high/node.cpp +++ b/sources/enduro2d/high/node.cpp @@ -9,47 +9,44 @@ namespace e2d { - node::node(world& world) - : entity_(world.registry().create_entity()) {} - - node::node(const ecs::entity& entity) - : entity_(entity) {} + node::node(const gobject_iptr& owner) + : owner_(owner) {} node::~node() noexcept { E2D_ASSERT(!parent_); remove_all_children(); } - node_iptr node::create(world& world) { - return node_iptr(new node(world)); + node_iptr node::create() { + return node_iptr(new node()); } - node_iptr node::create(world& world, const node_iptr& parent) { - node_iptr child = create(world); + node_iptr node::create(const node_iptr& parent) { + node_iptr child = create(); if ( parent ) { parent->add_child(child); } return child; } - node_iptr node::create(const ecs::entity& entity) { - return node_iptr(new node(entity)); + node_iptr node::create(const gobject_iptr& owner) { + return node_iptr(new node(owner)); } - node_iptr node::create(const ecs::entity& entity, const node_iptr& parent) { - node_iptr child = create(entity); + node_iptr node::create(const gobject_iptr& owner, const node_iptr& parent) { + node_iptr child = create(owner); if ( parent ) { parent->add_child(child); } return child; } - ecs::entity node::entity() noexcept { - return entity_; + gobject_iptr node::owner() noexcept { + return owner_; } - ecs::const_entity node::entity() const noexcept { - return entity_; + const_gobject_iptr node::owner() const noexcept { + return owner_; } void node::transform(const t3f& transform) noexcept { diff --git a/sources/enduro2d/high/systems/render_system_impl/render_system_drawer.cpp b/sources/enduro2d/high/systems/render_system_impl/render_system_drawer.cpp index 68453418..0cdc72b7 100644 --- a/sources/enduro2d/high/systems/render_system_impl/render_system_drawer.cpp +++ b/sources/enduro2d/high/systems/render_system_impl/render_system_drawer.cpp @@ -59,11 +59,12 @@ namespace e2d { namespace render_system_impl void drawer::context::draw( const const_node_iptr& node) { - if ( !node || !node->entity().valid() ) { + if ( !node || !node->owner() ) { return; } - ecs::const_entity node_e = node->entity(); + E2D_ASSERT(node->owner()->entity().valid()); + ecs::const_entity node_e = node->owner()->entity(); const renderer* node_r = node_e.find_component(); if ( node_r && node_r->enabled() ) { diff --git a/sources/enduro2d/high/world.cpp b/sources/enduro2d/high/world.cpp index 32cc2571..48f6d8b3 100644 --- a/sources/enduro2d/high/world.cpp +++ b/sources/enduro2d/high/world.cpp @@ -6,11 +6,14 @@ #include +#include +#include + namespace e2d { world::~world() noexcept { while ( !gobjects_.empty() ) { - destroy_instance(gobjects_.begin()->second); + destroy_instance(gobjects_.begin()->second, true); } } @@ -34,7 +37,17 @@ namespace e2d return instance; } - void world::destroy_instance(const gobject_iptr& inst) noexcept { + void world::destroy_instance(const gobject_iptr& inst, bool recursive) noexcept { + if ( recursive ) { + node_iptr inst_n = inst && inst->get_component().exists() + ? inst->get_component().get().node() + : nullptr; + if ( inst_n ) { + inst_n->for_each_child([this](const node_iptr& child_n){ + destroy_instance(child_n->owner(), true); + }); + } + } if ( inst ) { inst->entity().remove_all_components(); gobjects_.erase(inst->entity().id());