Moved majority over to flint2 and updated sol2

This commit is contained in:
2020-05-06 22:32:30 +02:00
parent 748b9c76ef
commit 3b49c00df0
9 changed files with 188 additions and 44 deletions

View File

@@ -0,0 +1,82 @@
#include "components.h"
#if __has_include("ecs-lua.h")
#include "sol.hpp"
namespace generated {
void init(sol::state& lua) {
sol::table preload = lua["package"]["preload"];
preload["ecs.Position"] = [&lua] {
sol::table component = lua.create_table();
component.new_usertype<Position>("Position",
"x", &Position::x, "y", &Position::y,
sol::base_classes, sol::bases<ecs::Component>()
);
component.set_function("new", sol::factories([](float _x, float _y) {
return std::make_pair(new Position(_x, _y), "Position");
}));
lua.set_function("_internal_to_Position", [] (ecs::Component* component) {
return (Position*)component;
});
return component;
};
preload["ecs.Velocity"] = [&lua] {
sol::table component = lua.create_table();
component.new_usertype<Velocity>("Velocity",
"x", &Velocity::x, "y", &Velocity::y,
sol::base_classes, sol::bases<ecs::Component>()
);
component.set_function("new", sol::factories([](float _x, float _y) {
return std::make_pair(new Velocity(_x, _y), "Velocity");
}));
lua.set_function("_internal_to_Velocity", [] (ecs::Component* component) {
return (Velocity*)component;
});
return component;
};
preload["ecs.Meta"] = [&lua] {
sol::table component = lua.create_table();
component.new_usertype<Meta>("Meta",
"name", &Meta::name,
sol::base_classes, sol::bases<ecs::Component>()
);
component.set_function("new", sol::factories([](std::string _name) {
return std::make_pair(new Meta(_name), "Meta");
}));
lua.set_function("_internal_to_Meta", [] (ecs::Component* component) {
return (Meta*)component;
});
return component;
};
}
}
#endif
#if __has_include("ecs-serial.h")
namespace generated {
void init() {
ecs::serial::register_component<Position>(
&Position::x, &Position::y
);
ecs::serial::register_component<Velocity>(
&Velocity::x, &Velocity::y
);
ecs::serial::register_component<Meta>(
&Meta::name
);
}
}
#endif

View File

@@ -1,5 +1,3 @@
#include "/home/tim/Projects/cpp/iohelper/iohelper/include/iohelper/read.h"
#include "/home/tim/Projects/cpp/iohelper/iohelper/include/iohelper/write.h"
#include "components.h"
#include "ecs-lua.h"
#include "ecs-serial.h"
@@ -60,29 +58,29 @@ int main(int argc, const char** argv) {
for (auto [a, b] : wrapper->table) {
size++;
}
iohelper::write_length(os, size);
io::write<size_t>(os, size);
for (auto [a, b] : wrapper->table) {
iohelper::write<std::string>(os, a.as<std::string>());
io::write<std::string>(os, a.as<std::string>());
sol::type type = b.get_type();
iohelper::write<uint8_t>(os, (uint8_t)type);
io::write<uint8_t>(os, (uint8_t)type);
switch (type) {
case sol::type::none:
case sol::type::nil:
break;
case sol::type::string:
iohelper::write<std::string>(os, b.as<std::string>());
io::write<std::string>(os, b.as<std::string>());
break;
case sol::type::number:
// @todo These should be doubles instead of floats
iohelper::write<float>(os, b.as<float>());
io::write<float>(os, b.as<float>());
break;
case sol::type::boolean:
iohelper::write<bool>(os, b.as<bool>());
io::write<bool>(os, b.as<bool>());
break;
case sol::type::table:
@@ -101,14 +99,14 @@ int main(int argc, const char** argv) {
// @todo Only do this if table is not created yet
wrapper->table = lua.create_table();
size_t size = iohelper::read_length(is);
size_t size = io::read<size_t>(is);
std::cout << "Size: " << size << '\n';
for (size_t i = 0; i < size; ++i) {
std::string name = iohelper::read<std::string>(is);
std::string name = io::read<std::string>(is);
std::cout << "Name: " << name << '\n';
sol::type type = (sol::type)iohelper::read<uint8_t>(is);
sol::type type = (sol::type)io::read<uint8_t>(is);
switch (type) {
case sol::type::none:
break;
@@ -117,16 +115,16 @@ int main(int argc, const char** argv) {
break;
case sol::type::string:
wrapper->table[name] = iohelper::read<std::string>(is);
wrapper->table[name] = io::read<std::string>(is);
break;
case sol::type::number:
wrapper->table[name] = iohelper::read<float>(is);
wrapper->table[name] = io::read<float>(is);
break;
case sol::type::boolean:
{
wrapper->table[name] = iohelper::read<bool>(is);
wrapper->table[name] = io::read<bool>(is);
break;
}
@@ -199,7 +197,7 @@ int main(int argc, const char** argv) {
std::ofstream file("entities", std::ios::out | std::ios::trunc);
ecs::serial::serialize_ids(file);
iohelper::write_length(file, manager.view<>().size());
io::write<size_t>(file, manager.view<>().size());
for (auto [uuid, entity] : manager.view<>()) {
ecs::serial::serialize(file, entity);
}
@@ -221,7 +219,7 @@ int main(int argc, const char** argv) {
ecs::serial::deserialize_ids(file);
size_t entity_count = iohelper::read_length(file);
size_t entity_count = io::read<size_t>(file);
size_t pos = file.tellg();
for (size_t i = 0; i < entity_count; ++i) {
ecs::serial::deserialize(file, manager);

View File

@@ -7,8 +7,8 @@ local Wrapper = require 'ecs.Wrapper'
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(Position.new(1.9, 9.7))
ent:add_component(Meta.new("Soldier"))
data = {