initial ECS based on EnTT

This commit is contained in:
Hartmut Seichter 2020-12-08 23:06:09 +01:00
parent 2ef6af25c1
commit cea83d06e8
9 changed files with 92 additions and 14 deletions

View file

@ -1,6 +1,9 @@
//#include "script_scene.hpp"
#include "pw/scene/node.hpp"
#include "pw/scene/entity.hpp"
#include "pw/scene/scene.hpp"
#include "runtime_lua.hpp"
namespace pw {
@ -8,6 +11,14 @@ namespace pw {
void register_scene_function(sol::state&,sol::table &ns)
{
ns.new_usertype<scene>("scene",
sol::constructors<scene()>());
ns.new_usertype<entity>("entity",
sol::constructors<entity>());
ns.new_usertype<node>("node",
sol::constructors<node()>(),
"add_child",&node::add_child,

View file

@ -1,6 +1,7 @@
set(scripts_demo
${CMAKE_SOURCE_DIR}/src/scripts/demos/simple_000.lua
${CMAKE_SOURCE_DIR}/src/scripts/demos/simple_001.lua
)
set(scripts_test

View file

@ -38,6 +38,7 @@ class node;
/**
* @brief components represent attributes of the scene nodes
*/
class component {
public:

View file

@ -38,24 +38,42 @@ namespace pw {
class entity {
public:
entity() = delete;
entity() = default;
entity(const entity&) = default;
entity(scene& s);
~entity();
template <typename T,typename... Args>
auto add_component(Args&&... args) {
return _scene._registry.emplace<T>(_entity,std::forward<Args>(args)...);
T& add_component(Args&&... args) {
return _registry->emplace<T>(this->_entity,std::forward<Args>(args)...);
}
template <typename T>
void remove_component(entity) {
_registry->remove<T>(_entity);
}
template<typename T>
bool has_component(){ return _scene._registry.valid(_entity);}
T& get_component() {
return _registry->get<T>(_entity);
}
template<typename T>
bool has_component(){ return _registry->valid(_entity);}
void destroy();
operator bool() const { return _registry && _entity != entt::null; }
operator std::uint32_t() const { return static_cast<std::uint32_t>(_entity); }
bool valid() const { return _registry->valid(_entity); }
private:
scene& _scene;
std::shared_ptr<entt::registry> _registry;
entt::entity _entity { entt::null };
};
}

View file

@ -32,13 +32,14 @@ namespace pw {
class entity;
class scene : public std::enable_shared_from_this<scene> {
class scene {
public:
scene() = default;
scene();
~scene() = default;
protected:
entt::registry _registry;
std::shared_ptr<entt::registry> _registry;
friend class entity;
};

View file

@ -3,19 +3,48 @@
namespace pw {
entity::entity(scene& s)
: _scene(s)
, _entity(s._registry.create())
namespace test {
struct relationship {
std::vector<entt::entity> children;
entt::entity parent { entt::null };
static void add_child(entt::registry& r,entt::entity p,entt::entity c)
{
// auto child_rels = r.view<relationship>(c);
// remove potential parents
}
};
void testbed()
{
using namespace entt;
registry r;
auto e = r.create();
}
}
entity::entity(scene &s)
: _registry(s._registry)
, _entity(s._registry->create())
{
}
entity::~entity()
{
this->destroy();
}
void entity::destroy()
{
_scene._registry.destroy(_entity);
_registry->destroy(_entity);
}
}

View file

@ -3,5 +3,10 @@
namespace pw {
scene::scene()
: _registry{ std::make_shared<entt::registry>()}
{
}
}

View file

@ -0,0 +1,12 @@
--
-- small demonstrator for Lua binding on pixwerx
--
-- loading our libraries
pw.script:load_all()
print("hello pixwerx!")
local s = pw.scene.new()

View file

@ -66,7 +66,7 @@ struct texture::impl {
//
//
// Wrapper
//
texture::texture()