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

This commit is contained in:
Dreaded_X 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;
};
}
}

View File

@ -37,4 +37,5 @@ executable "test"
dependency "ecs-lua"
dependency "ecs-serial"
run_dir "test"
run_target "test"

View File

@ -22,23 +22,33 @@ struct Velocity : ecs::Component {
int main() {
sol::state lua;
lua.open_libraries(sol::lib::base);
lua.open_libraries(sol::lib::base, sol::lib::package);
ecs::lua::init(lua);
ecs::lua::register_component<Position, float, float>(lua,
"x", &Position::x,
"y", &Position::y
);
// @todo We should probably make this simpler to do...
sol::table preload = lua["package"]["preload"];
preload["ecs_test"] = [&lua] {
sol::table test = lua.create_table();
ecs::lua::register_component<Position, float, float>(lua, test,
"x", &Position::x,
"y", &Position::y
);
ecs::lua::register_component<Velocity, float, float>(lua, test,
"x", &Velocity::x,
"y", &Velocity::y
);
return test;
};
ecs::serial::register_component<Position>(
&Position::x,
&Position::y
);
ecs::lua::register_component<Velocity, float, float>(lua,
"x", &Velocity::x,
"y", &Velocity::y
);
ecs::serial::register_component<Velocity>(
&Velocity::x,
&Velocity::y
@ -57,67 +67,7 @@ int main() {
return manager;
});
lua.script(R"lua(
manager = get_manager()
ent = manager:create_entity()
-- In the future we will be able to also write components in lua
-- @todo Figure out how get_component will work in this case
ent:add_component("Position", Position.new(1.9, 9.7))
ent:add_component("Velocity", Velocity.new(0.2, 0.3))
random = {
a = 10,
b = 11,
c = function(s)
print("Hello " .. s .. "!")
end
}
ent:add_component("Random", LuaWrapper.new(random))
print(ent:has_components("Position", "Velocity"))
pos = ent:get_component("Position")
print("x: " .. pos.x)
print("y: " .. pos.y)
vel = ent:get_component("Velocity")
print("v_x: " .. vel.x)
print("v_y: " .. vel.y)
print("View test")
view = manager:view("Position", "Velocity")
view:for_each(function(ent)
pos = ent:get_component("Position")
vel = ent:get_component("Velocity")
pos.x = pos.x + vel.x
pos.y = pos.y + vel.y
print(ent)
print("x: " .. pos.x)
print("y: " .. pos.y)
print("v_x: " .. vel.x)
print("v_y: " .. vel.y)
end)
-- @todo Implement this
-- for uuid,entity in pairs(view) do
-- print "TEST"
-- end
manager:view("Random"):for_each(function(ent)
wrapped = ent:get_component("Random").object
print(wrapped.a)
wrapped.a = 11
print(wrapped.a)
print(random.a)
random.a = 20
print(wrapped.a)
print(random.a)
random.c("you")
end)
)lua");
lua.script_file("test.lua");
// for (int i = 0; i < 10; ++i) {
// // We can create entities

60
test/test.lua Normal file
View File

@ -0,0 +1,60 @@
local ecs = require "ecs"
local test = require "ecs_test"
manager = get_manager()
ent = manager:create_entity()
ent:add_component("Position", test.Position.new(1.9, 9.7))
ent:add_component("Velocity", test.Velocity.new(0.2, 0.3))
random = {
a = 10,
b = 11,
c = function(s)
print("Hello " .. s .. "!")
end
}
ent:add_component("Random", ecs.LuaWrapper.new(random))
print(ent:has_components("Position", "Velocity"))
pos = ent:get_component("Position")
print("x: " .. pos.x)
print("y: " .. pos.y)
vel = ent:get_component("Velocity")
print("v_x: " .. vel.x)
print("v_y: " .. vel.y)
print("View test")
view = manager:view("Position", "Velocity")
view:for_each(function(ent)
pos = ent:get_component("Position")
vel = ent:get_component("Velocity")
pos.x = pos.x + vel.x
pos.y = pos.y + vel.y
print(ent)
print("x: " .. pos.x)
print("y: " .. pos.y)
print("v_x: " .. vel.x)
print("v_y: " .. vel.y)
end)
-- @todo Implement this
-- for uuid,entity in pairs(view) do
-- print "TEST"
-- end
manager:view("Random"):for_each(function(ent)
wrapped = ent:get_component("Random").object
print(wrapped.a)
wrapped.a = 11
print(wrapped.a)
print(random.a)
random.a = 20
print(wrapped.a)
print(random.a)
random.c("you")
end)