Component ids can now be serialized and deserialized, deserializer converts between ids
This commit is contained in:
@@ -24,7 +24,22 @@ inline void handle_error(sol::optional<std::string> maybe_msg) {
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int main(int argc, const char** argv) {
|
||||
bool save;
|
||||
if (argc == 2) {
|
||||
if (!std::string("save").compare(argv[1])) {
|
||||
save = true;
|
||||
} else if (!std::string("load").compare(argv[1])) {
|
||||
save = false;
|
||||
} else {
|
||||
std::cerr << "Usage: " << argv[0] << " [save/load]\n";
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
std::cerr << "Usage: " << argv[0] << " [save/load]\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
sol::state lua(sol::c_call<decltype(&handle_error), &handle_error>);
|
||||
lua.open_libraries(sol::lib::base, sol::lib::package);
|
||||
|
||||
@@ -33,8 +48,7 @@ int main() {
|
||||
generated::init(lua);
|
||||
generated::init();
|
||||
|
||||
ecs::Manager manager;
|
||||
|
||||
// @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;
|
||||
@@ -107,13 +121,14 @@ int main() {
|
||||
break;
|
||||
|
||||
case sol::type::number:
|
||||
// @todo Handle floats
|
||||
wrapper->table[name] = iohelper::read<float>(is);
|
||||
break;
|
||||
|
||||
case sol::type::boolean:
|
||||
{
|
||||
wrapper->table[name] = iohelper::read<bool>(is);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
@@ -122,117 +137,124 @@ int main() {
|
||||
}
|
||||
);
|
||||
|
||||
// Testing interop with lua
|
||||
{
|
||||
if (save) {
|
||||
ecs::Manager manager;
|
||||
|
||||
lua.set_function("get_manager", [&manager] () -> ecs::Manager& {
|
||||
return manager;
|
||||
});
|
||||
// Testing interop with lua
|
||||
{
|
||||
|
||||
lua.safe_script_file("test.lua");
|
||||
lua.set_function("get_manager", [&manager] () -> ecs::Manager& {
|
||||
return manager;
|
||||
});
|
||||
|
||||
std::cout << "Update position\n";
|
||||
manager.view<Position, Velocity>().for_each([](ecs::Entity*, Position* pos, Velocity* vel) {
|
||||
pos->x += vel->x;
|
||||
pos->y += vel->y;
|
||||
});
|
||||
lua.safe_script_file("test.lua");
|
||||
|
||||
std::cout << "Show position!\n";
|
||||
manager.view<Position>().for_each([](ecs::Entity*, Position* pos) {
|
||||
std::cout << "X: " << pos->x << '\n';
|
||||
std::cout << "Y: " << pos->y << '\n';
|
||||
});
|
||||
std::cout << "Update position\n";
|
||||
manager.view<Position, Velocity>().for_each([](ecs::Entity*, Position* pos, Velocity* vel) {
|
||||
pos->x += vel->x;
|
||||
pos->y += vel->y;
|
||||
});
|
||||
|
||||
// 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;
|
||||
std::cout << "Show position!\n";
|
||||
manager.view<Position>().for_each([](ecs::Entity*, Position* pos) {
|
||||
std::cout << "X: " << pos->x << '\n';
|
||||
std::cout << "Y: " << pos->y << '\n';
|
||||
});
|
||||
|
||||
random["a"] = 21;
|
||||
std::cout << random["a"].get<std::string>() << '\n';
|
||||
};
|
||||
// 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::get_id({"Random"}))) {
|
||||
sol::table random = ((ecs::lua::Wrapper*)entity->get_component(ecs::ComponentID::get_id({"Random"})[0]))->table;
|
||||
random["a"] = 21;
|
||||
std::cout << random["a"].get<std::string>() << '\n';
|
||||
};
|
||||
|
||||
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;
|
||||
|
||||
std::cout << random["a"].get<std::string>() << '\n';
|
||||
};
|
||||
}
|
||||
|
||||
// Test serialization
|
||||
{
|
||||
std::cout << "STORE\n";
|
||||
|
||||
// Create entities and store them
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
// We can create entities
|
||||
ecs::Entity* entity = manager.create_entity();
|
||||
// Then we can add components to them
|
||||
entity->add_component<Position>(0.0f, 0.0f);
|
||||
if (i % 2 == 0) {
|
||||
entity->add_component<Velocity>(0.1f, 0.2f);
|
||||
}
|
||||
}
|
||||
|
||||
manager.view<Position, Velocity>().for_each([] (ecs::Entity*, Position* pos, Velocity* vel) {
|
||||
pos->x += vel->x;
|
||||
pos->y += vel->y;
|
||||
});
|
||||
|
||||
std::ofstream file("entities", std::ios::out | std::ios::trunc);
|
||||
ecs::serial::serialize_ids(file);
|
||||
|
||||
iohelper::write_length(file, manager.view<>().size());
|
||||
for (auto [uuid, entity] : manager.view<>()) {
|
||||
ecs::serial::serialize(file, entity);
|
||||
}
|
||||
file.close();
|
||||
|
||||
manager.view<Position>().for_each([] (ecs::Entity* entity, Position* pos) {
|
||||
std::cout << "uuid: " << entity->uuid << '\n';
|
||||
std::cout << "x: " << pos->x << '\n';
|
||||
std::cout << "y: " << pos->y << '\n';
|
||||
});
|
||||
}
|
||||
} else {
|
||||
ecs::Manager manager;
|
||||
{
|
||||
std::cout << "LOAD\n";
|
||||
|
||||
// Load entities from disk as a test
|
||||
std::ifstream file("entities", std::ios::in);
|
||||
|
||||
ecs::serial::deserialize_ids(file);
|
||||
|
||||
size_t entity_count = iohelper::read_length(file);
|
||||
size_t pos = file.tellg();
|
||||
for (size_t i = 0; i < entity_count; ++i) {
|
||||
ecs::serial::deserialize(file, manager);
|
||||
}
|
||||
|
||||
// Load the first entity again (simulating an update)
|
||||
file.seekg(pos, std::ios::beg);
|
||||
ecs::serial::deserialize(file, manager);
|
||||
|
||||
if (false) {
|
||||
auto ent = manager.get_entity(uuids::uuid::from_string("6d58fdb5-6d8c-4e6f-89d4-f7d7b184f463").value());
|
||||
if (ent->has_components<Position>()) {
|
||||
auto pos = ent->get_component<Position>();
|
||||
pos->x = 1.2f;
|
||||
pos->y = 3.4f;
|
||||
}
|
||||
}
|
||||
|
||||
manager.view<Position>().for_each([] (ecs::Entity* entity, Position* pos) {
|
||||
std::cout << "uuid: " << entity->uuid << '\n';
|
||||
std::cout << "x: " << pos->x << '\n';
|
||||
std::cout << "y: " << pos->y << '\n';
|
||||
});
|
||||
|
||||
lua.set_function("get_manager", [&manager] () -> ecs::Manager& {
|
||||
return manager;
|
||||
});
|
||||
|
||||
lua.safe_script_file("test2.lua");
|
||||
}
|
||||
}
|
||||
|
||||
// Test serialization
|
||||
{
|
||||
std::cout << "STORE\n";
|
||||
|
||||
// Create entities and store them
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
// We can create entities
|
||||
ecs::Entity* entity = manager.create_entity();
|
||||
// Then we can add components to them
|
||||
entity->add_component<Position>(0.0f, 0.0f);
|
||||
if (i % 2 == 0) {
|
||||
entity->add_component<Velocity>(0.1f, 0.2f);
|
||||
}
|
||||
}
|
||||
|
||||
manager.view<Position, Velocity>().for_each([] (ecs::Entity*, Position* pos, Velocity* vel) {
|
||||
pos->x += vel->x;
|
||||
pos->y += vel->y;
|
||||
});
|
||||
|
||||
std::ofstream file("entities", std::ios::out | std::ios::trunc);
|
||||
iohelper::write_length(file, manager.view<>().size());
|
||||
for (auto [uuid, entity] : manager.view<>()) {
|
||||
ecs::serial::serialize(file, entity);
|
||||
}
|
||||
file.close();
|
||||
|
||||
manager.view<Position>().for_each([] (ecs::Entity* entity, Position* pos) {
|
||||
std::cout << "uuid: " << entity->uuid << '\n';
|
||||
std::cout << "x: " << pos->x << '\n';
|
||||
std::cout << "y: " << pos->y << '\n';
|
||||
});
|
||||
|
||||
// @todo This should be used to constuct a conversion table
|
||||
// This is needed to avoid id conflicts between clients
|
||||
std::ofstream ids2("ids", std::ios::out | std::ios::trunc);
|
||||
ecs::serial::serialize_ids(ids2);
|
||||
ids2.close();
|
||||
}
|
||||
|
||||
{
|
||||
std::cout << "LOAD\n";
|
||||
ecs::Manager manager2;
|
||||
|
||||
// Load entities from disk as a test
|
||||
std::ifstream file("entities", std::ios::in);
|
||||
|
||||
size_t count = iohelper::read_length(file);
|
||||
for (size_t i = 0; i < count; ++i) {
|
||||
ecs::serial::deserialize(file, manager2);
|
||||
}
|
||||
|
||||
file.seekg(0, std::ios::beg);
|
||||
iohelper::read_length(file);
|
||||
ecs::serial::deserialize(file, manager2);
|
||||
|
||||
if (false) {
|
||||
auto ent = manager2.get_entity(uuids::uuid::from_string("6d58fdb5-6d8c-4e6f-89d4-f7d7b184f463").value());
|
||||
if (ent->has_components<Position>()) {
|
||||
auto pos = ent->get_component<Position>();
|
||||
pos->x = 1.2f;
|
||||
pos->y = 3.4f;
|
||||
}
|
||||
}
|
||||
|
||||
manager2.view<Position>().for_each([] (ecs::Entity* entity, Position* pos) {
|
||||
std::cout << "uuid: " << entity->uuid << '\n';
|
||||
std::cout << "x: " << pos->x << '\n';
|
||||
std::cout << "y: " << pos->y << '\n';
|
||||
});
|
||||
|
||||
lua.set_function("get_manager", [&manager] () -> ecs::Manager& {
|
||||
return manager;
|
||||
});
|
||||
|
||||
lua.safe_script_file("test2.lua");
|
||||
for (auto [name, id] : ecs::ComponentID::_ids()) {
|
||||
std::cout << name << ' ' << id << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,21 @@
|
||||
require "ecs"
|
||||
require "ecs.Wrapper"
|
||||
|
||||
manager = get_manager()
|
||||
|
||||
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)
|
||||
-- print("alive: " .. data.alive)
|
||||
end)
|
||||
|
||||
manager:view("TestThing"):for_each(function(ent)
|
||||
data = ent:get_component("TestThing")
|
||||
print("test: " .. data.test)
|
||||
-- @todo You cannot concatenate bool to string
|
||||
print("alive: ")
|
||||
print(data.alive)
|
||||
end)
|
||||
|
||||
-- @todo Allow this
|
||||
@@ -18,4 +23,4 @@ end)
|
||||
-- print(i, v)
|
||||
-- end
|
||||
|
||||
print(manager:has_entity("6d58fdb5-6d8c-4e6f-89d4-f7d7b184f463"))
|
||||
print(manager:has_entity("70bca3cf-33dd-40dc-8b0f-2e19aa0b4a17"))
|
||||
|
||||
Reference in New Issue
Block a user