Component now stores whether or not it is a runtime component

This commit is contained in:
Dreaded_X 2019-06-26 23:39:07 +02:00
parent 7d55563ec8
commit 5229dc6e76
4 changed files with 26 additions and 18 deletions

View File

@ -7,10 +7,10 @@
#include <utility>
namespace ecs::lua {
struct Wrapper : TaggedComponent {
struct Wrapper : Component {
// @todo Figure out a more elegant way
Wrapper(sol::table _table) : TaggedComponent(get_typename<Wrapper>()), table(_table) {}
Wrapper() : TaggedComponent(get_typename<Wrapper>()) {}
Wrapper(sol::table _table) : Component(ComponentID::id<Wrapper>), table(_table) {}
Wrapper() : Component(ComponentID::id<Wrapper>) {}
sol::table table;
};

View File

@ -31,15 +31,15 @@ namespace ecs::lua {
"get_component", [&lua] (Entity* thiz, std::string name) -> sol::object {
// Convert to the correct component type
Component* component = thiz->get_component(ComponentID::get_id({name})[0]);
if (!component->_tagged()) {
if (!component->_runtime) {
// @todo Figure out a more elegant way to convert
auto f1 = lua["_internal_to_" + name];
if (f1.valid()) {
return f1(component);
}
} else {
TaggedComponent* tagged_component = (TaggedComponent*)component;
auto f1 = lua["_internal_to_" + tagged_component->_tag];
// @todo This call is not very fast...
auto f1 = lua["_internal_to_" + ComponentID::get_name(component->_id)];
if (f1.valid()) {
return f1(component);
}

View File

@ -19,7 +19,7 @@ namespace ecs::serial {
auto components = entity->get_components();
iohelper::write_length(os, components.size());
for (auto [id, component] : components) {
if (!component->_tagged()) {
if (!component->_runtime) {
auto functions = internal::functions.find(id);
if (functions == internal::functions.end()) {
throw std::runtime_error("No known serializer for id");
@ -29,8 +29,7 @@ namespace ecs::serial {
iohelper::write<bool>(os, false);
std::get<0>(internal::functions[id])(os, component);
} else {
TaggedComponent* tagged_component = (TaggedComponent*)component;
auto new_id = ComponentID::get_id({tagged_component->_tag})[0];
auto new_id = component->_id;
auto functions = internal::functions.find(new_id);
if (functions == internal::functions.end()) {

View File

@ -40,23 +40,32 @@ namespace ecs {
static std::unordered_map<std::string, size_t>& _ids();
static std::vector<size_t> get_id(std::vector<std::string> names);
// @todo This is slow...
static std::string get_name(size_t id) {
for (auto it = _ids().begin(); it != _ids().end(); ++it) {
if (it->second == id) {
return it->first;
}
}
throw std::runtime_error("ID does not exist");
}
// This looks kind of ugly
template <typename T>
inline static const size_t id = get_id({get_typename<T>()})[0];
};
struct Component {
Component() : _runtime(false), _id(0) {}
// @todo Would be nice if this worked with templates
Component(size_t id) : _runtime(true), _id(id) {}
virtual ~Component() {}
virtual bool _tagged() { return false; }
};
// Tag stores the real name of a the component for special components that allow duplicates
struct TaggedComponent : Component{
TaggedComponent(std::string tag) : _tag(tag) {}
const std::string _tag;
bool _tagged() override { return true; }
const bool _runtime;
const size_t _id;
};
class Entity {