We can properly use range based for loops with view and interaction with Lua is significantly improved
This commit is contained in:
@@ -14,6 +14,14 @@
|
||||
#include "ecs.h"
|
||||
#include "ecs_components.h"
|
||||
|
||||
class Orb : public ecs::Entity {
|
||||
public:
|
||||
Orb(uuids::uuid uuid, float x, float y) : Entity(uuid) {
|
||||
auto position = add_component<Position>(x, y);
|
||||
auto velocity = add_component<Velocity>(0, 0);
|
||||
}
|
||||
};
|
||||
|
||||
inline void handle_error(sol::optional<std::string> maybe_msg) {
|
||||
std::cerr << "Lua error, aborting!\n";
|
||||
if (maybe_msg) {
|
||||
@@ -38,10 +46,9 @@ int main(int argc, const char** argv) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
ecs::ComponentID::regist<ecs::lua::Wrapper>("Wrapper");
|
||||
ecs::ComponentID::regist<Position>("Position");
|
||||
ecs::ComponentID::regist<Velocity>("Velocity");
|
||||
ecs::ComponentID::regist<Meta>("Meta");
|
||||
ecs::ComponentID::add<Position>("Position");
|
||||
ecs::ComponentID::add<Velocity>("Velocity");
|
||||
ecs::ComponentID::add<Meta>("Meta");
|
||||
|
||||
sol::state lua(sol::c_call<decltype(&handle_error), &handle_error>);
|
||||
lua.open_libraries(sol::lib::base, sol::lib::package);
|
||||
@@ -83,19 +90,12 @@ int main(int argc, const char** argv) {
|
||||
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::resolve("Random"))) {
|
||||
sol::table random = ((ecs::lua::Wrapper*)entity->get_component(ecs::ComponentID::resolve("Random")))->table;
|
||||
|
||||
random["a"] = 21;
|
||||
std::cout << random["a"].get<std::string>() << '\n';
|
||||
};
|
||||
|
||||
for (auto [uuid, entity] : manager.view(ecs::ComponentID::resolve("Random"))) {
|
||||
sol::table random = ((ecs::lua::Wrapper*)entity->get_component(ecs::ComponentID::resolve("Random")))->table;
|
||||
|
||||
std::cout << random["a"].get<std::string>() << '\n';
|
||||
};
|
||||
std::cout << "Adding Orb!\n";
|
||||
manager.create_entity<Orb>(1.2f, 3.4f);
|
||||
for (auto [ent, pos, vel] : manager.view<Position, Velocity>()) {
|
||||
std::cout << "X: " << pos->x << " + " << vel->x << '\n';
|
||||
std::cout << "Y: " << pos->y << " + " << vel->y << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
// Test serialization
|
||||
@@ -122,7 +122,7 @@ int main(int argc, const char** argv) {
|
||||
ecs::serial::serialize_ids(file);
|
||||
|
||||
io::write<size_t>(file, manager.view<>().size());
|
||||
for (auto [uuid, entity] : manager.view<>()) {
|
||||
for (auto [entity] : manager.view<>()) {
|
||||
ecs::serial::serialize(file, entity);
|
||||
}
|
||||
file.close();
|
||||
|
||||
Reference in New Issue
Block a user