Serializer now has a nice register function
This commit is contained in:
@@ -1,19 +1,23 @@
|
||||
#include "ecs-lua.h"
|
||||
#include "ecs-serial.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
struct Position : ecs::Component {
|
||||
Position(float x, float y) : _x(x), _y(y) {}
|
||||
Position(float _x, float _y) : x(_x), y(_y) {}
|
||||
Position() {}
|
||||
|
||||
float _x;
|
||||
float _y;
|
||||
float x;
|
||||
float y;
|
||||
};
|
||||
|
||||
struct Velocity : ecs::Component{
|
||||
Velocity(float vx, float vy) : _vx(vx), _vy(vy) {}
|
||||
struct Velocity : ecs::Component {
|
||||
Velocity(float _x, float _y) : x(_x), y(_y) {}
|
||||
Velocity() {}
|
||||
|
||||
float _vx;
|
||||
float _vy;
|
||||
float x;
|
||||
float y;
|
||||
};
|
||||
|
||||
int main() {
|
||||
@@ -23,122 +27,205 @@ int main() {
|
||||
ecs::lua::init(lua);
|
||||
|
||||
ecs::lua::register_component<Position, float, float>(lua,
|
||||
"x", &Position::_x,
|
||||
"y", &Position::_y
|
||||
"x", &Position::x,
|
||||
"y", &Position::y
|
||||
);
|
||||
ecs::serial::register_component<Position>(
|
||||
&Position::x,
|
||||
&Position::y
|
||||
);
|
||||
|
||||
ecs::lua::register_component<Velocity, float, float>(lua,
|
||||
"vx", &Velocity::_vx,
|
||||
"vy", &Velocity::_vy
|
||||
"x", &Velocity::x,
|
||||
"y", &Velocity::y
|
||||
);
|
||||
ecs::serial::register_component<Velocity>(
|
||||
&Velocity::x,
|
||||
&Velocity::y
|
||||
);
|
||||
|
||||
// This manages all our entities
|
||||
ecs::Manager manager;
|
||||
ecs::serial::register_component<ecs::lua::LuaWrapper>(
|
||||
// @todo We need to create a read and write function for sol::object
|
||||
// &ecs::lua::LuaWrapper::object
|
||||
);
|
||||
|
||||
lua.set_function("get_manager", [&manager] () -> ecs::Manager& {
|
||||
return manager;
|
||||
});
|
||||
{
|
||||
// This manages all our entities
|
||||
ecs::Manager manager;
|
||||
|
||||
lua.script(R"lua(
|
||||
manager = get_manager()
|
||||
ent = manager:create_entity()
|
||||
-- In the future we will be able to also write components in lua
|
||||
-- @todo Figure out how get_component will work in this case
|
||||
ent:add_component("Position", Position.new(1.9, 9.7))
|
||||
ent:add_component("Velocity", Velocity.new(0.2, 0.3))
|
||||
random = {
|
||||
a = 10,
|
||||
b = 11,
|
||||
c = function(s)
|
||||
print("Hello " .. s .. "!")
|
||||
end
|
||||
}
|
||||
ent:add_component("Random", LuaWrapper.new(random))
|
||||
lua.set_function("get_manager", [&manager] () -> ecs::Manager& {
|
||||
return manager;
|
||||
});
|
||||
|
||||
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.vx)
|
||||
print("v_y: " .. vel.vy)
|
||||
lua.script(R"lua(
|
||||
manager = get_manager()
|
||||
ent = manager:create_entity()
|
||||
-- In the future we will be able to also write components in lua
|
||||
-- @todo Figure out how get_component will work in this case
|
||||
ent:add_component("Position", Position.new(1.9, 9.7))
|
||||
ent:add_component("Velocity", Velocity.new(0.2, 0.3))
|
||||
random = {
|
||||
a = 10,
|
||||
b = 11,
|
||||
c = function(s)
|
||||
print("Hello " .. s .. "!")
|
||||
end
|
||||
}
|
||||
ent:add_component("Random", LuaWrapper.new(random))
|
||||
|
||||
print("View test")
|
||||
view = manager:view("Position", "Velocity")
|
||||
view:for_each(function(ent)
|
||||
print(ent:has_components("Position", "Velocity"))
|
||||
pos = ent:get_component("Position")
|
||||
vel = ent:get_component("Velocity")
|
||||
|
||||
pos.x = pos.x + vel.vx
|
||||
pos.y = pos.y + vel.vy
|
||||
|
||||
print("x: " .. pos.x)
|
||||
print("y: " .. pos.y)
|
||||
vel = ent:get_component("Velocity")
|
||||
print("v_x: " .. vel.x)
|
||||
print("v_y: " .. vel.y)
|
||||
|
||||
print("v_x: " .. vel.vx)
|
||||
print("v_y: " .. vel.vy)
|
||||
end)
|
||||
print("View test")
|
||||
view = manager:view("Position", "Velocity")
|
||||
view: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("Random"):for_each(function(ent)
|
||||
wrapped = ent:get_component("Random").object
|
||||
print(ent)
|
||||
|
||||
print(wrapped.a)
|
||||
wrapped.a = 11
|
||||
print(wrapped.a)
|
||||
print(random.a)
|
||||
random.a = 20
|
||||
print(wrapped.a)
|
||||
print(random.a)
|
||||
print("x: " .. pos.x)
|
||||
print("y: " .. pos.y)
|
||||
|
||||
random.c("you")
|
||||
end)
|
||||
)lua");
|
||||
print("v_x: " .. vel.x)
|
||||
print("v_y: " .. vel.y)
|
||||
end)
|
||||
|
||||
// for (int i = 0; i < 10; ++i) {
|
||||
// // We can create entities
|
||||
// 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);
|
||||
// }
|
||||
//
|
||||
// // We can check for components by name
|
||||
// if (entity->has_components<Velocity>()) {
|
||||
// std::cout << "YES\n";
|
||||
// } else {
|
||||
// std::cout << "NO\n";
|
||||
// }
|
||||
// }
|
||||
-- @todo Implement this
|
||||
-- for uuid,entity in pairs(view) do
|
||||
-- print "TEST"
|
||||
-- end
|
||||
|
||||
std::cout << "Update position\n";
|
||||
manager.view<Position, Velocity>().for_each([](ecs::Entity*, Position* pos, Velocity* vel) {
|
||||
pos->_x += vel->_vx;
|
||||
pos->_y += vel->_vy;
|
||||
});
|
||||
manager:view("Random"):for_each(function(ent)
|
||||
wrapped = ent:get_component("Random").object
|
||||
|
||||
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';
|
||||
});
|
||||
print(wrapped.a)
|
||||
wrapped.a = 11
|
||||
print(wrapped.a)
|
||||
print(random.a)
|
||||
random.a = 20
|
||||
print(wrapped.a)
|
||||
print(random.a)
|
||||
|
||||
// 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::LuaWrapper*)entity->get_component(ecs::ComponentID::get_id({"Random"})[0]))->_object;
|
||||
random.c("you")
|
||||
end)
|
||||
)lua");
|
||||
|
||||
random["a"] = 21;
|
||||
std::cout << random["a"].get<std::string>() << '\n';
|
||||
};
|
||||
// for (int i = 0; i < 10; ++i) {
|
||||
// // We can create entities
|
||||
// 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);
|
||||
// }
|
||||
//
|
||||
// // We can check for components by name
|
||||
// if (entity->has_components<Velocity>()) {
|
||||
// std::cout << "YES\n";
|
||||
// } else {
|
||||
// std::cout << "NO\n";
|
||||
// }
|
||||
// }
|
||||
|
||||
for (auto [uuid, entity] : manager.view(ecs::ComponentID::get_id({"Random"}))) {
|
||||
sol::table random = ((ecs::lua::LuaWrapper*)entity->get_component(ecs::ComponentID::get_id({"Random"})[0]))->_object;
|
||||
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;
|
||||
});
|
||||
|
||||
std::cout << random["a"].get<std::string>() << '\n';
|
||||
};
|
||||
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';
|
||||
});
|
||||
|
||||
// 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::LuaWrapper*)entity->get_component(ecs::ComponentID::get_id({"Random"})[0]))->object;
|
||||
|
||||
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::LuaWrapper*)entity->get_component(ecs::ComponentID::get_id({"Random"})[0]))->object;
|
||||
|
||||
std::cout << random["a"].get<std::string>() << '\n';
|
||||
};
|
||||
}
|
||||
|
||||
{
|
||||
std::cout << "STORE\n";
|
||||
ecs::Manager manager;
|
||||
|
||||
// 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';
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
std::cout << "LOAD\n";
|
||||
ecs::Manager manager;
|
||||
|
||||
// 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, manager);
|
||||
}
|
||||
|
||||
file.seekg(0, std::ios::beg);
|
||||
iohelper::read_length(file);
|
||||
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';
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user