mirror of
https://github.com/enduro2d/enduro2d.git
synced 2025-12-16 14:08:59 +07:00
world holds entity instances now
This commit is contained in:
@@ -13,23 +13,41 @@ namespace e2d
|
|||||||
class gobject;
|
class gobject;
|
||||||
using gobject_iptr = intrusive_ptr<gobject>;
|
using gobject_iptr = intrusive_ptr<gobject>;
|
||||||
using const_gobject_iptr = intrusive_ptr<const 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
|
namespace e2d
|
||||||
{
|
{
|
||||||
class gobject final
|
class gobject final
|
||||||
: private noncopyable
|
: private noncopyable
|
||||||
, public ref_counter<gobject>
|
, public ref_counter<gobject> {
|
||||||
, public intrusive_list_hook<world_gobjects_ilist_tag> {
|
|
||||||
public:
|
public:
|
||||||
gobject(ecs::registry& registry);
|
gobject(ecs::registry& registry);
|
||||||
gobject(ecs::registry& registry, const gobject& source);
|
|
||||||
gobject(ecs::registry& registry, const ecs::prototype& proto);
|
gobject(ecs::registry& registry, const ecs::prototype& proto);
|
||||||
~gobject() noexcept;
|
~gobject() noexcept;
|
||||||
|
|
||||||
|
ecs::entity entity() noexcept;
|
||||||
|
ecs::const_entity entity() const noexcept;
|
||||||
|
ecs::entity_filler entity_filler() noexcept;
|
||||||
|
|
||||||
|
template < typename T >
|
||||||
|
ecs::component<T> get_component() noexcept;
|
||||||
|
|
||||||
|
template < typename T >
|
||||||
|
ecs::const_component<T> get_component() const noexcept;
|
||||||
private:
|
private:
|
||||||
ecs::entity entity_;
|
ecs::entity entity_;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace e2d
|
||||||
|
{
|
||||||
|
template < typename T >
|
||||||
|
ecs::component<T> gobject::get_component() noexcept {
|
||||||
|
return ecs::component<T>(entity_);
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename T >
|
||||||
|
ecs::const_component<T> gobject::get_component() const noexcept {
|
||||||
|
return ecs::const_component<T>(entity_);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -197,7 +197,7 @@ namespace e2d
|
|||||||
|
|
||||||
inline asset_group& asset_group::add_asset(str_view address, const asset_ptr& asset) {
|
inline asset_group& asset_group::add_asset(str_view address, const asset_ptr& asset) {
|
||||||
str main_address = address::parent(address);
|
str main_address = address::parent(address);
|
||||||
auto iter = std::upper_bound(
|
const auto iter = std::upper_bound(
|
||||||
assets_.begin(), assets_.end(), main_address,
|
assets_.begin(), assets_.end(), main_address,
|
||||||
[](const str& l, const auto& r){
|
[](const str& l, const auto& r){
|
||||||
return l < r.first;
|
return l < r.first;
|
||||||
|
|||||||
@@ -8,8 +8,8 @@
|
|||||||
|
|
||||||
#include "_high.hpp"
|
#include "_high.hpp"
|
||||||
|
|
||||||
|
#include "prefab.hpp"
|
||||||
#include "gobject.hpp"
|
#include "gobject.hpp"
|
||||||
#include "assets/prefab_asset.hpp"
|
|
||||||
|
|
||||||
namespace e2d
|
namespace e2d
|
||||||
{
|
{
|
||||||
@@ -29,17 +29,17 @@ namespace e2d
|
|||||||
priority_render_section_end = 4500
|
priority_render_section_end = 4500
|
||||||
};
|
};
|
||||||
public:
|
public:
|
||||||
world();
|
world() = default;
|
||||||
~world() noexcept final;
|
~world() noexcept final;
|
||||||
|
|
||||||
ecs::registry& registry() noexcept;
|
ecs::registry& registry() noexcept;
|
||||||
const ecs::registry& registry() const noexcept;
|
const ecs::registry& registry() const noexcept;
|
||||||
|
|
||||||
gobject_iptr instantiate();
|
gobject_iptr instantiate();
|
||||||
gobject_iptr instantiate(const prefab_asset::ptr& prefab);
|
gobject_iptr instantiate(const prefab& prefab);
|
||||||
gobject_iptr instantiate(const const_gobject_iptr& gobject);
|
void destroy_instance(const gobject_iptr& inst) noexcept;
|
||||||
private:
|
private:
|
||||||
ecs::registry registry_;
|
ecs::registry registry_;
|
||||||
world_gobjects gobjects_;
|
hash_map<ecs::entity_id, gobject_iptr> gobjects_;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,13 +11,22 @@ namespace e2d
|
|||||||
gobject::gobject(ecs::registry& registry)
|
gobject::gobject(ecs::registry& registry)
|
||||||
: entity_(registry.create_entity()) {}
|
: 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)
|
gobject::gobject(ecs::registry& registry, const ecs::prototype& proto)
|
||||||
: entity_(registry.create_entity(proto)) {}
|
: entity_(registry.create_entity(proto)) {}
|
||||||
|
|
||||||
gobject::~gobject() noexcept {
|
gobject::~gobject() noexcept {
|
||||||
entity_.destroy();
|
entity_.destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ecs::entity gobject::entity() noexcept {
|
||||||
|
return entity_;
|
||||||
|
}
|
||||||
|
|
||||||
|
ecs::const_entity gobject::entity() const noexcept {
|
||||||
|
return entity_;
|
||||||
|
}
|
||||||
|
|
||||||
|
ecs::entity_filler gobject::entity_filler() noexcept {
|
||||||
|
return ecs::entity_filler(entity_);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,8 +8,11 @@
|
|||||||
|
|
||||||
namespace e2d
|
namespace e2d
|
||||||
{
|
{
|
||||||
world::world() = default;
|
world::~world() noexcept {
|
||||||
world::~world() noexcept = default;
|
while ( !gobjects_.empty() ) {
|
||||||
|
destroy_instance(gobjects_.begin()->second);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ecs::registry& world::registry() noexcept {
|
ecs::registry& world::registry() noexcept {
|
||||||
return registry_;
|
return registry_;
|
||||||
@@ -20,27 +23,21 @@ namespace e2d
|
|||||||
}
|
}
|
||||||
|
|
||||||
gobject_iptr world::instantiate() {
|
gobject_iptr world::instantiate() {
|
||||||
auto instance = gobject_iptr(new gobject(registry_));
|
auto instance = make_intrusive<gobject>(registry_);
|
||||||
intrusive_ptr_add_ref(instance.get());
|
gobjects_.emplace(instance->entity().id(), instance);
|
||||||
gobjects_.push_back(*instance);
|
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
gobject_iptr world::instantiate(const prefab_asset::ptr& prefab) {
|
gobject_iptr world::instantiate(const prefab& prefab) {
|
||||||
auto instance = prefab
|
auto instance = make_intrusive<gobject>(registry_, prefab.prototype());
|
||||||
? gobject_iptr(new gobject(registry_, prefab->content().prototype()))
|
gobjects_.emplace(instance->entity().id(), instance);
|
||||||
: gobject_iptr(new gobject(registry_));
|
|
||||||
intrusive_ptr_add_ref(instance.get());
|
|
||||||
gobjects_.push_back(*instance);
|
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
gobject_iptr world::instantiate(const const_gobject_iptr& source) {
|
void world::destroy_instance(const gobject_iptr& inst) noexcept {
|
||||||
auto instance = source
|
if ( inst ) {
|
||||||
? gobject_iptr(new gobject(registry_, *source))
|
inst->entity().remove_all_components();
|
||||||
: gobject_iptr(new gobject(registry_));
|
gobjects_.erase(inst->entity().id());
|
||||||
intrusive_ptr_add_ref(instance.get());
|
}
|
||||||
gobjects_.push_back(*instance);
|
|
||||||
return instance;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user