Lua Wrapper component can now be serialized

This commit is contained in:
2019-06-26 20:29:28 +00:00
parent 7ff9e21b4d
commit 7d55563ec8
7 changed files with 174 additions and 62 deletions

View File

@@ -7,26 +7,14 @@
#include <utility>
namespace ecs::lua {
struct Wrapper : Component {
Wrapper(std::string _name, sol::table _table) : name(_name), table(_table) {}
Wrapper() {}
struct Wrapper : TaggedComponent {
// @todo Figure out a more elegant way
Wrapper(sol::table _table) : TaggedComponent(get_typename<Wrapper>()), table(_table) {}
Wrapper() : TaggedComponent(get_typename<Wrapper>()) {}
std::string name;
sol::table table;
};
template <typename T, typename... Constructor, typename... Args>
void register_component(sol::state& lua, sol::table& table, Args... args) {
table.new_usertype<T>(get_typename<T>(),
"new", sol::factories([](Constructor... constructor) {
return std::make_pair(new T(constructor...), get_typename<T>());
}), args...
);
lua.set_function("_internal_to_" + get_typename<T>(), [] (Component* component) {
return (T*)component;
});
}
void init(sol::state& lua);
}

View File

@@ -1,6 +1,12 @@
#include "ecs-lua.h"
#include "ecs.h"
namespace sol {
template<>
struct is_container<ecs::View<>> : std::true_type {};
};
namespace ecs::lua {
void init(sol::state& lua) {
// Add a preloader that loads all the ecs stuff
@@ -24,9 +30,19 @@ namespace ecs::lua {
},
"get_component", [&lua] (Entity* thiz, std::string name) -> sol::object {
// Convert to the correct component type
auto f1 = lua["_internal_to_" + name];
if (f1.valid()) {
return f1(thiz->get_component(ComponentID::get_id({name})[0]));
Component* component = thiz->get_component(ComponentID::get_id({name})[0]);
if (!component->_tagged()) {
// @todo Figure out a more elegant way to convert
auto f1 = lua["_internal_to_" + name];
if (f1.valid()) {
return f1(component);
}
} else {
TaggedComponent* tagged_component = (TaggedComponent*)component;
auto f1 = lua["_internal_to_" + tagged_component->_tag];
if (f1.valid()) {
return f1(component);
}
}
throw std::runtime_error("Unknown component");
}
@@ -41,6 +57,14 @@ namespace ecs::lua {
// @todo Allow to construct with given uuid
return thiz->create_entity();
},
"has_entity", [] (Manager* thiz, std::string uuid) {
// @todo Check if valid
return thiz->has_entity(uuids::uuid::from_string(uuid).value());
},
"get_entity", [] (Manager* thiz, std::string uuid) {
// @todo Check if valid
return thiz->get_entity(uuids::uuid::from_string(uuid).value());
},
"view", [] (Manager* thiz, sol::variadic_args args) {
std::vector<std::string> names;
for (std::string name : args) {
@@ -50,12 +74,29 @@ namespace ecs::lua {
}
);
register_component<Wrapper, std::string, sol::object>(lua, ecs,
"name", &Wrapper::name,
"table", &Wrapper::table
return ecs;
};
preload["ecs.Wrapper"] = [&lua] {
sol::table component = lua.create_table();
component.new_usertype<Wrapper>("Wrapper",
"__index", [] (Wrapper* thiz, std::string key) {
return thiz->table[key];
},
"__newindex", [] (Wrapper* thiz, std::string key, sol::object value) {
thiz->table[key] = value;
}
);
return ecs;
component.set_function("new", sol::factories([](std::string _name, sol::table _table) {
return std::make_pair(new Wrapper(_table), _name);
}));
lua.set_function("_internal_to_Wrapper", [] (ecs::Component* component) {
return (Wrapper*)component;
});
return component;
};
}
}