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>
|
template <typename T, typename... Constructor, typename... Args>
|
||||||
void register_component(sol::state& lua, Args... args) {
|
void register_component(sol::state& lua, sol::table& table, Args... args) {
|
||||||
lua.new_usertype<T>(get_typename<T>(),
|
table.new_usertype<T>(get_typename<T>(),
|
||||||
"new", sol::factories([](Constructor... constructor) {
|
"new", sol::factories([](Constructor... constructor) {
|
||||||
return new T(constructor...);
|
return new T(constructor...);
|
||||||
}), args...
|
}), args...
|
||||||
|
|
|
@ -2,55 +2,61 @@
|
||||||
|
|
||||||
namespace ecs::lua {
|
namespace ecs::lua {
|
||||||
void init(sol::state& 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",
|
ecs.new_usertype<Entity>("Entity",
|
||||||
"__tostring", [] (Entity* thiz) {
|
"__tostring", [] (Entity* thiz) {
|
||||||
return uuids::to_string(thiz->uuid);
|
return uuids::to_string(thiz->uuid);
|
||||||
},
|
},
|
||||||
"add_component", [] (Entity* thiz, std::string name, Component* component) {
|
"add_component", [] (Entity* thiz, std::string name, Component* component) {
|
||||||
return thiz->add_component(ComponentID::get_id({name})[0], component);
|
return thiz->add_component(ComponentID::get_id({name})[0], component);
|
||||||
},
|
},
|
||||||
"has_components", [] (Entity* thiz, sol::variadic_args args) {
|
"has_components", [] (Entity* thiz, sol::variadic_args args) {
|
||||||
std::vector<std::string> names;
|
std::vector<std::string> names;
|
||||||
for (std::string name : args) {
|
for (std::string name : args) {
|
||||||
names.push_back(name);
|
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 {
|
ecs.new_usertype<View<>>("View",
|
||||||
// Convert to the correct component type
|
"for_each", &View<>::for_each
|
||||||
auto f1 = lua["_internal_to_" + name];
|
);
|
||||||
if (f1.valid()) {
|
|
||||||
return f1(thiz->get_component(ComponentID::get_id({name})[0]));
|
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
|
register_component<LuaWrapper, sol::object>(lua, ecs,
|
||||||
auto f2 = lua["_internal_to_LuaWrapper"];
|
"object", &LuaWrapper::object
|
||||||
return f2(thiz->get_component(ComponentID::get_id({name})[0]));
|
);
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
ecs.new_usertype<View<>>("View",
|
return ecs;
|
||||||
"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
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,4 +37,5 @@ executable "test"
|
||||||
dependency "ecs-lua"
|
dependency "ecs-lua"
|
||||||
dependency "ecs-serial"
|
dependency "ecs-serial"
|
||||||
|
|
||||||
|
run_dir "test"
|
||||||
run_target "test"
|
run_target "test"
|
||||||
|
|
|
@ -22,23 +22,33 @@ struct Velocity : ecs::Component {
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
sol::state lua;
|
sol::state lua;
|
||||||
lua.open_libraries(sol::lib::base);
|
lua.open_libraries(sol::lib::base, sol::lib::package);
|
||||||
|
|
||||||
ecs::lua::init(lua);
|
ecs::lua::init(lua);
|
||||||
|
|
||||||
ecs::lua::register_component<Position, float, float>(lua,
|
// @todo We should probably make this simpler to do...
|
||||||
"x", &Position::x,
|
sol::table preload = lua["package"]["preload"];
|
||||||
"y", &Position::y
|
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>(
|
ecs::serial::register_component<Position>(
|
||||||
&Position::x,
|
&Position::x,
|
||||||
&Position::y
|
&Position::y
|
||||||
);
|
);
|
||||||
|
|
||||||
ecs::lua::register_component<Velocity, float, float>(lua,
|
|
||||||
"x", &Velocity::x,
|
|
||||||
"y", &Velocity::y
|
|
||||||
);
|
|
||||||
ecs::serial::register_component<Velocity>(
|
ecs::serial::register_component<Velocity>(
|
||||||
&Velocity::x,
|
&Velocity::x,
|
||||||
&Velocity::y
|
&Velocity::y
|
||||||
|
@ -57,67 +67,7 @@ int main() {
|
||||||
return manager;
|
return manager;
|
||||||
});
|
});
|
||||||
|
|
||||||
lua.script(R"lua(
|
lua.script_file("test.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");
|
|
||||||
|
|
||||||
// for (int i = 0; i < 10; ++i) {
|
// for (int i = 0; i < 10; ++i) {
|
||||||
// // We can create entities
|
// // 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