Added codegen to register components, and started reworking lua
This commit is contained in:
25
test/include/components.h
Normal file
25
test/include/components.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
#include "ecs.h"
|
||||
|
||||
struct Position : ecs::Component {
|
||||
Position(float _x, float _y) : x(_x), y(_y) {}
|
||||
Position() {}
|
||||
|
||||
float x = 0;
|
||||
float y = 0;
|
||||
};
|
||||
|
||||
struct Velocity : ecs::Component {
|
||||
Velocity(float _x, float _y) : x(_x), y(_y) {}
|
||||
Velocity() {}
|
||||
|
||||
float x = 0;
|
||||
float y = 0;
|
||||
};
|
||||
|
||||
struct Meta : ecs::Component {
|
||||
Meta(std::string _name) : name(_name) {}
|
||||
Meta() {}
|
||||
|
||||
std::string name;
|
||||
};
|
||||
@@ -1,24 +1,20 @@
|
||||
#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"
|
||||
|
||||
#include <bits/stdint-intn.h>
|
||||
#include <bits/stdint-uintn.h>
|
||||
#include <cstddef>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
struct Position : ecs::Component {
|
||||
Position(float _x, float _y) : x(_x), y(_y) {}
|
||||
Position() {}
|
||||
|
||||
float x;
|
||||
float y;
|
||||
};
|
||||
|
||||
struct Velocity : ecs::Component {
|
||||
Velocity(float _x, float _y) : x(_x), y(_y) {}
|
||||
Velocity() {}
|
||||
|
||||
float x;
|
||||
float y;
|
||||
};
|
||||
#include <ostream>
|
||||
#include <stdexcept>
|
||||
#include <sys/types.h>
|
||||
#include "ecs.h"
|
||||
#include "ecs_components.h"
|
||||
|
||||
int main() {
|
||||
sol::state lua;
|
||||
@@ -26,42 +22,99 @@ int main() {
|
||||
|
||||
ecs::lua::init(lua);
|
||||
|
||||
// @todo We should probably make this simpler to do...
|
||||
sol::table preload = lua["package"]["preload"];
|
||||
preload["ecs_test"] = [&lua] {
|
||||
sol::table test = lua.create_table();
|
||||
generated::init(lua);
|
||||
generated::init();
|
||||
|
||||
ecs::lua::register_component<Position, float, float>(lua, test,
|
||||
"x", &Position::x,
|
||||
"y", &Position::y
|
||||
);
|
||||
ecs::Manager manager;
|
||||
|
||||
ecs::lua::register_component<Velocity, float, float>(lua, test,
|
||||
"x", &Velocity::x,
|
||||
"y", &Velocity::y
|
||||
);
|
||||
ecs::serial::register_component_custom<ecs::lua::Wrapper>(
|
||||
[](std::ostream& os, ecs::Component* component) {
|
||||
ecs::lua::Wrapper* wrapper = (ecs::lua::Wrapper*)component;
|
||||
|
||||
return test;
|
||||
};
|
||||
iohelper::write<std::string>(os, wrapper->name);
|
||||
|
||||
ecs::serial::register_component<Position>(
|
||||
&Position::x,
|
||||
&Position::y
|
||||
);
|
||||
|
||||
ecs::serial::register_component<Velocity>(
|
||||
&Velocity::x,
|
||||
&Velocity::y
|
||||
);
|
||||
|
||||
ecs::serial::register_component<ecs::lua::LuaWrapper>(
|
||||
// @todo We need to create a read and write function for sol::object
|
||||
// &ecs::lua::LuaWrapper::object
|
||||
// #todo .size() does not work
|
||||
size_t size = 0;
|
||||
for (auto [a, b] : wrapper->table) {
|
||||
size++;
|
||||
}
|
||||
iohelper::write_length(os, size);
|
||||
|
||||
for (auto [a, b] : wrapper->table) {
|
||||
iohelper::write<std::string>(os, a.as<std::string>());
|
||||
|
||||
sol::type type = b.get_type();
|
||||
iohelper::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>());
|
||||
break;
|
||||
|
||||
case sol::type::number:
|
||||
iohelper::write<int32_t>(os, b.as<int32_t>());
|
||||
break;
|
||||
|
||||
case sol::type::boolean:
|
||||
iohelper::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;
|
||||
}
|
||||
}
|
||||
},
|
||||
[](std::istream& is, ecs::Component* component) {
|
||||
ecs::lua::Wrapper* wrapper = (ecs::lua::Wrapper*)component;
|
||||
|
||||
wrapper->name = iohelper::read<std::string>(is);
|
||||
std::cout << wrapper->name << '\n';
|
||||
|
||||
size_t size = iohelper::read_length(is);
|
||||
std::cout << "Size: " << size << '\n';
|
||||
for (size_t i = 0; i < size; ++i) {
|
||||
std::string name = iohelper::read<std::string>(is);
|
||||
|
||||
std::cout << "Name: " << name << '\n';
|
||||
|
||||
sol::type type = (sol::type)iohelper::read<uint8_t>(is);
|
||||
switch (type) {
|
||||
case sol::type::none:
|
||||
break;
|
||||
|
||||
case sol::type::nil:
|
||||
break;
|
||||
|
||||
case sol::type::string:
|
||||
std::cout << "Value: " << iohelper::read<std::string>(is) << '\n';
|
||||
break;
|
||||
|
||||
case sol::type::number:
|
||||
std::cout << "Value: " << std::dec << iohelper::read<int32_t>(is) << '\n';
|
||||
break;
|
||||
|
||||
case sol::type::boolean:
|
||||
std::cout << "Value: " << std::dec << iohelper::read<bool>(is) << '\n';
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// Testing interop with lua
|
||||
{
|
||||
// This manages all our entities
|
||||
ecs::Manager manager;
|
||||
|
||||
lua.set_function("get_manager", [&manager] () -> ecs::Manager& {
|
||||
return manager;
|
||||
@@ -69,23 +122,6 @@ int main() {
|
||||
|
||||
lua.script_file("test.lua");
|
||||
|
||||
// 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";
|
||||
// }
|
||||
// }
|
||||
|
||||
std::cout << "Update position\n";
|
||||
manager.view<Position, Velocity>().for_each([](ecs::Entity*, Position* pos, Velocity* vel) {
|
||||
pos->x += vel->x;
|
||||
@@ -100,22 +136,22 @@ int main() {
|
||||
|
||||
// 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;
|
||||
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';
|
||||
};
|
||||
|
||||
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;
|
||||
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";
|
||||
ecs::Manager manager;
|
||||
|
||||
// Create entities and store them
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
@@ -145,26 +181,31 @@ int main() {
|
||||
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);
|
||||
}
|
||||
|
||||
{
|
||||
std::cout << "LOAD\n";
|
||||
ecs::Manager manager;
|
||||
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, manager);
|
||||
ecs::serial::deserialize(file, manager2);
|
||||
}
|
||||
|
||||
file.seekg(0, std::ios::beg);
|
||||
iohelper::read_length(file);
|
||||
ecs::serial::deserialize(file, manager);
|
||||
ecs::serial::deserialize(file, manager2);
|
||||
|
||||
if (false) {
|
||||
auto ent = manager.get_entity(uuids::uuid::from_string("6d58fdb5-6d8c-4e6f-89d4-f7d7b184f463").value());
|
||||
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;
|
||||
@@ -172,7 +213,7 @@ int main() {
|
||||
}
|
||||
}
|
||||
|
||||
manager.view<Position>().for_each([] (ecs::Entity* entity, Position* pos) {
|
||||
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';
|
||||
|
||||
@@ -1,18 +1,29 @@
|
||||
local ecs = require "ecs"
|
||||
local test = require "ecs_test"
|
||||
-- local test = require "ecs_test"
|
||||
local Position = require "ecs.Position"
|
||||
local Velocity = require "ecs.Velocity"
|
||||
local Meta = require "ecs.Meta"
|
||||
|
||||
manager = get_manager()
|
||||
ent = manager:create_entity()
|
||||
ent:add_component("Position", test.Position.new(1.9, 9.7))
|
||||
ent:add_component("Velocity", test.Velocity.new(0.2, 0.3))
|
||||
ent:add_component(Position.new(1.9, 9.7))
|
||||
ent:add_component(Velocity.new(0.2, 0.3))
|
||||
ent:add_component(Meta.new("Soldier"))
|
||||
|
||||
random = {
|
||||
a = 10,
|
||||
b = 11,
|
||||
c = function(s)
|
||||
print("Hello " .. s .. "!")
|
||||
end
|
||||
speed = 10,
|
||||
something = "Hello, World!",
|
||||
alive = true
|
||||
}
|
||||
ent:add_component("Random", ecs.LuaWrapper.new(random))
|
||||
ent:add_component(ecs.Wrapper.new("Random", random))
|
||||
|
||||
-- @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")
|
||||
@@ -23,8 +34,7 @@ print("v_x: " .. vel.x)
|
||||
print("v_y: " .. vel.y)
|
||||
|
||||
print("View test")
|
||||
view = manager:view("Position", "Velocity")
|
||||
view:for_each(function(ent)
|
||||
manager:view("Position", "Velocity"):for_each(function(ent)
|
||||
pos = ent:get_component("Position")
|
||||
vel = ent:get_component("Velocity")
|
||||
|
||||
@@ -38,6 +48,11 @@ view:for_each(function(ent)
|
||||
|
||||
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)
|
||||
|
||||
-- @todo Implement this
|
||||
@@ -45,16 +60,20 @@ end)
|
||||
-- print "TEST"
|
||||
-- end
|
||||
|
||||
manager:view("Random"):for_each(function(ent)
|
||||
wrapped = ent:get_component("Random").object
|
||||
manager:view("Wrapper"):for_each(function(ent)
|
||||
wrapped = ent:get_component("Wrapper").table
|
||||
|
||||
print(wrapped.a)
|
||||
wrapped.a = 11
|
||||
print(wrapped.a)
|
||||
print(random.a)
|
||||
random.a = 20
|
||||
print(wrapped.a)
|
||||
print(random.a)
|
||||
print(wrapped.speed)
|
||||
wrapped.speed = 11
|
||||
print(wrapped.speed)
|
||||
print(random.speed)
|
||||
random.speed = 20
|
||||
print(wrapped.speed)
|
||||
print(random.speed)
|
||||
|
||||
random.c("you")
|
||||
print(wrapped.alive)
|
||||
wrapped.alive = false
|
||||
print(wrapped.alive)
|
||||
|
||||
print(wrapped.something)
|
||||
end)
|
||||
|
||||
Reference in New Issue
Block a user