diff --git a/sources/Component/Collision/CollisionComponent.cpp b/sources/Component/Collision/CollisionComponent.cpp index fe27492f..dc0992f4 100644 --- a/sources/Component/Collision/CollisionComponent.cpp +++ b/sources/Component/Collision/CollisionComponent.cpp @@ -16,27 +16,38 @@ namespace BBM return new CollisionComponent(entity); } - CollisionComponent::CollisionComponent(WAL::Entity &entity, std::function callback, Vector3f bound) - : WAL::Component(entity), onCollide(callback), bound(bound) + CollisionComponent::CollisionComponent(WAL::Entity &entity, std::function onCollide, std::function onCollided, Vector3f bound) + : WAL::Component(entity), _onCollide(onCollide), _onCollided(onCollided), bound(bound) { } - CollisionComponent::CollisionComponent(WAL::Entity &entity, std::function callback, float boundSize) - : WAL::Component(entity), onCollide(callback), bound({boundSize, boundSize, boundSize}) + CollisionComponent::CollisionComponent(WAL::Entity &entity, std::function onCollide, std::function onCollided, float boundSize) + : WAL::Component(entity), _onCollide(onCollide), _onCollided(onCollided), bound({boundSize, boundSize, boundSize}) { } - CollisionComponent::CollisionComponent(WAL::Entity &entity, WAL::Callback callback, Vector3f bound) - : WAL::Component(entity), onCollide(callback), bound(bound) + CollisionComponent::CollisionComponent(WAL::Entity &entity, WAL::Callback onCollide, WAL::Callback onCollided, Vector3f bound) + : WAL::Component(entity), _onCollide(onCollide), _onCollided(onCollided), bound(bound) { } - CollisionComponent::CollisionComponent(WAL::Entity &entity, WAL::Callback callback, float boundSize) - : WAL::Component(entity), onCollide(callback), bound({boundSize, boundSize, boundSize}) + CollisionComponent::CollisionComponent(WAL::Entity &entity, WAL::Callback onCollide, WAL::Callback onCollided, float boundSize) + : WAL::Component(entity), _onCollide(onCollide), _onCollided(onCollided), bound({boundSize, boundSize, boundSize}) { } CollisionComponent::CollisionComponent(WAL::Entity &entity, Vector3f bound) - : WAL::Component(entity), onCollide(), bound(bound) + : WAL::Component(entity), _onCollide(), _onCollided(), bound(bound) { } CollisionComponent::CollisionComponent(WAL::Entity &entity, float boundSize) - : WAL::Component(entity), onCollide(), bound({boundSize, boundSize, boundSize}) + : WAL::Component(entity), _onCollide(), _onCollided(), bound({boundSize, boundSize, boundSize}) { } + + const WAL::Callback &CollisionComponent::getOnCollide(void) const + { + return _onCollide; + } + + const WAL::Callback &CollisionComponent::getOnCollided(void) const + { + return _onCollided; + } + } \ No newline at end of file diff --git a/sources/Component/Collision/CollisionComponent.hpp b/sources/Component/Collision/CollisionComponent.hpp index 2f10ad5b..8ef05bcd 100644 --- a/sources/Component/Collision/CollisionComponent.hpp +++ b/sources/Component/Collision/CollisionComponent.hpp @@ -14,28 +14,30 @@ namespace BBM class CollisionComponent : public WAL::Component { private: + //! @brief onCollide functions to be called + WAL::Callback _onCollide; + //! @brief onCollided functions to be called + WAL::Callback _onCollided; public: //! @brief Bound size on all axis Vector3f bound; - //! @brief onCollide functions to be called - WAL::Callback onCollide; //! @inherit WAL::Component *clone(WAL::Entity &entity) const override; - + //! @brief A component can't be instantiated, it should be derived. explicit CollisionComponent(WAL::Entity &entity); //! @brief Constructor with a callback function - CollisionComponent(WAL::Entity &entity, std::function callback, Vector3f bound); + CollisionComponent(WAL::Entity &entity, std::function onCollide, std::function onCollided, Vector3f bound); //! @brief Constructor with a callback function, same boundSize for all axis - CollisionComponent(WAL::Entity &entity, std::function callback, float boundSize = 0); + CollisionComponent(WAL::Entity &entity, std::function onCollide, std::function onCollided, float boundSize = 0); //! @brief Constructor with a WAL::Callback - CollisionComponent(WAL::Entity &entity, WAL::Callback callback, Vector3f bound); + CollisionComponent(WAL::Entity &entity, WAL::Callback onCollide, WAL::Callback onCollided,Vector3f bound); //! @brief Constructor with a WAL::Callback, same boundSize for all axis - CollisionComponent(WAL::Entity &entity, WAL::Callback callback, float boundSize = 0); + CollisionComponent(WAL::Entity &entity, WAL::Callback onCollide, WAL::Callback onCollided, float boundSize = 0); //! @brief Constructor of collider with no callback CollisionComponent(WAL::Entity &entity, Vector3f bound); @@ -51,5 +53,11 @@ namespace BBM //! @brief A component can't be assigned CollisionComponent &operator=(const CollisionComponent &) = delete; + + //! @brief Get reference of the onCollide callback + const WAL::Callback &getOnCollide(void) const; + + //! @brief Get reference of the onCollided callback + const WAL::Callback &getOnCollided(void) const; }; } \ No newline at end of file diff --git a/sources/Component/Movable/MovableComponent.cpp b/sources/Component/Movable/MovableComponent.cpp index 4fc9bc0c..51f0707d 100644 --- a/sources/Component/Movable/MovableComponent.cpp +++ b/sources/Component/Movable/MovableComponent.cpp @@ -19,4 +19,15 @@ namespace BBM { this->_acceleration += force; } + + void MovableComponent::resetVelocity(void) + { + this->_velocity = {0, 0, 0}; + } + + const Vector3f &MovableComponent::getVelocity(void) const + { + return _velocity; + } + } // namespace WAL \ No newline at end of file diff --git a/sources/Component/Movable/MovableComponent.hpp b/sources/Component/Movable/MovableComponent.hpp index b9a997c9..13c8bf90 100644 --- a/sources/Component/Movable/MovableComponent.hpp +++ b/sources/Component/Movable/MovableComponent.hpp @@ -15,14 +15,20 @@ namespace BBM private: //! @brief The acceleration of this entity. Vector3f _acceleration; - public: //! @brief The velocity of the entity. Vector3f _velocity; + public: //! @brief Add an instant force to this entity. //! @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; + //! @inherit WAL::Component *clone(WAL::Entity &entity) const override; diff --git a/sources/System/Collision/CollisionSystem.cpp b/sources/System/Collision/CollisionSystem.cpp index 152051d6..d3063ebd 100644 --- a/sources/System/Collision/CollisionSystem.cpp +++ b/sources/System/Collision/CollisionSystem.cpp @@ -20,7 +20,7 @@ namespace BBM auto &col = entity.getComponent(); Vector3f position = posA.position; if (entity.hasComponent(typeid(MovableComponent))) - position += entity.getComponent()._velocity; + position += entity.getComponent().getVelocity(); Vector3f minA = Vector3f::min(position, position + col.bound); Vector3f maxA = Vector3f::max(position, position + col.bound); for (auto &other : _wal.scene->getEntities()) { @@ -29,14 +29,16 @@ namespace BBM if (!other.hasComponent(typeid(CollisionComponent)) || !other.hasComponent(typeid(PositionComponent))) continue; - auto colB = entity.getComponent().bound; + auto colB = entity.getComponent(); auto posB = other.getComponent().position; - Vector3f minB = Vector3f::min(posB, posB + colB); - Vector3f maxB = Vector3f::max(posB, posB + colB); + Vector3f minB = Vector3f::min(posB, posB + colB.bound); + Vector3f maxB = Vector3f::max(posB, posB + colB.bound); if ((minA.x <= maxB.x && maxA.x >= minB.x) && (minA.y <= maxB.y && maxA.y >= minB.y) && - (minA.z <= maxB.z && maxA.z >= minB.z)) - col.onCollide(entity, other); + (minA.z <= maxB.z && maxA.z >= minB.z)) { + col.getOnCollide()(entity, other); + colB.getOnCollided()(entity, other); + } } } } \ No newline at end of file