3#include "spark/core/Component.h"
4#include "spark/core/GameObject.h"
5#include "spark/core/components/Transform.h"
7#include "spark/math/Rectangle.h"
8#include "spark/math/Vector4.h"
9#include "spark/patterns/Signal.h"
10#include "spark/patterns/Traverser.h"
11#include "spark/rtti/HasRtti.h"
13#include "glm/gtc/matrix_transform.hpp"
15namespace spark::core::components
24 SPARK_ALLOW_PRIVATE_SERIALIZATION
35 matrix = glm::translate(matrix, {m_rectangle.position.x, m_rectangle.position.y, 0});
38 auto matrix4 = glm::translate(matrix, {m_rectangle.extent.x, m_rectangle.extent.y, 0});
41 return {matrix[3][0], matrix[3][1], matrix4[3][0], matrix4[3][1]};
56 if (this_bounds.x <= other_bounds.z && this_bounds.z >= other_bounds.x && this_bounds.y <= other_bounds.w && this_bounds.w >= other_bounds.y)
63 :
Component(parent), m_rectangle(std::move(rectangle)) {}
81 :
Collider(parent, std::move(rectangle)) {}
100 : Collider(parent, std::move(rectangle)) {}
105 for (
const auto* collider : allColliders())
106 if (collider !=
this)
116 [[nodiscard]] std::vector<const Collider*> allColliders()
118 std::vector<const Collider*> colliders;
119 auto traverser = patterns::make_traverser<GameObject>([&](
const GameObject* obj)
121 for (
const auto* component : obj->components())
123 colliders.push_back(static_cast<const
Collider*>(component));
125 patterns::traverse_tree(
gameObject()->root(), traverser);
A component that can be attached to a GameObject to provide additional functionality.
Definition Component.h:20
GameObject * gameObject()
Gets the GameObject that the component is attached to.
Definition Component.cpp:19
A GameObject is any object in the game. It contains a list of components that provides functionality ...
Definition GameObject.h:26
components::Transform * transform() const
Gets the transform component of the GameObject.
Definition GameObject.cpp:63
A component representing a square collider.
Definition Collider.h:22
bool collidesWith(const Collider &other) const
Checks if the collider is colliding with another collider.
Definition Collider.h:49
math::Vector4< float > bounds() const
Gets the bounds of the rectangle in screen space.
Definition Collider.h:31
A square collider that moves and checks for collisions with other colliders.
Definition Collider.h:88
void onUpdate(float) override
Method called on every frame.
Definition Collider.h:102
patterns::Signal< const Collider & > onCollision
Signal emitted when a collision is detected.
Definition Collider.h:93
A square collider that does not move and is only used for collision detection.
Definition Collider.h:73
Represents a rectangle in 2D space.
Definition Rectangle.h:15
A vector with four components.
Definition Vector4.h:13
A signal is a class used to emit events.
Definition Slot.h:10
virtual RttiBase & rttiInstance() const =0