Streamlined library and reworked the way runtime components work

This commit is contained in:
2020-05-12 02:06:10 +02:00
parent c794e4a5d2
commit de5af0a5aa
11 changed files with 424 additions and 359 deletions

View File

@@ -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';
}
}