instantiate gobject from prototype and another gobject

This commit is contained in:
2019-04-13 02:04:57 +07:00
parent 4e278f3c15
commit bb3d7bb3b5
9 changed files with 67 additions and 8 deletions

View File

@@ -13,15 +13,21 @@ namespace e2d
class gobject;
using gobject_iptr = intrusive_ptr<gobject>;
using const_gobject_iptr = intrusive_ptr<const gobject>;
class world_gobjects_ilist_tag;
using world_gobjects = intrusive_list<gobject, world_gobjects_ilist_tag>;
}
namespace e2d
{
class gobject final
: private noncopyable
, public ref_counter<gobject> {
, public ref_counter<gobject>
, public intrusive_list_hook<world_gobjects_ilist_tag> {
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_;

View File

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

View File

@@ -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_;
};
}

View File

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

View File

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

View File

@@ -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 \

View File

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

View File

@@ -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 {

View File

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