Streamlined library and reworked the way runtime components work
This commit is contained in:
@@ -15,7 +15,7 @@ namespace generated {
|
||||
);
|
||||
|
||||
component.set_function("new", sol::factories([](float _x, float _y) {
|
||||
return std::make_pair(new Position(_x, _y), "Position");
|
||||
return std::make_pair(ecs::ComponentID::id<Position>, new Position(_x, _y));
|
||||
}));
|
||||
|
||||
lua.set_function("_internal_to_Position", [] (ecs::Component* component) {
|
||||
@@ -33,7 +33,7 @@ namespace generated {
|
||||
);
|
||||
|
||||
component.set_function("new", sol::factories([](float _x, float _y) {
|
||||
return std::make_pair(new Velocity(_x, _y), "Velocity");
|
||||
return std::make_pair(ecs::ComponentID::id<Velocity>, new Velocity(_x, _y));
|
||||
}));
|
||||
|
||||
lua.set_function("_internal_to_Velocity", [] (ecs::Component* component) {
|
||||
@@ -51,7 +51,7 @@ namespace generated {
|
||||
);
|
||||
|
||||
component.set_function("new", sol::factories([](std::string _name) {
|
||||
return std::make_pair(new Meta(_name), "Meta");
|
||||
return std::make_pair(ecs::ComponentID::id<Meta>, new Meta(_name));
|
||||
}));
|
||||
|
||||
lua.set_function("_internal_to_Meta", [] (ecs::Component* component) {
|
||||
|
||||
@@ -38,6 +38,11 @@ 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");
|
||||
|
||||
sol::state lua(sol::c_call<decltype(&handle_error), &handle_error>);
|
||||
lua.open_libraries(sol::lib::base, sol::lib::package);
|
||||
|
||||
@@ -46,95 +51,6 @@ int main(int argc, const char** argv) {
|
||||
generated::init(lua);
|
||||
generated::init();
|
||||
|
||||
// @todo I am pretty sure that iohelper actually has an api that allows us to write a custom (de)serializer
|
||||
ecs::serial::register_component_custom<ecs::lua::Wrapper>(
|
||||
[](std::ostream& os, ecs::Component* component) {
|
||||
ecs::lua::Wrapper* wrapper = (ecs::lua::Wrapper*)component;
|
||||
|
||||
// iohelper::write<std::string>(os, wrapper->name);
|
||||
|
||||
// #todo .size() does not work
|
||||
size_t size = 0;
|
||||
for (auto [a, b] : wrapper->table) {
|
||||
size++;
|
||||
}
|
||||
io::write<size_t>(os, size);
|
||||
|
||||
for (auto [a, b] : wrapper->table) {
|
||||
io::write<std::string>(os, a.as<std::string>());
|
||||
|
||||
sol::type type = b.get_type();
|
||||
io::write<uint8_t>(os, (uint8_t)type);
|
||||
switch (type) {
|
||||
case sol::type::none:
|
||||
case sol::type::nil:
|
||||
break;
|
||||
|
||||
case sol::type::string:
|
||||
io::write<std::string>(os, b.as<std::string>());
|
||||
break;
|
||||
|
||||
case sol::type::number:
|
||||
// @todo These should be doubles instead of floats
|
||||
io::write<float>(os, b.as<float>());
|
||||
break;
|
||||
|
||||
case sol::type::boolean:
|
||||
io::write<bool>(os, b.as<bool>());
|
||||
break;
|
||||
|
||||
case sol::type::table:
|
||||
// @todo Make this happen
|
||||
break;
|
||||
|
||||
// All other types are not supported
|
||||
default:
|
||||
throw std::runtime_error("Unsupported type in wrapped lua table");
|
||||
break;
|
||||
}
|
||||
}
|
||||
},
|
||||
[&lua](std::istream& is, ecs::Component* component) {
|
||||
ecs::lua::Wrapper* wrapper = (ecs::lua::Wrapper*)component;
|
||||
// @todo Only do this if table is not created yet
|
||||
wrapper->table = lua.create_table();
|
||||
|
||||
size_t size = io::read<size_t>(is);
|
||||
std::cout << "Size: " << size << '\n';
|
||||
for (size_t i = 0; i < size; ++i) {
|
||||
std::string name = io::read<std::string>(is);
|
||||
|
||||
std::cout << "Name: " << name << '\n';
|
||||
|
||||
sol::type type = (sol::type)io::read<uint8_t>(is);
|
||||
switch (type) {
|
||||
case sol::type::none:
|
||||
break;
|
||||
|
||||
case sol::type::nil:
|
||||
break;
|
||||
|
||||
case sol::type::string:
|
||||
wrapper->table[name] = io::read<std::string>(is);
|
||||
break;
|
||||
|
||||
case sol::type::number:
|
||||
wrapper->table[name] = io::read<float>(is);
|
||||
break;
|
||||
|
||||
case sol::type::boolean:
|
||||
{
|
||||
wrapper->table[name] = io::read<bool>(is);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
if (save) {
|
||||
ecs::Manager manager;
|
||||
|
||||
@@ -147,6 +63,14 @@ int main(int argc, const char** argv) {
|
||||
|
||||
lua.safe_script_file("test.lua");
|
||||
|
||||
sol::protected_function init = lua["init"];
|
||||
std::cout << "Running init()\n";
|
||||
init();
|
||||
|
||||
sol::protected_function run = lua["run"];
|
||||
std::cout << "Running run()\n";
|
||||
run();
|
||||
|
||||
std::cout << "Update position\n";
|
||||
manager.view<Position, Velocity>().for_each([](ecs::Entity*, Position* pos, Velocity* vel) {
|
||||
pos->x += vel->x;
|
||||
@@ -160,15 +84,15 @@ int main(int argc, const char** argv) {
|
||||
});
|
||||
|
||||
// These are really just an internal api that should not be used
|
||||
for (auto [uuid, entity] : manager.view(ecs::ComponentID::get_id({"Random"}))) {
|
||||
sol::table random = ((ecs::lua::Wrapper*)entity->get_component(ecs::ComponentID::get_id({"Random"})[0]))->table;
|
||||
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::get_id({"Random"}))) {
|
||||
sol::table random = ((ecs::lua::Wrapper*)entity->get_component(ecs::ComponentID::get_id({"Random"})[0]))->table;
|
||||
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';
|
||||
};
|
||||
@@ -214,6 +138,11 @@ int main(int argc, const char** argv) {
|
||||
{
|
||||
std::cout << "LOAD\n";
|
||||
|
||||
lua.safe_script_file("test2.lua");
|
||||
sol::protected_function init = lua["init"];
|
||||
std::cout << "Running init()\n";
|
||||
init();
|
||||
|
||||
// Load entities from disk as a test
|
||||
std::ifstream file("entities", std::ios::in);
|
||||
|
||||
@@ -248,11 +177,13 @@ int main(int argc, const char** argv) {
|
||||
return manager;
|
||||
});
|
||||
|
||||
lua.safe_script_file("test2.lua");
|
||||
sol::protected_function run = lua["run"];
|
||||
std::cout << "Running run()\n";
|
||||
run();
|
||||
}
|
||||
}
|
||||
|
||||
for (auto [name, id] : ecs::ComponentID::_ids()) {
|
||||
for (auto [name, id] : ecs::ComponentID::get_map()) {
|
||||
std::cout << name << ' ' << id << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
143
test/test.lua
143
test/test.lua
@@ -1,87 +1,102 @@
|
||||
local ecs = require "ecs"
|
||||
-- local test = require "ecs_test"
|
||||
local Position = require "ecs.Position"
|
||||
local Velocity = require "ecs.Velocity"
|
||||
local Meta = require "ecs.Meta"
|
||||
local Wrapper = require 'ecs.Wrapper'
|
||||
|
||||
manager = get_manager()
|
||||
ent = manager:create_entity()
|
||||
ent:add_component(Velocity.new(0.2, 0.3))
|
||||
ent:add_component(Position.new(1.9, 9.7))
|
||||
ent:add_component(Meta.new("Soldier"))
|
||||
function init()
|
||||
print("Registering components")
|
||||
|
||||
data = {
|
||||
speed = 10,
|
||||
something = "Hello, World!",
|
||||
alive = true
|
||||
}
|
||||
ent:add_component(Wrapper.new("LuaData", data))
|
||||
ent:add_component(Wrapper.new("TestThing", { test = 1.23563 }))
|
||||
LuaData = ecs.register("LuaData", {
|
||||
speed = "number",
|
||||
something = "string",
|
||||
alive = "boolean"
|
||||
})
|
||||
TestThing = ecs.register("TestThing", {
|
||||
test = "string"
|
||||
})
|
||||
end
|
||||
|
||||
-- @todo Make this happen...
|
||||
-- stats = {
|
||||
-- hp = 100,
|
||||
-- magic = 30
|
||||
-- }
|
||||
--
|
||||
-- ent:add_component(ecs.Wrapper.new("Stats", stats))
|
||||
|
||||
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)
|
||||
function run()
|
||||
print("Running!")
|
||||
manager = get_manager()
|
||||
ent = manager:create_entity()
|
||||
ent:add_component(Position.new(1.9, 9.7))
|
||||
ent:add_component(Velocity.new(0.2, 0.3))
|
||||
ent:add_component(Meta.new("Soldier"))
|
||||
|
||||
print("View test")
|
||||
manager:view("Position", "Velocity"):for_each(function(ent)
|
||||
ent:add_component(LuaData.new({
|
||||
speed = 10,
|
||||
something = "Hello, World!",
|
||||
alive = true
|
||||
}))
|
||||
ent:add_component(TestThing.new({
|
||||
test = "This is a test!"
|
||||
}))
|
||||
|
||||
-- @todo Make this happen...
|
||||
-- stats = {
|
||||
-- hp = 100,
|
||||
-- magic = 30
|
||||
-- }
|
||||
--
|
||||
-- ent:add_component(ecs.Wrapper.new("Stats", stats))
|
||||
|
||||
print(ent:has_components("Position", "Velocity"))
|
||||
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)
|
||||
|
||||
vel = ent:get_component("Velocity")
|
||||
print("v_x: " .. vel.x)
|
||||
print("v_y: " .. vel.y)
|
||||
|
||||
if ent:has_components("Meta") then
|
||||
meta = ent:get_component("Meta")
|
||||
print("name: " .. meta.name)
|
||||
end
|
||||
end)
|
||||
print("View test")
|
||||
manager:view("Position", "Velocity"):for_each(function(ent)
|
||||
pos = ent:get_component("Position")
|
||||
vel = ent:get_component("Velocity")
|
||||
|
||||
-- @todo Implement this
|
||||
-- for uuid,entity in pairs(view) do
|
||||
-- print "TEST"
|
||||
-- end
|
||||
pos.x = pos.x + vel.x
|
||||
pos.y = pos.y + vel.y
|
||||
|
||||
manager:view("LuaData"):for_each(function(ent)
|
||||
d = ent:get_component("LuaData")
|
||||
print(ent)
|
||||
|
||||
print(d.speed)
|
||||
d.speed = 11
|
||||
print(d.speed)
|
||||
print(data.speed)
|
||||
data.speed = 20
|
||||
print(d.speed)
|
||||
print(data.speed)
|
||||
print("x: " .. pos.x)
|
||||
print("y: " .. pos.y)
|
||||
|
||||
print(d.alive)
|
||||
d.alive = false
|
||||
print(d.alive)
|
||||
print("v_x: " .. vel.x)
|
||||
print("v_y: " .. vel.y)
|
||||
|
||||
print(d.something)
|
||||
end)
|
||||
if ent:has_components("Meta") then
|
||||
meta = ent:get_component("Meta")
|
||||
print("name: " .. meta.name)
|
||||
end
|
||||
end)
|
||||
|
||||
manager:view("TestThing"):for_each(function(ent)
|
||||
t = ent:get_component("TestThing")
|
||||
-- @todo Implement this
|
||||
-- for uuid,entity in pairs(view) do
|
||||
-- print "TEST"
|
||||
-- end
|
||||
|
||||
manager:view("LuaData"):for_each(function(ent)
|
||||
d = ent:get_component("LuaData")
|
||||
|
||||
print(t.test)
|
||||
end)
|
||||
print("speed: " .. d.speed)
|
||||
d.speed = 11
|
||||
print("speed: " .. d.speed)
|
||||
|
||||
print("alive: ") print(d.alive)
|
||||
d.alive = false
|
||||
print("alive: ") print(d.alive)
|
||||
|
||||
print("Something: " .. d.something)
|
||||
end)
|
||||
|
||||
manager:view("TestThing"):for_each(function(ent)
|
||||
t = ent:get_component("TestThing")
|
||||
|
||||
print("test: " .. t.test)
|
||||
end)
|
||||
|
||||
print("Done running!")
|
||||
end
|
||||
|
||||
@@ -1,26 +1,49 @@
|
||||
require "ecs"
|
||||
local ecs = require "ecs"
|
||||
require "ecs.Wrapper"
|
||||
|
||||
manager = get_manager()
|
||||
function init()
|
||||
print("Registering components")
|
||||
|
||||
manager:view("TestThing"):for_each(function(ent)
|
||||
data = ent:get_component("TestThing")
|
||||
print("test: " .. data.test)
|
||||
end)
|
||||
LuaData = ecs.register("LuaData", {
|
||||
speed = "number",
|
||||
something = "string",
|
||||
alive = "boolean"
|
||||
})
|
||||
TestThing = ecs.register("TestThing", {
|
||||
test = "string"
|
||||
})
|
||||
end
|
||||
|
||||
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")
|
||||
print("speed: " .. data.speed)
|
||||
print("something: " .. data.something)
|
||||
-- @todo You cannot concatenate bool to string
|
||||
print("alive: ")
|
||||
print(data.alive)
|
||||
end)
|
||||
function run()
|
||||
print("Running!")
|
||||
|
||||
-- @todo Allow this
|
||||
-- for i, v in pairs(manager:view("TestThing")) do
|
||||
-- print(i, v)
|
||||
-- end
|
||||
manager = get_manager()
|
||||
|
||||
print(manager:has_entity("70bca3cf-33dd-40dc-8b0f-2e19aa0b4a17"))
|
||||
print("AAA")
|
||||
|
||||
manager:view("TestThing"):for_each(function(ent)
|
||||
data = ent:get_component("TestThing")
|
||||
print("test: " .. data.test)
|
||||
end)
|
||||
|
||||
|
||||
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")
|
||||
print("speed: " .. data.speed)
|
||||
print("something: " .. data.something)
|
||||
-- @todo You cannot concatenate bool to string
|
||||
print("alive: ")
|
||||
print(data.alive)
|
||||
end)
|
||||
|
||||
-- @todo Allow this
|
||||
-- for i, v in pairs(manager:view("TestThing")) do
|
||||
-- print(i, v)
|
||||
-- end
|
||||
|
||||
print("Exists: ")
|
||||
print(manager:has_entity("70bca3cf-33dd-40dc-8b0f-2e19aa0b4a17"))
|
||||
|
||||
print("Done running!")
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user