diff --git a/headers/enduro2d/high/gobject.hpp b/headers/enduro2d/high/gobject.hpp index 7c43fed9..70fe1033 100644 --- a/headers/enduro2d/high/gobject.hpp +++ b/headers/enduro2d/high/gobject.hpp @@ -13,15 +13,21 @@ namespace e2d class gobject; using gobject_iptr = intrusive_ptr; using const_gobject_iptr = intrusive_ptr; + + class world_gobjects_ilist_tag; + using world_gobjects = intrusive_list; } namespace e2d { class gobject final : private noncopyable - , public ref_counter { + , public ref_counter + , public intrusive_list_hook { public: gobject(ecs::registry& registry); + gobject(ecs::registry& registry, const gobject& source); + gobject(ecs::registry& registry, const ecs::prototype& proto); ~gobject() noexcept; private: ecs::entity entity_; diff --git a/headers/enduro2d/high/prefab.hpp b/headers/enduro2d/high/prefab.hpp index 7514c6cc..0ab9e6bd 100644 --- a/headers/enduro2d/high/prefab.hpp +++ b/headers/enduro2d/high/prefab.hpp @@ -26,6 +26,13 @@ namespace e2d prefab& assign(prefab&& other) noexcept; prefab& assign(const prefab& other); + + prefab& set_prototype(ecs::prototype&& proto) noexcept; + prefab& set_prototype(const ecs::prototype& proto); + + const ecs::prototype& prototype() const noexcept; + private: + ecs::prototype prototype_; }; void swap(prefab& l, prefab& r) noexcept; diff --git a/headers/enduro2d/high/world.hpp b/headers/enduro2d/high/world.hpp index eb4e4d22..b5541067 100644 --- a/headers/enduro2d/high/world.hpp +++ b/headers/enduro2d/high/world.hpp @@ -37,7 +37,9 @@ namespace e2d gobject_iptr instantiate(); gobject_iptr instantiate(const prefab_asset::ptr& prefab); + gobject_iptr instantiate(const const_gobject_iptr& gobject); private: ecs::registry registry_; + world_gobjects gobjects_; }; } diff --git a/scripts/cloc_all.sh b/scripts/cloc_all.sh index f6abdbd9..ab40e99e 100755 --- a/scripts/cloc_all.sh +++ b/scripts/cloc_all.sh @@ -2,6 +2,8 @@ SCRIPT_DIR=`dirname "$BASH_SOURCE"` cloc \ $SCRIPT_DIR/../headers/enduro2d \ + $SCRIPT_DIR/../headers/3rdparty/ecs.hpp \ + $SCRIPT_DIR/../headers/3rdparty/promise.hpp \ $SCRIPT_DIR/../sources/enduro2d \ $SCRIPT_DIR/../samples/sources \ $SCRIPT_DIR/../untests/sources diff --git a/scripts/cloc_engine.sh b/scripts/cloc_engine.sh index 57b570c6..91c18f7b 100755 --- a/scripts/cloc_engine.sh +++ b/scripts/cloc_engine.sh @@ -2,4 +2,6 @@ SCRIPT_DIR=`dirname "$BASH_SOURCE"` cloc \ $SCRIPT_DIR/../headers/enduro2d \ + $SCRIPT_DIR/../headers/3rdparty/ecs.hpp \ + $SCRIPT_DIR/../headers/3rdparty/promise.hpp \ $SCRIPT_DIR/../sources/enduro2d diff --git a/scripts/cloc_headers.sh b/scripts/cloc_headers.sh index beff52c2..a2e9aa29 100755 --- a/scripts/cloc_headers.sh +++ b/scripts/cloc_headers.sh @@ -1,3 +1,6 @@ #!/bin/bash SCRIPT_DIR=`dirname "$BASH_SOURCE"` -cloc $SCRIPT_DIR/../headers/enduro2d +cloc \ + $SCRIPT_DIR/../headers/enduro2d \ + $SCRIPT_DIR/../headers/3rdparty/ecs.hpp \ + $SCRIPT_DIR/../headers/3rdparty/promise.hpp \ diff --git a/sources/enduro2d/high/gobject.cpp b/sources/enduro2d/high/gobject.cpp index 0686f87c..daba75e5 100644 --- a/sources/enduro2d/high/gobject.cpp +++ b/sources/enduro2d/high/gobject.cpp @@ -11,6 +11,12 @@ namespace e2d gobject::gobject(ecs::registry& registry) : entity_(registry.create_entity()) {} + gobject::gobject(ecs::registry& registry, const gobject& source) + : entity_(registry.create_entity(source.entity_)) {} + + gobject::gobject(ecs::registry& registry, const ecs::prototype& proto) + : entity_(registry.create_entity(proto)) {} + gobject::~gobject() noexcept { entity_.destroy(); } diff --git a/sources/enduro2d/high/prefab.cpp b/sources/enduro2d/high/prefab.cpp index a25bb70d..b9a38004 100644 --- a/sources/enduro2d/high/prefab.cpp +++ b/sources/enduro2d/high/prefab.cpp @@ -28,11 +28,12 @@ namespace e2d } void prefab::clear() noexcept { + prototype_.clear(); } void prefab::swap(prefab& other) noexcept { using std::swap; - E2D_UNUSED(other); + swap(prototype_, other.prototype_); } prefab& prefab::assign(prefab&& other) noexcept { @@ -46,10 +47,25 @@ namespace e2d prefab& prefab::assign(const prefab& other) { if ( this != &other ) { prefab s; + s.prototype_ = other.prototype_; swap(s); } return *this; } + + prefab& prefab::set_prototype(ecs::prototype&& proto) noexcept { + prototype_ = std::move(proto); + return *this; + } + + prefab& prefab::set_prototype(const ecs::prototype& proto) { + prototype_ = proto; + return *this; + } + + const ecs::prototype& prefab::prototype() const noexcept { + return prototype_; + } } namespace e2d @@ -59,8 +75,7 @@ namespace e2d } bool operator==(const prefab& l, const prefab& r) noexcept { - E2D_UNUSED(l, r); - return true; + return l.prototype().empty() && r.prototype().empty(); } bool operator!=(const prefab& l, const prefab& r) noexcept { diff --git a/sources/enduro2d/high/world.cpp b/sources/enduro2d/high/world.cpp index 0fe898e2..eaae757a 100644 --- a/sources/enduro2d/high/world.cpp +++ b/sources/enduro2d/high/world.cpp @@ -20,11 +20,27 @@ namespace e2d } gobject_iptr world::instantiate() { - return gobject_iptr(new gobject(registry_)); + auto instance = gobject_iptr(new gobject(registry_)); + intrusive_ptr_add_ref(instance.get()); + gobjects_.push_back(*instance); + return instance; } gobject_iptr world::instantiate(const prefab_asset::ptr& prefab) { - E2D_UNUSED(prefab); - return instantiate(); + auto instance = prefab + ? gobject_iptr(new gobject(registry_, prefab->content().prototype())) + : gobject_iptr(new gobject(registry_)); + intrusive_ptr_add_ref(instance.get()); + gobjects_.push_back(*instance); + return instance; + } + + gobject_iptr world::instantiate(const const_gobject_iptr& source) { + auto instance = source + ? gobject_iptr(new gobject(registry_, *source)) + : gobject_iptr(new gobject(registry_)); + intrusive_ptr_add_ref(instance.get()); + gobjects_.push_back(*instance); + return instance; } }