Added codegen to register components, and started reworking lua

This commit is contained in:
2019-06-07 15:52:57 +02:00
parent 53a0b98341
commit 7ff9e21b4d
14 changed files with 381 additions and 110 deletions

View File

@@ -4,20 +4,22 @@
#include "ecs.h"
#include <iostream>
#include <utility>
namespace ecs::lua {
struct LuaWrapper : Component {
LuaWrapper(sol::object _object) : object(_object) {}
LuaWrapper() {}
struct Wrapper : Component {
Wrapper(std::string _name, sol::table _table) : name(_name), table(_table) {}
Wrapper() {}
sol::object object;
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 new T(constructor...);
return std::make_pair(new T(constructor...), get_typename<T>());
}), args...
);
lua.set_function("_internal_to_" + get_typename<T>(), [] (Component* component) {

View File

@@ -1,5 +1,6 @@
#include "ecs-lua.h"
#include "ecs.h"
namespace ecs::lua {
void init(sol::state& lua) {
// Add a preloader that loads all the ecs stuff
@@ -11,7 +12,7 @@ namespace ecs::lua {
"__tostring", [] (Entity* thiz) {
return uuids::to_string(thiz->uuid);
},
"add_component", [] (Entity* thiz, std::string name, Component* component) {
"add_component", [] (Entity* thiz, Component* component, std::string name) {
return thiz->add_component(ComponentID::get_id({name})[0], component);
},
"has_components", [] (Entity* thiz, sol::variadic_args args) {
@@ -27,10 +28,7 @@ namespace ecs::lua {
if (f1.valid()) {
return f1(thiz->get_component(ComponentID::get_id({name})[0]));
}
// If the type of the component unknown we assume it a lua object and convert it to the wrapper
auto f2 = lua["_internal_to_LuaWrapper"];
return f2(thiz->get_component(ComponentID::get_id({name})[0]));
throw std::runtime_error("Unknown component");
}
);
@@ -52,8 +50,9 @@ namespace ecs::lua {
}
);
register_component<LuaWrapper, sol::object>(lua, ecs,
"object", &LuaWrapper::object
register_component<Wrapper, std::string, sol::object>(lua, ecs,
"name", &Wrapper::name,
"table", &Wrapper::table
);
return ecs;