diff --git a/ecs-lua/include/ecs-lua.h b/ecs-lua/include/ecs-lua.h index ce75e35..bcdbed9 100644 --- a/ecs-lua/include/ecs-lua.h +++ b/ecs-lua/include/ecs-lua.h @@ -7,10 +7,10 @@ #include namespace ecs::lua { - struct Wrapper : TaggedComponent { + struct Wrapper : Component { // @todo Figure out a more elegant way - Wrapper(sol::table _table) : TaggedComponent(get_typename()), table(_table) {} - Wrapper() : TaggedComponent(get_typename()) {} + Wrapper(sol::table _table) : Component(ComponentID::id), table(_table) {} + Wrapper() : Component(ComponentID::id) {} sol::table table; }; diff --git a/ecs-lua/src/ecs-lua.cpp b/ecs-lua/src/ecs-lua.cpp index 45e76b3..8413f30 100644 --- a/ecs-lua/src/ecs-lua.cpp +++ b/ecs-lua/src/ecs-lua.cpp @@ -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); } diff --git a/ecs-serial/src/ecs-serial.cpp b/ecs-serial/src/ecs-serial.cpp index f2e8563..9d89342 100644 --- a/ecs-serial/src/ecs-serial.cpp +++ b/ecs-serial/src/ecs-serial.cpp @@ -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(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()) { diff --git a/ecs/include/ecs.h b/ecs/include/ecs.h index e8889e2..40b1f24 100644 --- a/ecs/include/ecs.h +++ b/ecs/include/ecs.h @@ -40,23 +40,32 @@ namespace ecs { static std::unordered_map& _ids(); static std::vector get_id(std::vector 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 inline static const size_t id = get_id({get_typename()})[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 {