Skip to content

File ColliderComponent.hpp

File List > Components > ColliderComponent.hpp

Go to the documentation of this file

#ifndef ENGINE_COMPONENTS_COLLIDER_HPP_
#define ENGINE_COMPONENTS_COLLIDER_HPP_

#include "CollisionLayers.hpp"
#include "ShapeComponent.hpp"

#include <cstdint>
#include <optional>
#include <raylib.h>

namespace Engine {
struct ColliderComponent {

    std::optional<ShapeComponent> shape;

    bool enabled = true;

    bool isTrigger = false;

    bool isStatic = false;

    int layer = CollisionLayers::kDefaultLayer;

    std::uint32_t mask = CollisionLayers::kAllLayers;

    bool Accepts(const ColliderComponent& other) const {
        return (mask & CollisionLayers::Bit(other.layer)) != 0u;
    }

    static bool CanCollide(const ColliderComponent& a, const ColliderComponent& b) {
        return a.Accepts(b) && b.Accepts(a);
    }

    static ColliderComponent Create(const bool enabled = true, const bool isTrigger = false) {
        ColliderComponent collider;
        collider.enabled = enabled;
        collider.isTrigger = isTrigger;
        return collider;
    }

    static ColliderComponent Create(const std::optional<ShapeComponent>& shape,
                                    const bool enabled = true, const bool isTrigger = false) {
        ColliderComponent collider;
        collider.shape = shape;
        collider.enabled = enabled;
        collider.isTrigger = isTrigger;
        return collider;
    }
};
} // namespace Engine

#endif // ENGINE_COMPONENTS_COLLIDER_HPP_