diff --git a/lib/wal/sources/Entity/Entity.cpp b/lib/wal/sources/Entity/Entity.cpp index 56627a16..1dd1a1b3 100644 --- a/lib/wal/sources/Entity/Entity.cpp +++ b/lib/wal/sources/Entity/Entity.cpp @@ -21,7 +21,7 @@ namespace WAL _disabled(other._disabled) { for (const auto &cmp : other._components) - this->addComponent(*cmp); + this->addComponent(*cmp.second); } unsigned Entity::getUid() const @@ -46,25 +46,20 @@ namespace WAL Entity &Entity::addComponent(const Component &component) { - if (this->hasComponent(typeid(component))) - throw DuplicateError("A component of the type \"" + std::string(typeid(component).name()) + "\" already exists."); - this->_components.emplace_back(component.clone(*this)); + const std::type_index &type = typeid(component); + if (this->hasComponent(type)) + throw DuplicateError("A component of the type \"" + std::string(type.name()) + "\" already exists."); + this->_components.emplace(type, component.clone(*this)); return *this; } bool Entity::hasComponent(const std::type_info &type) const { - auto existing = std::find_if(this->_components.begin(), this->_components.end(), [&type] (const auto &cmp) { - return typeid(*cmp) == type; - }); - return existing != this->_components.end(); + return this->hasComponent(static_cast(type)); } bool Entity::hasComponent(const std::type_index &type) const { - auto existing = std::find_if(this->_components.begin(), this->_components.end(), [&type] (const auto &cmp) { - return std::type_index(typeid(*cmp)) == type; - }); - return existing != this->_components.end(); + return this->_components.contains(type); } } // namespace WAL \ No newline at end of file diff --git a/lib/wal/sources/Entity/Entity.hpp b/lib/wal/sources/Entity/Entity.hpp index 8e157c6e..e9fa672e 100644 --- a/lib/wal/sources/Entity/Entity.hpp +++ b/lib/wal/sources/Entity/Entity.hpp @@ -5,7 +5,7 @@ #pragma once #include -#include +#include #include #include #include "Component/Component.hpp" @@ -24,7 +24,7 @@ namespace WAL //! @brief Is this entity enabled? bool _disabled = false; //! @brief The list of the components of this entity - std::vector> _components = {}; + std::unordered_map> _components = {}; //! @brief This ID will be the one of the next entity created. static unsigned nextID; @@ -45,13 +45,11 @@ namespace WAL template T &getComponent() { - const std::type_info &type = typeid(T); - auto existing = std::find_if(this->_components.begin(), this->_components.end(), [&type] (const auto &cmp) { - return typeid(*cmp) == type; - }); + const std::type_index &type = typeid(T); + auto existing = this->_components.find(type); if (existing == this->_components.end()) throw NotFoundError("No component could be found with the type \"" + std::string(type.name()) + "\"."); - return *static_cast(existing->get()); + return *static_cast(existing->second.get()); } //! @brief Check if this entity has a component. @@ -77,9 +75,10 @@ namespace WAL template Entity &addComponent(Types &&...params) { - if (this->hasComponent()) - throw DuplicateError("A component of the type \"" + std::string(typeid(T).name()) + "\" already exists."); - this->_components.push_back(std::make_unique(*this, std::forward(params)...)); + const std::type_index &type = typeid(T); + if (this->hasComponent(type)) + throw DuplicateError("A component of the type \"" + std::string(type.name()) + "\" already exists."); + this->_components[type] = std::make_unique(*this, std::forward(params)...); return *this; } @@ -94,9 +93,7 @@ namespace WAL Entity &removeComponent() { const std::type_info &type = typeid(T); - auto existing = std::find_if(this->_components.begin(), this->_components.end(), [&type] (const auto &cmp) { - return typeid(*cmp) == type; - }); + auto existing = this->_components.find(type); if (existing == this->_components.end()) throw NotFoundError("No component could be found with the type \"" + std::string(type.name()) + "\"."); this->_components.erase(existing); diff --git a/sources/Map/Map.cpp b/sources/Map/Map.cpp index c90609f4..338a1f42 100644 --- a/sources/Map/Map.cpp +++ b/sources/Map/Map.cpp @@ -3,6 +3,7 @@ // Edited by Benjamin Henry on 5/26/21. // +#include #include "Map.hpp" namespace RAY3D = RAY::Drawables::Drawables3D; @@ -18,8 +19,8 @@ namespace BBM for (int j = 0; j < height + 1; j++) { if (!(i % 2) && !(j % 2)) { scene->addEntity("Unbreakable Wall") - .addComponent(Vector3f(i, 0, j)) - //.addComponent(1) + .addComponent(i, 0, j) + .addComponent(1) .addComponent>(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj)); } } @@ -33,19 +34,19 @@ namespace BBM scene->addEntity("Bottom Wall") .addComponent(Vector3f((width + 1) / 2, 0, -1)) - //.addComponent(1) + .addComponent(1) .addComponent>(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(width + 3, 1, 1)); scene->addEntity("Upper Wall") .addComponent(Vector3f((width + 1) / 2, 0, height + 1)) - //.addComponent(1) + .addComponent(1) .addComponent>(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(width + 3, 1, 1)); scene->addEntity("Left Wall") .addComponent(Vector3f(width + 1, 0, (height + 1) / 2)) - //.addComponent(1) + .addComponent(1) .addComponent>(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(1, 1, height + 3)); scene->addEntity("Right Wall") .addComponent(Vector3f(-1, 0, (height + 1) / 2)) - //.addComponent(1) + .addComponent(1) .addComponent>(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(1, 1, height + 3)); } @@ -53,7 +54,6 @@ namespace BBM { scene->addEntity("Floor") .addComponent(Vector3f(width / 2, -1, height / 2)) - //.addComponent(1) .addComponent>("assets/wall/floor.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/floor.png"), RAY::Vector3(width + 2, 0, height + 2)); } @@ -81,7 +81,7 @@ namespace BBM scene->addEntity("Breakable Block") .addComponent(coords) .addComponent(1) - //.addComponent(1) + .addComponent(1) .addComponent>("assets/wall/breakable_wall.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/breakable_wall.png")); } @@ -89,7 +89,6 @@ namespace BBM { scene->addEntity("Floor") .addComponent(Vector3f(coords)) - //.addComponent(1) .addComponent>("assets/wall/floor.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/floor.png")); } @@ -97,7 +96,7 @@ namespace BBM { scene->addEntity("Unbreakable Block") .addComponent(coords) - //.addComponent(1) + .addComponent(1) .addComponent>("assets/wall/unbreakable_wall.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/unbreakable_wall.png")); }