We can properly use range based for loops with view and interaction with Lua is significantly improved
This commit is contained in:
parent
de5af0a5aa
commit
c9f05af6c9
|
@ -7,9 +7,9 @@
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
namespace ecs::lua {
|
namespace ecs::lua {
|
||||||
struct Wrapper : Component {
|
struct LuaComponent : Component {
|
||||||
// @todo Figure out a more elegant way
|
// @todo Figure out a more elegant way
|
||||||
Wrapper() {}
|
LuaComponent() {}
|
||||||
|
|
||||||
sol::table table;
|
sol::table table;
|
||||||
};
|
};
|
||||||
|
|
|
@ -18,8 +18,80 @@ namespace ecs::lua {
|
||||||
preload["ecs"] = [&lua] {
|
preload["ecs"] = [&lua] {
|
||||||
sol::table ecs = lua.create_table();
|
sol::table ecs = lua.create_table();
|
||||||
|
|
||||||
ecs.set_function("register", [&lua] (std::string name, sol::table table) {
|
ecs.new_usertype<Entity>("Entity",
|
||||||
size_t id = ComponentID::regist(name);
|
"__tostring", [] (Entity* thiz) {
|
||||||
|
return uuids::to_string(thiz->uuid);
|
||||||
|
},
|
||||||
|
"add_component", [] (Entity* thiz, size_t id, Component* component) {
|
||||||
|
return thiz->add_component(id, component);
|
||||||
|
},
|
||||||
|
"has_components", [] (Entity* thiz, sol::variadic_args args) {
|
||||||
|
std::vector<size_t> ids;
|
||||||
|
for (sol::table table : args) {
|
||||||
|
// @todo Check that the table has id and convert()
|
||||||
|
ids.push_back(table["_id"]);
|
||||||
|
}
|
||||||
|
return thiz->has_components(ids);
|
||||||
|
},
|
||||||
|
"get_component", [&lua] (Entity* thiz, sol::table table) -> sol::object {
|
||||||
|
// @todo Check that the table has id and convert()
|
||||||
|
|
||||||
|
Component* component = thiz->get_component(table["_id"]);
|
||||||
|
auto f1 = table["_convert"];
|
||||||
|
if (f1.valid()) {
|
||||||
|
return f1(component);
|
||||||
|
}
|
||||||
|
throw std::runtime_error("Unknown component");
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
ecs.new_usertype<View<>>("View",
|
||||||
|
"for_each", &View<>::for_each
|
||||||
|
);
|
||||||
|
|
||||||
|
ecs.new_usertype<Manager>("Manager",
|
||||||
|
"create_entity", [] (Manager* thiz) {
|
||||||
|
// @todo Allow to construct with given uuid
|
||||||
|
return thiz->create_entity();
|
||||||
|
},
|
||||||
|
"remove_entity", &Manager::remove_entity,
|
||||||
|
"has_entity", [] (Manager* thiz, std::string uuid) {
|
||||||
|
// @todo Check if valid
|
||||||
|
return thiz->has_entity(uuids::uuid::from_string(uuid).value());
|
||||||
|
},
|
||||||
|
"get_entity", [] (Manager* thiz, std::string uuid) {
|
||||||
|
// @todo Check if valid
|
||||||
|
return thiz->get_entity(uuids::uuid::from_string(uuid).value());
|
||||||
|
},
|
||||||
|
"view", [] (Manager* thiz, sol::variadic_args args) {
|
||||||
|
std::vector<size_t> ids;
|
||||||
|
for (sol::table table : args) {
|
||||||
|
// @todo Check that the table has id and convert()
|
||||||
|
ids.push_back(table["_id"]);
|
||||||
|
}
|
||||||
|
return thiz->view(ids);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return ecs;
|
||||||
|
};
|
||||||
|
|
||||||
|
preload["ecs.LuaComponent"] = [&lua] {
|
||||||
|
lua.new_usertype<LuaComponent>("LuaComponent",
|
||||||
|
"__index", [] (LuaComponent* thiz, std::string key) {
|
||||||
|
return thiz->table[key];
|
||||||
|
},
|
||||||
|
"__newindex", [] (LuaComponent* thiz, std::string key, sol::object value) {
|
||||||
|
thiz->table[key] = value;
|
||||||
|
},
|
||||||
|
sol::base_classes, sol::bases<ecs::Component>()
|
||||||
|
);
|
||||||
|
|
||||||
|
sol::table comp = lua.create_table();
|
||||||
|
comp.set_function("create", [&lua] (std::string name, sol::table table) {
|
||||||
|
std::cout << "Creating new component: " << name << '\n';
|
||||||
|
size_t id = ComponentID::add(name);
|
||||||
|
std::cout << "ID: " << id << '\n';
|
||||||
|
|
||||||
std::unordered_map<std::string, sol::type> map;
|
std::unordered_map<std::string, sol::type> map;
|
||||||
|
|
||||||
|
@ -38,8 +110,7 @@ namespace ecs::lua {
|
||||||
|
|
||||||
#ifdef _OPTIONAL_ECS_SERIAL
|
#ifdef _OPTIONAL_ECS_SERIAL
|
||||||
auto serialize = [map, name] (std::ostream& os, ecs::Component* component) {
|
auto serialize = [map, name] (std::ostream& os, ecs::Component* component) {
|
||||||
std::cout << "SERIALIZE: " << name << '\n';
|
LuaComponent* wrapper = (LuaComponent*)component;
|
||||||
Wrapper* wrapper = (Wrapper*)component;
|
|
||||||
|
|
||||||
// Write each of the entries
|
// Write each of the entries
|
||||||
io::write<size_t>(os, map.size());
|
io::write<size_t>(os, map.size());
|
||||||
|
@ -65,9 +136,7 @@ namespace ecs::lua {
|
||||||
};
|
};
|
||||||
|
|
||||||
auto deserialize = [map] (std::istream& is, Component* component) {
|
auto deserialize = [map] (std::istream& is, Component* component) {
|
||||||
std::cout << "DESERIALIZE\n";
|
LuaComponent* wrapper = (LuaComponent*)component;
|
||||||
|
|
||||||
Wrapper* wrapper = (Wrapper*)component;
|
|
||||||
|
|
||||||
// Write each of the entries
|
// Write each of the entries
|
||||||
size_t size = io::read<size_t>(is);
|
size_t size = io::read<size_t>(is);
|
||||||
|
@ -80,17 +149,14 @@ namespace ecs::lua {
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case sol::type::string:
|
case sol::type::string:
|
||||||
std::cout << "STRING\n";
|
|
||||||
wrapper->table[key] = io::read<std::string>(is);
|
wrapper->table[key] = io::read<std::string>(is);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case sol::type::number:
|
case sol::type::number:
|
||||||
std::cout << "NUMBER\n";
|
|
||||||
wrapper->table[key] = io::read<float>(is);
|
wrapper->table[key] = io::read<float>(is);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case sol::type::boolean:
|
case sol::type::boolean:
|
||||||
std::cout << "BOOL\n";
|
|
||||||
wrapper->table[key] = io::read<bool>(is);
|
wrapper->table[key] = io::read<bool>(is);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -101,8 +167,7 @@ namespace ecs::lua {
|
||||||
};
|
};
|
||||||
|
|
||||||
auto create = [&lua, map] {
|
auto create = [&lua, map] {
|
||||||
std::cout << "CREATE\n";
|
LuaComponent* wrapper = new LuaComponent();
|
||||||
Wrapper* wrapper = new Wrapper();
|
|
||||||
wrapper->table = lua.create_table();
|
wrapper->table = lua.create_table();
|
||||||
|
|
||||||
for (auto [key, type] : map) {
|
for (auto [key, type] : map) {
|
||||||
|
@ -130,14 +195,11 @@ namespace ecs::lua {
|
||||||
ecs::serial::register_component_custom(id, serialize, deserialize, create);
|
ecs::serial::register_component_custom(id, serialize, deserialize, create);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
lua.set_function("_internal_to_" + name, [] (ecs::Component* component) {
|
|
||||||
return (Wrapper*)component;
|
|
||||||
});
|
|
||||||
|
|
||||||
sol::table component = lua.create_table();
|
sol::table component = lua.create_table();
|
||||||
|
component["_id"] = id;
|
||||||
|
|
||||||
component.set_function("new", sol::factories([&lua, id, map] (sol::table table) {
|
component.set_function("new", sol::factories([&lua, id, map] (sol::table table) {
|
||||||
Wrapper* wrapper = new Wrapper();
|
LuaComponent* wrapper = new LuaComponent();
|
||||||
wrapper->table = lua.create_table();
|
wrapper->table = lua.create_table();
|
||||||
|
|
||||||
for (auto [key, type] : map) {
|
for (auto [key, type] : map) {
|
||||||
|
@ -147,75 +209,14 @@ namespace ecs::lua {
|
||||||
return std::make_pair(id, wrapper);
|
return std::make_pair(id, wrapper);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
component.set_function("_convert", [] (ecs::Component* component) {
|
||||||
|
return (LuaComponent*)component;
|
||||||
|
});
|
||||||
|
|
||||||
return component;
|
return component;
|
||||||
});
|
});
|
||||||
|
|
||||||
ecs.new_usertype<Entity>("Entity",
|
return comp;
|
||||||
"__tostring", [] (Entity* thiz) {
|
|
||||||
return uuids::to_string(thiz->uuid);
|
|
||||||
},
|
|
||||||
"add_component", [] (Entity* thiz, size_t id, Component* component) {
|
|
||||||
return thiz->add_component(id, component);
|
|
||||||
},
|
|
||||||
"has_components", [] (Entity* thiz, sol::variadic_args args) {
|
|
||||||
std::vector<std::string> names;
|
|
||||||
for (std::string name : args) {
|
|
||||||
names.push_back(name);
|
|
||||||
}
|
|
||||||
return thiz->has_components(ComponentID::resolve(names));
|
|
||||||
},
|
|
||||||
"get_component", [&lua] (Entity* thiz, std::string name) -> sol::object {
|
|
||||||
// Convert to the correct component type
|
|
||||||
Component* component = thiz->get_component(ComponentID::resolve(name));
|
|
||||||
// @todo Figure out a more elegant way to convert
|
|
||||||
auto f1 = lua["_internal_to_" + name];
|
|
||||||
if (f1.valid()) {
|
|
||||||
return f1(component);
|
|
||||||
}
|
|
||||||
throw std::runtime_error("Unknown component");
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
ecs.new_usertype<View<>>("View",
|
|
||||||
"for_each", &View<>::for_each
|
|
||||||
);
|
|
||||||
|
|
||||||
ecs.new_usertype<Manager>("Manager",
|
|
||||||
"create_entity", [] (Manager* thiz) {
|
|
||||||
// @todo Allow to construct with given uuid
|
|
||||||
return thiz->create_entity();
|
|
||||||
},
|
|
||||||
"remove_entity", &Manager::remove_entity,
|
|
||||||
"has_entity", [] (Manager* thiz, std::string uuid) {
|
|
||||||
// @todo Check if valid
|
|
||||||
return thiz->has_entity(uuids::uuid::from_string(uuid).value());
|
|
||||||
},
|
|
||||||
"get_entity", [] (Manager* thiz, std::string uuid) {
|
|
||||||
// @todo Check if valid
|
|
||||||
return thiz->get_entity(uuids::uuid::from_string(uuid).value());
|
|
||||||
},
|
|
||||||
"view", [] (Manager* thiz, sol::variadic_args args) {
|
|
||||||
std::vector<std::string> names;
|
|
||||||
for (std::string name : args) {
|
|
||||||
names.push_back(name);
|
|
||||||
}
|
|
||||||
return thiz->view(ComponentID::resolve(names));
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
return ecs;
|
|
||||||
};
|
|
||||||
|
|
||||||
preload["ecs.Wrapper"] = [&lua] {
|
|
||||||
lua.new_usertype<Wrapper>("Wrapper",
|
|
||||||
"__index", [] (Wrapper* thiz, std::string key) {
|
|
||||||
return thiz->table[key];
|
|
||||||
},
|
|
||||||
"__newindex", [] (Wrapper* thiz, std::string key, sol::object value) {
|
|
||||||
thiz->table[key] = value;
|
|
||||||
},
|
|
||||||
sol::base_classes, sol::bases<ecs::Component>()
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,13 +20,13 @@ namespace ecs {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
template <typename T>
|
template <typename T>
|
||||||
static size_t regist(std::string name) {
|
static size_t add(std::string name) {
|
||||||
_ids.insert({name, id<T>});
|
_ids.insert({name, id<T>});
|
||||||
// @todo Do some other register stuff
|
// @todo Do some other register stuff
|
||||||
return id<T>;
|
return id<T>;
|
||||||
}
|
}
|
||||||
|
|
||||||
static size_t regist(std::string name) {
|
static size_t add(std::string name) {
|
||||||
size_t id = next_id++;
|
size_t id = next_id++;
|
||||||
_ids.insert({name, id});
|
_ids.insert({name, id});
|
||||||
return id;
|
return id;
|
||||||
|
@ -84,23 +84,16 @@ namespace ecs {
|
||||||
Entity(uuids::uuid _uuid) : uuid(_uuid) {}
|
Entity(uuids::uuid _uuid) : uuid(_uuid) {}
|
||||||
~Entity();
|
~Entity();
|
||||||
|
|
||||||
void add_component(size_t id, Component* component);
|
Component* add_component(size_t id, Component* component);
|
||||||
bool has_components(std::vector<size_t> ids) const;
|
bool has_components(std::vector<size_t> ids) const;
|
||||||
bool has_components(size_t id) const;
|
|
||||||
Component* get_component(size_t id) const;
|
Component* get_component(size_t id) const;
|
||||||
|
|
||||||
template <typename T, typename... Args>
|
template <typename T, typename... Args>
|
||||||
void add_component(Args... args) {
|
T* add_component(Args... args) {
|
||||||
size_t id = ComponentID::id<T>;
|
size_t id = ComponentID::id<T>;
|
||||||
|
T* component = new T(args...);
|
||||||
|
|
||||||
std::cout << "ID: " << id << '\n';
|
return (T*)add_component(id, component);
|
||||||
|
|
||||||
if (_components.find(id) != _components.end()) {
|
|
||||||
throw std::runtime_error("Component already exists");
|
|
||||||
}
|
|
||||||
|
|
||||||
// @todo Also register the component to the manager
|
|
||||||
_components[id] = new T(args...);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename... Ts>
|
template <typename... Ts>
|
||||||
|
@ -140,34 +133,106 @@ namespace ecs {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class ViewIterator;
|
||||||
|
|
||||||
size_t size() const {
|
size_t size() const {
|
||||||
return _entities.size();
|
return _entities.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
auto begin() {
|
auto begin() {
|
||||||
return _entities.begin();
|
return ViewIterator(_entities.begin());
|
||||||
}
|
}
|
||||||
|
|
||||||
auto begin() const {
|
auto begin() const {
|
||||||
return _entities.begin();
|
return ConstViewIterator(_entities.begin());
|
||||||
}
|
}
|
||||||
|
|
||||||
auto cbegin() const {
|
auto cbegin() const {
|
||||||
return _entities.cbegin();
|
return ConstViewIterator(_entities.cbegin());
|
||||||
}
|
}
|
||||||
|
|
||||||
auto end() {
|
auto end() {
|
||||||
return _entities.end();
|
return ViewIterator(_entities.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
auto end() const {
|
auto end() const {
|
||||||
return _entities.end();
|
return ConstViewIterator(_entities.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
auto cend() const {
|
auto cend() const {
|
||||||
return _entities.cend();
|
return ConstViewIterator(_entities.cend());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class ViewIterator {
|
||||||
|
public:
|
||||||
|
using Iiterator = std::unordered_map<uuids::uuid, Entity*>::iterator;
|
||||||
|
|
||||||
|
ViewIterator(Iiterator it) : it(it) {}
|
||||||
|
|
||||||
|
ViewIterator& operator=(Iiterator it) {
|
||||||
|
this->it = it;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
ViewIterator& operator++() {
|
||||||
|
it++;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
ViewIterator operator++(int) {
|
||||||
|
ViewIterator iterator = *this;
|
||||||
|
++*this;
|
||||||
|
return iterator;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator!=(const ViewIterator& other) const {
|
||||||
|
return it != other.it;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto operator*() {
|
||||||
|
return std::make_tuple(it->second, it->second->get_component<Ts>()...);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
Iiterator it;
|
||||||
|
};
|
||||||
|
|
||||||
|
class ConstViewIterator {
|
||||||
|
public:
|
||||||
|
using Iiterator = std::unordered_map<uuids::uuid, Entity*>::const_iterator;
|
||||||
|
|
||||||
|
ConstViewIterator(Iiterator it) : it(it) {}
|
||||||
|
|
||||||
|
ConstViewIterator& operator=(Iiterator it) {
|
||||||
|
this->it = it;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
ConstViewIterator& operator++() {
|
||||||
|
it++;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
ConstViewIterator operator++(int) {
|
||||||
|
ViewIterator iterator = *this;
|
||||||
|
++*this;
|
||||||
|
return iterator;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator!=(const ViewIterator& other) const {
|
||||||
|
return it != other.it;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto operator*() const {
|
||||||
|
return std::make_tuple(it->second, it->second->get_component<Ts>()...);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
Iiterator it;
|
||||||
|
};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unordered_map<uuids::uuid, Entity*> _entities;
|
std::unordered_map<uuids::uuid, Entity*> _entities;
|
||||||
};
|
};
|
||||||
|
@ -190,18 +255,6 @@ namespace ecs {
|
||||||
return View<Ts...>(entities);
|
return View<Ts...>(entities);
|
||||||
}
|
}
|
||||||
|
|
||||||
View<> view(size_t id) {
|
|
||||||
std::unordered_map<uuids::uuid, Entity*> entities;
|
|
||||||
|
|
||||||
for (auto [uuid, entity] : _entities) {
|
|
||||||
if (entity->has_components(id)) {
|
|
||||||
entities.insert({uuid, entity});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return View<>(entities);
|
|
||||||
}
|
|
||||||
|
|
||||||
View<> view(std::vector<size_t> ids) {
|
View<> view(std::vector<size_t> ids) {
|
||||||
std::unordered_map<uuids::uuid, Entity*> entities;
|
std::unordered_map<uuids::uuid, Entity*> entities;
|
||||||
|
|
||||||
|
@ -217,6 +270,15 @@ namespace ecs {
|
||||||
Entity* create_entity(uuids::uuid uuid);
|
Entity* create_entity(uuids::uuid uuid);
|
||||||
Entity* create_entity();
|
Entity* create_entity();
|
||||||
|
|
||||||
|
template <typename T, typename... Args>
|
||||||
|
T* create_entity(Args... args) {
|
||||||
|
uuids::uuid uuid = uuids::uuid_system_generator{}();
|
||||||
|
|
||||||
|
T* entity = new T(uuid, args...);
|
||||||
|
_entities.insert({uuid, entity});
|
||||||
|
return entity;
|
||||||
|
}
|
||||||
|
|
||||||
void remove_entity(ecs::Entity* entity);
|
void remove_entity(ecs::Entity* entity);
|
||||||
|
|
||||||
bool has_entity(uuids::uuid uuid) {
|
bool has_entity(uuids::uuid uuid) {
|
||||||
|
|
|
@ -15,7 +15,7 @@ namespace ecs {
|
||||||
_components.clear();
|
_components.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Entity::add_component(size_t id, Component* component) {
|
Component* Entity::add_component(size_t id, Component* component) {
|
||||||
if (id == 0) {
|
if (id == 0) {
|
||||||
throw std::runtime_error("Unknown component!");
|
throw std::runtime_error("Unknown component!");
|
||||||
}
|
}
|
||||||
|
@ -24,14 +24,8 @@ namespace ecs {
|
||||||
}
|
}
|
||||||
|
|
||||||
_components[id] = component;
|
_components[id] = component;
|
||||||
}
|
|
||||||
|
|
||||||
bool Entity::has_components(size_t id) const {
|
return component;
|
||||||
if (_components.find(id) == _components.end()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Entity::has_components(std::vector<size_t> ids) const {
|
bool Entity::has_components(std::vector<size_t> ids) const {
|
||||||
|
|
|
@ -9,6 +9,8 @@ namespace generated {
|
||||||
|
|
||||||
preload["ecs.Position"] = [&lua] {
|
preload["ecs.Position"] = [&lua] {
|
||||||
sol::table component = lua.create_table();
|
sol::table component = lua.create_table();
|
||||||
|
component["_id"] = ecs::ComponentID::id<Position>;
|
||||||
|
|
||||||
component.new_usertype<Position>("Position",
|
component.new_usertype<Position>("Position",
|
||||||
"x", &Position::x, "y", &Position::y,
|
"x", &Position::x, "y", &Position::y,
|
||||||
sol::base_classes, sol::bases<ecs::Component>()
|
sol::base_classes, sol::bases<ecs::Component>()
|
||||||
|
@ -18,7 +20,7 @@ namespace generated {
|
||||||
return std::make_pair(ecs::ComponentID::id<Position>, new Position(_x, _y));
|
return std::make_pair(ecs::ComponentID::id<Position>, new Position(_x, _y));
|
||||||
}));
|
}));
|
||||||
|
|
||||||
lua.set_function("_internal_to_Position", [] (ecs::Component* component) {
|
component.set_function("_convert", [] (ecs::Component* component) {
|
||||||
return (Position*)component;
|
return (Position*)component;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -27,6 +29,8 @@ namespace generated {
|
||||||
|
|
||||||
preload["ecs.Velocity"] = [&lua] {
|
preload["ecs.Velocity"] = [&lua] {
|
||||||
sol::table component = lua.create_table();
|
sol::table component = lua.create_table();
|
||||||
|
component["_id"] = ecs::ComponentID::id<Velocity>;
|
||||||
|
|
||||||
component.new_usertype<Velocity>("Velocity",
|
component.new_usertype<Velocity>("Velocity",
|
||||||
"x", &Velocity::x, "y", &Velocity::y,
|
"x", &Velocity::x, "y", &Velocity::y,
|
||||||
sol::base_classes, sol::bases<ecs::Component>()
|
sol::base_classes, sol::bases<ecs::Component>()
|
||||||
|
@ -36,7 +40,7 @@ namespace generated {
|
||||||
return std::make_pair(ecs::ComponentID::id<Velocity>, new Velocity(_x, _y));
|
return std::make_pair(ecs::ComponentID::id<Velocity>, new Velocity(_x, _y));
|
||||||
}));
|
}));
|
||||||
|
|
||||||
lua.set_function("_internal_to_Velocity", [] (ecs::Component* component) {
|
component.set_function("_convert", [] (ecs::Component* component) {
|
||||||
return (Velocity*)component;
|
return (Velocity*)component;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -45,6 +49,8 @@ namespace generated {
|
||||||
|
|
||||||
preload["ecs.Meta"] = [&lua] {
|
preload["ecs.Meta"] = [&lua] {
|
||||||
sol::table component = lua.create_table();
|
sol::table component = lua.create_table();
|
||||||
|
component["_id"] = ecs::ComponentID::id<Meta>;
|
||||||
|
|
||||||
component.new_usertype<Meta>("Meta",
|
component.new_usertype<Meta>("Meta",
|
||||||
"name", &Meta::name,
|
"name", &Meta::name,
|
||||||
sol::base_classes, sol::bases<ecs::Component>()
|
sol::base_classes, sol::bases<ecs::Component>()
|
||||||
|
@ -54,7 +60,7 @@ namespace generated {
|
||||||
return std::make_pair(ecs::ComponentID::id<Meta>, new Meta(_name));
|
return std::make_pair(ecs::ComponentID::id<Meta>, new Meta(_name));
|
||||||
}));
|
}));
|
||||||
|
|
||||||
lua.set_function("_internal_to_Meta", [] (ecs::Component* component) {
|
component.set_function("_convert", [] (ecs::Component* component) {
|
||||||
return (Meta*)component;
|
return (Meta*)component;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,14 @@
|
||||||
#include "ecs.h"
|
#include "ecs.h"
|
||||||
#include "ecs_components.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) {
|
inline void handle_error(sol::optional<std::string> maybe_msg) {
|
||||||
std::cerr << "Lua error, aborting!\n";
|
std::cerr << "Lua error, aborting!\n";
|
||||||
if (maybe_msg) {
|
if (maybe_msg) {
|
||||||
|
@ -38,10 +46,9 @@ int main(int argc, const char** argv) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ecs::ComponentID::regist<ecs::lua::Wrapper>("Wrapper");
|
ecs::ComponentID::add<Position>("Position");
|
||||||
ecs::ComponentID::regist<Position>("Position");
|
ecs::ComponentID::add<Velocity>("Velocity");
|
||||||
ecs::ComponentID::regist<Velocity>("Velocity");
|
ecs::ComponentID::add<Meta>("Meta");
|
||||||
ecs::ComponentID::regist<Meta>("Meta");
|
|
||||||
|
|
||||||
sol::state lua(sol::c_call<decltype(&handle_error), &handle_error>);
|
sol::state lua(sol::c_call<decltype(&handle_error), &handle_error>);
|
||||||
lua.open_libraries(sol::lib::base, sol::lib::package);
|
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';
|
std::cout << "Y: " << pos->y << '\n';
|
||||||
});
|
});
|
||||||
|
|
||||||
// These are really just an internal api that should not be used
|
std::cout << "Adding Orb!\n";
|
||||||
for (auto [uuid, entity] : manager.view(ecs::ComponentID::resolve("Random"))) {
|
manager.create_entity<Orb>(1.2f, 3.4f);
|
||||||
sol::table random = ((ecs::lua::Wrapper*)entity->get_component(ecs::ComponentID::resolve("Random")))->table;
|
for (auto [ent, pos, vel] : manager.view<Position, Velocity>()) {
|
||||||
|
std::cout << "X: " << pos->x << " + " << vel->x << '\n';
|
||||||
random["a"] = 21;
|
std::cout << "Y: " << pos->y << " + " << vel->y << '\n';
|
||||||
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';
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test serialization
|
// Test serialization
|
||||||
|
@ -122,7 +122,7 @@ int main(int argc, const char** argv) {
|
||||||
ecs::serial::serialize_ids(file);
|
ecs::serial::serialize_ids(file);
|
||||||
|
|
||||||
io::write<size_t>(file, manager.view<>().size());
|
io::write<size_t>(file, manager.view<>().size());
|
||||||
for (auto [uuid, entity] : manager.view<>()) {
|
for (auto [entity] : manager.view<>()) {
|
||||||
ecs::serial::serialize(file, entity);
|
ecs::serial::serialize(file, entity);
|
||||||
}
|
}
|
||||||
file.close();
|
file.close();
|
||||||
|
|
|
@ -2,17 +2,17 @@ local ecs = require "ecs"
|
||||||
local Position = require "ecs.Position"
|
local Position = require "ecs.Position"
|
||||||
local Velocity = require "ecs.Velocity"
|
local Velocity = require "ecs.Velocity"
|
||||||
local Meta = require "ecs.Meta"
|
local Meta = require "ecs.Meta"
|
||||||
local Wrapper = require 'ecs.Wrapper'
|
local LuaComponent = require 'ecs.LuaComponent'
|
||||||
|
|
||||||
function init()
|
function init()
|
||||||
print("Registering components")
|
print("Creating LuaComponents")
|
||||||
|
|
||||||
LuaData = ecs.register("LuaData", {
|
LuaData = LuaComponent.create("LuaData", {
|
||||||
speed = "number",
|
speed = "number",
|
||||||
something = "string",
|
something = "string",
|
||||||
alive = "boolean"
|
alive = "boolean"
|
||||||
})
|
})
|
||||||
TestThing = ecs.register("TestThing", {
|
TestThing = LuaComponent.create("TestThing", {
|
||||||
test = "string"
|
test = "string"
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
@ -43,18 +43,18 @@ function run()
|
||||||
--
|
--
|
||||||
-- ent:add_component(ecs.Wrapper.new("Stats", stats))
|
-- ent:add_component(ecs.Wrapper.new("Stats", stats))
|
||||||
|
|
||||||
print(ent:has_components("Position", "Velocity"))
|
print(ent:has_components(Position, Velocity))
|
||||||
pos = ent:get_component("Position")
|
pos = ent:get_component(Position)
|
||||||
print("x: " .. pos.x)
|
print("x: " .. pos.x)
|
||||||
print("y: " .. pos.y)
|
print("y: " .. pos.y)
|
||||||
vel = ent:get_component("Velocity")
|
vel = ent:get_component(Velocity)
|
||||||
print("v_x: " .. vel.x)
|
print("v_x: " .. vel.x)
|
||||||
print("v_y: " .. vel.y)
|
print("v_y: " .. vel.y)
|
||||||
|
|
||||||
print("View test")
|
print("View test")
|
||||||
manager:view("Position", "Velocity"):for_each(function(ent)
|
manager:view(Position, Velocity):for_each(function(ent)
|
||||||
pos = ent:get_component("Position")
|
pos = ent:get_component(Position)
|
||||||
vel = ent:get_component("Velocity")
|
vel = ent:get_component(Velocity)
|
||||||
|
|
||||||
pos.x = pos.x + vel.x
|
pos.x = pos.x + vel.x
|
||||||
pos.y = pos.y + vel.y
|
pos.y = pos.y + vel.y
|
||||||
|
@ -67,8 +67,8 @@ function run()
|
||||||
print("v_x: " .. vel.x)
|
print("v_x: " .. vel.x)
|
||||||
print("v_y: " .. vel.y)
|
print("v_y: " .. vel.y)
|
||||||
|
|
||||||
if ent:has_components("Meta") then
|
if ent:has_components(Meta) then
|
||||||
meta = ent:get_component("Meta")
|
meta = ent:get_component(Meta)
|
||||||
print("name: " .. meta.name)
|
print("name: " .. meta.name)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
@ -78,8 +78,8 @@ function run()
|
||||||
-- print "TEST"
|
-- print "TEST"
|
||||||
-- end
|
-- end
|
||||||
|
|
||||||
manager:view("LuaData"):for_each(function(ent)
|
manager:view(LuaData):for_each(function(ent)
|
||||||
d = ent:get_component("LuaData")
|
d = ent:get_component(LuaData)
|
||||||
|
|
||||||
print("speed: " .. d.speed)
|
print("speed: " .. d.speed)
|
||||||
d.speed = 11
|
d.speed = 11
|
||||||
|
@ -92,8 +92,8 @@ function run()
|
||||||
print("Something: " .. d.something)
|
print("Something: " .. d.something)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
manager:view("TestThing"):for_each(function(ent)
|
manager:view(TestThing):for_each(function(ent)
|
||||||
t = ent:get_component("TestThing")
|
t = ent:get_component(TestThing)
|
||||||
|
|
||||||
print("test: " .. t.test)
|
print("test: " .. t.test)
|
||||||
end)
|
end)
|
||||||
|
|
|
@ -1,17 +1,18 @@
|
||||||
local ecs = require "ecs"
|
local ecs = require "ecs"
|
||||||
require "ecs.Wrapper"
|
local LuaComponent = require "ecs.LuaComponent"
|
||||||
|
|
||||||
function init()
|
function init()
|
||||||
print("Registering components")
|
print("Registering components")
|
||||||
|
|
||||||
LuaData = ecs.register("LuaData", {
|
-- Swapped compared to test.lua
|
||||||
|
TestThing = LuaComponent.create("TestThing", {
|
||||||
|
test = "string"
|
||||||
|
})
|
||||||
|
LuaData = LuaComponent.create("LuaData", {
|
||||||
speed = "number",
|
speed = "number",
|
||||||
something = "string",
|
something = "string",
|
||||||
alive = "boolean"
|
alive = "boolean"
|
||||||
})
|
})
|
||||||
TestThing = ecs.register("TestThing", {
|
|
||||||
test = "string"
|
|
||||||
})
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function run()
|
function run()
|
||||||
|
@ -19,17 +20,15 @@ function run()
|
||||||
|
|
||||||
manager = get_manager()
|
manager = get_manager()
|
||||||
|
|
||||||
print("AAA")
|
manager:view(TestThing):for_each(function(ent)
|
||||||
|
data = ent:get_component(TestThing)
|
||||||
manager:view("TestThing"):for_each(function(ent)
|
|
||||||
data = ent:get_component("TestThing")
|
|
||||||
print("test: " .. data.test)
|
print("test: " .. data.test)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|
||||||
manager:view("LuaData"):for_each(function(ent)
|
manager:view(LuaData):for_each(function(ent)
|
||||||
-- @todo It would be nice if this could somehow be passed in as function arg
|
-- @todo It would be nice if this could somehow be passed in as function arg
|
||||||
data = ent:get_component("LuaData")
|
data = ent:get_component(LuaData)
|
||||||
print("speed: " .. data.speed)
|
print("speed: " .. data.speed)
|
||||||
print("something: " .. data.something)
|
print("something: " .. data.something)
|
||||||
-- @todo You cannot concatenate bool to string
|
-- @todo You cannot concatenate bool to string
|
||||||
|
|
Loading…
Reference in New Issue
Block a user