The ecs stuff does not get loaded until they are required in lua

This commit is contained in:
2019-03-07 00:39:02 +01:00
parent 96ad0de01a
commit 53a0b98341
5 changed files with 134 additions and 117 deletions

View File

@@ -14,8 +14,8 @@ namespace ecs::lua {
};
template <typename T, typename... Constructor, typename... Args>
void register_component(sol::state& lua, Args... args) {
lua.new_usertype<T>(get_typename<T>(),
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...);
}), args...

View File

@@ -2,55 +2,61 @@
namespace ecs::lua {
void init(sol::state& lua) {
sol::table ecs = lua.create_table("ecs");
// Add a preloader that loads all the ecs stuff
sol::table preload = lua["package"]["preload"];
preload["ecs"] = [&lua] {
sol::table ecs = lua.create_table();
ecs.new_usertype<Entity>("Entity",
"__tostring", [] (Entity* thiz) {
return uuids::to_string(thiz->uuid);
},
"add_component", [] (Entity* thiz, std::string name, Component* component) {
return thiz->add_component(ComponentID::get_id({name})[0], component);
},
"has_components", [] (Entity* thiz, sol::variadic_args args) {
std::vector<std::string> names;
for (std::string name : args) {
names.push_back(name);
ecs.new_usertype<Entity>("Entity",
"__tostring", [] (Entity* thiz) {
return uuids::to_string(thiz->uuid);
},
"add_component", [] (Entity* thiz, std::string name, Component* component) {
return thiz->add_component(ComponentID::get_id({name})[0], component);
},
"has_components", [] (Entity* thiz, sol::variadic_args args) {
std::vector<std::string> names;
for (std::string name : args) {
names.push_back(name);
}
return thiz->has_components(ComponentID::get_id(names));
},
"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]));
}
// 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]));
}
return thiz->has_components(ComponentID::get_id(names));
},
"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]));
);
ecs.new_usertype<View<>>("View",
"for_each", &View<>::for_each
);
ecs.new_usertype<Manager>("Manager",
"create_entity", [] (Manager* thiz) {
// @todo Allow to construct with given uuid
return thiz->create_entity();
},
"view", [] (Manager* thiz, sol::variadic_args args) {
std::vector<std::string> names;
for (std::string name : args) {
names.push_back(name);
}
return thiz->view(ComponentID::get_id(names));
}
);
// 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]));
}
);
register_component<LuaWrapper, sol::object>(lua, ecs,
"object", &LuaWrapper::object
);
ecs.new_usertype<View<>>("View",
"for_each", &View<>::for_each
);
ecs.new_usertype<Manager>("Manager",
"create_entity", [] (Manager* thiz) {
// @todo Allow to construct with given uuid
return thiz->create_entity();
},
"view", [] (Manager* thiz, sol::variadic_args args) {
std::vector<std::string> names;
for (std::string name : args) {
names.push_back(name);
}
return thiz->view(ComponentID::get_id(names));
}
);
register_component<LuaWrapper, sol::object>(lua,
"object", &LuaWrapper::object
);
return ecs;
};
}
}