From 53a0b98341f3bc4263f5f49c7d88190d4275bde6 Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Thu, 7 Mar 2019 00:39:02 +0100 Subject: [PATCH] The ecs stuff does not get loaded until they are required in lua --- ecs-lua/include/ecs-lua.h | 4 +- ecs-lua/src/ecs-lua.cpp | 96 +++++++++++++++++++++------------------ flint.lua | 1 + test/src/main.cpp | 90 ++++++++---------------------------- test/test.lua | 60 ++++++++++++++++++++++++ 5 files changed, 134 insertions(+), 117 deletions(-) create mode 100644 test/test.lua diff --git a/ecs-lua/include/ecs-lua.h b/ecs-lua/include/ecs-lua.h index 77c81ac..6751aed 100644 --- a/ecs-lua/include/ecs-lua.h +++ b/ecs-lua/include/ecs-lua.h @@ -14,8 +14,8 @@ namespace ecs::lua { }; template - void register_component(sol::state& lua, Args... args) { - lua.new_usertype(get_typename(), + void register_component(sol::state& lua, sol::table& table, Args... args) { + table.new_usertype(get_typename(), "new", sol::factories([](Constructor... constructor) { return new T(constructor...); }), args... diff --git a/ecs-lua/src/ecs-lua.cpp b/ecs-lua/src/ecs-lua.cpp index 7026ce6..53e1d8a 100644 --- a/ecs-lua/src/ecs-lua.cpp +++ b/ecs-lua/src/ecs-lua.cpp @@ -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", - "__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 names; - for (std::string name : args) { - names.push_back(name); + ecs.new_usertype("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 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", + "for_each", &View<>::for_each + ); + + ecs.new_usertype("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 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(lua, ecs, + "object", &LuaWrapper::object + ); - ecs.new_usertype>("View", - "for_each", &View<>::for_each - ); - - ecs.new_usertype("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 names; - for (std::string name : args) { - names.push_back(name); - } - return thiz->view(ComponentID::get_id(names)); - } - ); - - register_component(lua, - "object", &LuaWrapper::object - ); + return ecs; + }; } } diff --git a/flint.lua b/flint.lua index 77d0f07..c40767e 100644 --- a/flint.lua +++ b/flint.lua @@ -37,4 +37,5 @@ executable "test" dependency "ecs-lua" dependency "ecs-serial" +run_dir "test" run_target "test" diff --git a/test/src/main.cpp b/test/src/main.cpp index 472ae53..d55bb42 100644 --- a/test/src/main.cpp +++ b/test/src/main.cpp @@ -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(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(lua, test, + "x", &Position::x, + "y", &Position::y + ); + + ecs::lua::register_component(lua, test, + "x", &Velocity::x, + "y", &Velocity::y + ); + + return test; + }; + ecs::serial::register_component( &Position::x, &Position::y ); - ecs::lua::register_component(lua, - "x", &Velocity::x, - "y", &Velocity::y - ); ecs::serial::register_component( &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 diff --git a/test/test.lua b/test/test.lua new file mode 100644 index 0000000..18bcb09 --- /dev/null +++ b/test/test.lua @@ -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)