Created basic ecs that has an optional lua module

This commit is contained in:
2019-01-30 00:23:32 +01:00
commit 49c1cacaa0
15 changed files with 934 additions and 0 deletions

138
test/src/main.cpp Normal file
View File

@@ -0,0 +1,138 @@
#include "ecs-lua.h"
#include <iostream>
struct Position : ecs::Component {
Position(float x, float y) : _x(x), _y(y) {}
float _x;
float _y;
};
struct Velocity : ecs::Component{
Velocity(float vx, float vy) : _vx(vx), _vy(vy) {}
float _vx;
float _vy;
};
int main() {
sol::state lua;
lua.open_libraries(sol::lib::base);
ecs::lua::init(lua);
ecs::lua::register_component<Position, float, float>(lua,
"x", &Position::_x,
"y", &Position::_y
);
ecs::lua::register_component<Velocity, float, float>(lua,
"vx", &Velocity::_vx,
"vy", &Velocity::_vy
);
// This manages all our entities
ecs::Manager manager;
lua.set_function("get_manager", [&manager] () -> ecs::Manager& {
return 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))
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)
print("View test")
view = manager:view("Position", "Velocity")
view:for_each(function(ent)
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)
print("v_x: " .. vel.vx)
print("v_y: " .. vel.vy)
end)
manager:view("Random"):for_each(function(ent)
wrapped = ent:get_component("Random").object
print(wrapped.a)
wrapped.a = 11
print(wrapped.a)
print(random.a)
random.a = 20
print(wrapped.a)
print(random.a)
random.c("you")
end)
)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->_vx;
pos->_y += vel->_vy;
});
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';
});
manager.view({"Random"}).for_each([](ecs::Entity* entity) {
sol::table random = ((ecs::lua::LuaWrapper*)entity->get_component("Random"))->_object;
random["a"] = 21;
std::cout << random["a"].get<std::string>() << '\n';
});
manager.view({"Random"}).for_each([](ecs::Entity* entity) {
sol::table random = ((ecs::lua::LuaWrapper*)entity->get_component("Random"))->_object;
std::cout << random["a"].get<std::string>() << '\n';
});
}