diff --git a/lib/wal/sources/Entity/Entity.hpp b/lib/wal/sources/Entity/Entity.hpp index 50770d18..25234f31 100644 --- a/lib/wal/sources/Entity/Entity.hpp +++ b/lib/wal/sources/Entity/Entity.hpp @@ -79,6 +79,32 @@ namespace WAL return static_cast(existing->second.get()); } + //! @brief Get a component of a specific type + //! @tparam The type of the component + //! @throw NotFoundError if the component could not be found + //! @return The component of the requested type. + template + const T &getComponent() const + { + const T *ret = this->tryGetComponent(); + if (ret == nullptr) + throw NotFoundError("No component could be found with the type \"" + std::string(typeid(T).name()) + "\"."); + return *ret; + } + + //! @brief Get a component of a specific type or null if not found. + //! @tparam The type of the component + //! @return The component or nullptr if not found. + template + const T *tryGetComponent() const + { + const std::type_index &type = typeid(T); + auto existing = this->_components.find(type); + if (existing == this->_components.end()) + return nullptr; + return static_cast(existing->second.get()); + } + //! @brief Check if this entity has a component. //! @tparam T The type of the component template diff --git a/lib/wal/sources/Models/Callback.hpp b/lib/wal/sources/Models/Callback.hpp index dc12a18b..2c988db4 100644 --- a/lib/wal/sources/Models/Callback.hpp +++ b/lib/wal/sources/Models/Callback.hpp @@ -64,4 +64,7 @@ namespace WAL this->addCallback(callback); } }; + + template + static constexpr Callback EmptyCallback; } // namespace WAL \ No newline at end of file diff --git a/sources/Component/Movable/MovableComponent.cpp b/sources/Component/Movable/MovableComponent.cpp index 51f0707d..26a8fd11 100644 --- a/sources/Component/Movable/MovableComponent.cpp +++ b/sources/Component/Movable/MovableComponent.cpp @@ -20,11 +20,6 @@ namespace BBM this->_acceleration += force; } - void MovableComponent::resetVelocity(void) - { - this->_velocity = {0, 0, 0}; - } - const Vector3f &MovableComponent::getVelocity(void) const { return _velocity; diff --git a/sources/Component/Movable/MovableComponent.hpp b/sources/Component/Movable/MovableComponent.hpp index 13c8bf90..6fecf89e 100644 --- a/sources/Component/Movable/MovableComponent.hpp +++ b/sources/Component/Movable/MovableComponent.hpp @@ -23,9 +23,6 @@ namespace BBM //! @param force The force to add to this entity's acceleration. The force is added instantly and in one go. void addForce(Vector3f force); - //! @brief Set velocity to 0 - void resetVelocity(void); - //! @brief Get velocity const Vector3f &getVelocity(void) const; @@ -42,5 +39,6 @@ namespace BBM MovableComponent &operator=(const MovableComponent &) = delete; friend class MovableSystem; + friend class MapGenerator; }; } // namespace WAL \ No newline at end of file diff --git a/sources/Map/Map.cpp b/sources/Map/Map.cpp index 258dcd9d..722f0433 100644 --- a/sources/Map/Map.cpp +++ b/sources/Map/Map.cpp @@ -5,11 +5,31 @@ #include #include "Map.hpp" +#include namespace RAY3D = RAY::Drawables::Drawables3D; namespace BBM -{ +{ + void MapGenerator::wallCollide(WAL::Entity &entity, const WAL::Entity &wall) + { + auto *mov = entity.tryGetComponent(); + if (!mov) + return; + auto &pos = entity.getComponent(); + const auto &wallPos = wall.getComponent(); + auto diff = pos.position + mov->getVelocity() - wallPos.position; + std::cout << diff << std::endl; + if (diff.x <= 0 && mov->_velocity.x < 0) + mov->_velocity.x = 0; + if (diff.x >= 0 && mov->_velocity.x > 0) + mov->_velocity.x = 0; + if (diff.z <= 0 && mov->_velocity.z < 0) + mov->_velocity.z = 0; + if (diff.z >= 0 && mov->_velocity.z > 0) + mov->_velocity.z = 0; + } + void MapGenerator::generateUnbreakableBlock(int width, int height, std::shared_ptr scene) { std::string unbreakableObj = "assets/wall/unbreakable_wall.obj"; @@ -20,7 +40,7 @@ namespace BBM if (!(i % 2) && !(j % 2)) { scene->addEntity("Unbreakable Wall") .addComponent(i, 0, j) - .addComponent(1) + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj)); } } @@ -34,19 +54,19 @@ namespace BBM scene->addEntity("Bottom Wall") .addComponent(Vector3f((width + 1) / 2, 0, -1)) - .addComponent(1) + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) .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(WAL::Callback(), &MapGenerator::wallCollide, .75) .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(WAL::Callback(), &MapGenerator::wallCollide, .75) .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(WAL::Callback(), &MapGenerator::wallCollide, .75) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(1, 1, height + 3)); } @@ -81,7 +101,7 @@ namespace BBM scene->addEntity("Breakable Block") .addComponent(coords) .addComponent(1) - .addComponent(1) + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) .addComponent("assets/wall/breakable_wall.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/breakable_wall.png")); } @@ -96,7 +116,7 @@ namespace BBM { scene->addEntity("Unbreakable Block") .addComponent(coords) - .addComponent(1) + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) .addComponent("assets/wall/unbreakable_wall.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/unbreakable_wall.png")); } @@ -155,7 +175,7 @@ namespace BBM MapGenerator::MapBlock MapGenerator::createHeight(MapBlock map, int width, int height) { - double rnd = static_cast(std::rand())/RAND_MAX; + double rnd = static_cast(std::rand()) / RAND_MAX; if (rnd > 0.60) { for (int i = 0; i < width; i++) { diff --git a/sources/Map/Map.hpp b/sources/Map/Map.hpp index 7f396b31..4059925b 100644 --- a/sources/Map/Map.hpp +++ b/sources/Map/Map.hpp @@ -119,6 +119,8 @@ namespace BBM public: + static void wallCollide(WAL::Entity &entity, const WAL::Entity &wall); + //! @param width Width of the map //! @param height Height of the map diff --git a/sources/Models/Vector3.hpp b/sources/Models/Vector3.hpp index dc1752f1..d4ba7337 100644 --- a/sources/Models/Vector3.hpp +++ b/sources/Models/Vector3.hpp @@ -71,7 +71,13 @@ namespace BBM } template - Vector3 &operator*=(T2 d) + Vector3 operator-(const Vector3 &vec) const + { + return Vector3(this->x - vec.x, this->y - vec.y, this->z - vec.z); + } + + template + Vector3 &operator*=(const T2 d) { this->x *= d; this->y *= d; @@ -80,7 +86,7 @@ namespace BBM } template - Vector3 operator*(T2 d) const + Vector3 operator*(const T2 d) const { return Vector3(this->x * d, this->y * d, this->z * d); } diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 6dbce849..bfcddef7 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -66,20 +66,8 @@ namespace BBM .addComponent() .addComponent() .addComponent(RAY::ModelAnimations("assets/player/player.iqm"), 1) - .addComponent(2) + .addComponent(1) .addComponent(); - scene->addEntity("cube") - .addComponent(-5, 0, -5) - .addComponent(Vector3f(-5, 0, -5), Vector3f(3, 3, 3), RED) - .addComponent() - .addComponent() - .addComponent([](WAL::Entity &, const WAL::Entity &){}, - [](WAL::Entity &actual, const WAL::Entity &) { - try { - auto &mov = actual.getComponent(); - mov.resetVelocity(); - } catch (std::exception &e) { } - }, 3); scene->addEntity("camera") .addComponent(8, 20, 7)