We can properly use range based for loops with view and interaction with Lua is significantly improved

This commit is contained in:
2020-05-13 01:17:25 +02:00
parent de5af0a5aa
commit c9f05af6c9
8 changed files with 234 additions and 172 deletions

View File

@@ -9,6 +9,8 @@ namespace generated {
preload["ecs.Position"] = [&lua] {
sol::table component = lua.create_table();
component["_id"] = ecs::ComponentID::id<Position>;
component.new_usertype<Position>("Position",
"x", &Position::x, "y", &Position::y,
sol::base_classes, sol::bases<ecs::Component>()
@@ -18,7 +20,7 @@ namespace generated {
return std::make_pair(ecs::ComponentID::id<Position>, new Position(_x, _y));
}));
lua.set_function("_internal_to_Position", [] (ecs::Component* component) {
component.set_function("_convert", [] (ecs::Component* component) {
return (Position*)component;
});
@@ -27,6 +29,8 @@ namespace generated {
preload["ecs.Velocity"] = [&lua] {
sol::table component = lua.create_table();
component["_id"] = ecs::ComponentID::id<Velocity>;
component.new_usertype<Velocity>("Velocity",
"x", &Velocity::x, "y", &Velocity::y,
sol::base_classes, sol::bases<ecs::Component>()
@@ -36,7 +40,7 @@ namespace generated {
return std::make_pair(ecs::ComponentID::id<Velocity>, new Velocity(_x, _y));
}));
lua.set_function("_internal_to_Velocity", [] (ecs::Component* component) {
component.set_function("_convert", [] (ecs::Component* component) {
return (Velocity*)component;
});
@@ -45,6 +49,8 @@ namespace generated {
preload["ecs.Meta"] = [&lua] {
sol::table component = lua.create_table();
component["_id"] = ecs::ComponentID::id<Meta>;
component.new_usertype<Meta>("Meta",
"name", &Meta::name,
sol::base_classes, sol::bases<ecs::Component>()
@@ -54,7 +60,7 @@ namespace generated {
return std::make_pair(ecs::ComponentID::id<Meta>, new Meta(_name));
}));
lua.set_function("_internal_to_Meta", [] (ecs::Component* component) {
component.set_function("_convert", [] (ecs::Component* component) {
return (Meta*)component;
});

View File

@@ -14,6 +14,14 @@
#include "ecs.h"
#include "ecs_components.h"
class Orb : public ecs::Entity {
public:
Orb(uuids::uuid uuid, float x, float y) : Entity(uuid) {
auto position = add_component<Position>(x, y);
auto velocity = add_component<Velocity>(0, 0);
}
};
inline void handle_error(sol::optional<std::string> maybe_msg) {
std::cerr << "Lua error, aborting!\n";
if (maybe_msg) {
@@ -38,10 +46,9 @@ int main(int argc, const char** argv) {
return -1;
}
ecs::ComponentID::regist<ecs::lua::Wrapper>("Wrapper");
ecs::ComponentID::regist<Position>("Position");
ecs::ComponentID::regist<Velocity>("Velocity");
ecs::ComponentID::regist<Meta>("Meta");
ecs::ComponentID::add<Position>("Position");
ecs::ComponentID::add<Velocity>("Velocity");
ecs::ComponentID::add<Meta>("Meta");
sol::state lua(sol::c_call<decltype(&handle_error), &handle_error>);
lua.open_libraries(sol::lib::base, sol::lib::package);
@@ -83,19 +90,12 @@ int main(int argc, const char** argv) {
std::cout << "Y: " << pos->y << '\n';
});
// These are really just an internal api that should not be used
for (auto [uuid, entity] : manager.view(ecs::ComponentID::resolve("Random"))) {
sol::table random = ((ecs::lua::Wrapper*)entity->get_component(ecs::ComponentID::resolve("Random")))->table;
random["a"] = 21;
std::cout << random["a"].get<std::string>() << '\n';
};
for (auto [uuid, entity] : manager.view(ecs::ComponentID::resolve("Random"))) {
sol::table random = ((ecs::lua::Wrapper*)entity->get_component(ecs::ComponentID::resolve("Random")))->table;
std::cout << random["a"].get<std::string>() << '\n';
};
std::cout << "Adding Orb!\n";
manager.create_entity<Orb>(1.2f, 3.4f);
for (auto [ent, pos, vel] : manager.view<Position, Velocity>()) {
std::cout << "X: " << pos->x << " + " << vel->x << '\n';
std::cout << "Y: " << pos->y << " + " << vel->y << '\n';
}
}
// Test serialization
@@ -122,7 +122,7 @@ int main(int argc, const char** argv) {
ecs::serial::serialize_ids(file);
io::write<size_t>(file, manager.view<>().size());
for (auto [uuid, entity] : manager.view<>()) {
for (auto [entity] : manager.view<>()) {
ecs::serial::serialize(file, entity);
}
file.close();

View File

@@ -2,17 +2,17 @@ local ecs = require "ecs"
local Position = require "ecs.Position"
local Velocity = require "ecs.Velocity"
local Meta = require "ecs.Meta"
local Wrapper = require 'ecs.Wrapper'
local LuaComponent = require 'ecs.LuaComponent'
function init()
print("Registering components")
print("Creating LuaComponents")
LuaData = ecs.register("LuaData", {
LuaData = LuaComponent.create("LuaData", {
speed = "number",
something = "string",
alive = "boolean"
})
TestThing = ecs.register("TestThing", {
TestThing = LuaComponent.create("TestThing", {
test = "string"
})
end
@@ -43,18 +43,18 @@ function run()
--
-- ent:add_component(ecs.Wrapper.new("Stats", stats))
print(ent:has_components("Position", "Velocity"))
pos = ent:get_component("Position")
print(ent:has_components(Position, Velocity))
pos = ent:get_component(Position)
print("x: " .. pos.x)
print("y: " .. pos.y)
vel = ent:get_component("Velocity")
vel = ent:get_component(Velocity)
print("v_x: " .. vel.x)
print("v_y: " .. vel.y)
print("View test")
manager:view("Position", "Velocity"):for_each(function(ent)
pos = ent:get_component("Position")
vel = ent:get_component("Velocity")
manager:view(Position, Velocity):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
@@ -67,8 +67,8 @@ function run()
print("v_x: " .. vel.x)
print("v_y: " .. vel.y)
if ent:has_components("Meta") then
meta = ent:get_component("Meta")
if ent:has_components(Meta) then
meta = ent:get_component(Meta)
print("name: " .. meta.name)
end
end)
@@ -78,8 +78,8 @@ function run()
-- print "TEST"
-- end
manager:view("LuaData"):for_each(function(ent)
d = ent:get_component("LuaData")
manager:view(LuaData):for_each(function(ent)
d = ent:get_component(LuaData)
print("speed: " .. d.speed)
d.speed = 11
@@ -92,8 +92,8 @@ function run()
print("Something: " .. d.something)
end)
manager:view("TestThing"):for_each(function(ent)
t = ent:get_component("TestThing")
manager:view(TestThing):for_each(function(ent)
t = ent:get_component(TestThing)
print("test: " .. t.test)
end)

View File

@@ -1,17 +1,18 @@
local ecs = require "ecs"
require "ecs.Wrapper"
local LuaComponent = require "ecs.LuaComponent"
function init()
print("Registering components")
LuaData = ecs.register("LuaData", {
-- Swapped compared to test.lua
TestThing = LuaComponent.create("TestThing", {
test = "string"
})
LuaData = LuaComponent.create("LuaData", {
speed = "number",
something = "string",
alive = "boolean"
})
TestThing = ecs.register("TestThing", {
test = "string"
})
end
function run()
@@ -19,17 +20,15 @@ function run()
manager = get_manager()
print("AAA")
manager:view("TestThing"):for_each(function(ent)
data = ent:get_component("TestThing")
manager:view(TestThing):for_each(function(ent)
data = ent:get_component(TestThing)
print("test: " .. data.test)
end)
manager:view("LuaData"):for_each(function(ent)
manager:view(LuaData):for_each(function(ent)
-- @todo It would be nice if this could somehow be passed in as function arg
data = ent:get_component("LuaData")
data = ent:get_component(LuaData)
print("speed: " .. data.speed)
print("something: " .. data.something)
-- @todo You cannot concatenate bool to string