The ecs stuff does not get loaded until they are required in lua
This commit is contained in:
parent
96ad0de01a
commit
53a0b98341
|
@ -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...
|
||||
|
|
|
@ -2,7 +2,10 @@
|
|||
|
||||
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) {
|
||||
|
@ -49,8 +52,11 @@ namespace ecs::lua {
|
|||
}
|
||||
);
|
||||
|
||||
register_component<LuaWrapper, sol::object>(lua,
|
||||
register_component<LuaWrapper, sol::object>(lua, ecs,
|
||||
"object", &LuaWrapper::object
|
||||
);
|
||||
|
||||
return ecs;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,4 +37,5 @@ executable "test"
|
|||
dependency "ecs-lua"
|
||||
dependency "ecs-serial"
|
||||
|
||||
run_dir "test"
|
||||
run_target "test"
|
||||
|
|
|
@ -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,
|
||||
// @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
60
test/test.lua
Normal 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)
|
Loading…
Reference in New Issue
Block a user