R-Type  2
Doom but in better
Loading...
Searching...
No Matches
Collision.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2024
3** rtype (Workspace)
4** File description:
5** Collision.cpp
6*/
7
15
24GUI::ECS::Systems::Collision::Collision(const std::uint32_t entityId, const float width, const float height, const float positionX, const float positionY)
25 : EntityNode(entityId), _rect({ {width, height}, {positionX, positionY} })
26{
27 _updateMouseCollisionData();
28}
29
34
41{
42 _rect.size.first = width;
43 _updateMouseCollisionData();
44}
45
52{
53 _rect.size.second = height;
54 _updateMouseCollisionData();
55}
56
63{
64 _rect.position.first = posX;
65 _updateMouseCollisionData();
66}
67
74{
75 _rect.position.second = posY;
76 _updateMouseCollisionData();
77}
78
84void GUI::ECS::Systems::Collision::setPosition(const std::pair<float, float> &position)
85{
86 _rect.position = position;
87 _updateMouseCollisionData();
88}
89
95void GUI::ECS::Systems::Collision::setDimension(const std::pair<float, float> &dimension)
96{
97 _rect.size = dimension;
98 _updateMouseCollisionData();
99}
100
107{
108 _rect = rect;
109 _updateMouseCollisionData();
110}
111
117void GUI::ECS::Systems::Collision::setMousePosition(const std::pair<int, int> &mousePosition)
118{
119 _mouse.update(mousePosition);
120 _updateMouseCollisionData();
121}
122
123
129void GUI::ECS::Systems::Collision::update(const std::pair<int, int> &mouse)
130{
131 _mouse.update(mouse);
132 _updateMouseCollisionData();
133}
140{
141 _isHovered = copy.isHovered();
142 _isClicked = copy.isClicked();
143 setGeometry(copy.getGeometry());
144 _mouse.update(copy.getMouseInfo());
145}
146
153{
154 _mouse.update(mouse);
155 _updateMouseCollisionData();
156}
157
158
165{
166 _mouse.update(mouse);
167 _updateMouseCollisionData();
168}
169
176{
177 return _isClicked;
178}
179
186{
187 return _isHovered;
188}
189
196{
197 return _rect.size.first;
198}
199
206{
207 return _rect.size.second;
208}
209
216{
217 return _rect.position.first;
218}
219
226{
227 return _rect.position.second;
228}
229
236{
237 return _rect;
238}
239
245const std::pair<float, float> GUI::ECS::Systems::Collision::getPosition() const
246{
247 return _rect.position;
248}
249
255const std::pair<float, float> GUI::ECS::Systems::Collision::getDimension() const
256{
257 return _rect.size;
258}
259
269
277{
278 PRETTY_INFO << "Collision: Checking if 2 shapes are colliding" << std::endl;
279 const bool rightEdge = _rect.position.first + _rect.size.first <= itemTwo._rect.position.first;
280 const bool leftEdge = _rect.position.first >= itemTwo._rect.position.first + itemTwo._rect.size.first;
281 const bool bottomEdge = _rect.position.second + _rect.size.second <= itemTwo._rect.position.second;
282 const bool topEdge = _rect.position.second >= itemTwo._rect.position.second + itemTwo._rect.size.second;
283 const bool noOverlap = (rightEdge || leftEdge || bottomEdge || topEdge);
284 if (noOverlap) {
285 return false;
286 }
287 return true;
288}
289
290const std::string GUI::ECS::Systems::Collision::getInfo(const unsigned int indent) const
291{
292
293 std::string indentation = "";
294 for (unsigned int i = 0; i < indent; ++i) {
295 indentation += "\t";
296 }
297 std::string result = indentation + "Collision Info:\n";
298 result += indentation + "- Entity Id: " + Recoded::myToString(getEntityNodeId()) + "\n";
299 result += indentation + "- Hovered: " + Recoded::myToString(_isHovered) + "\n";
300 result += indentation + "- Clicked: " + Recoded::myToString(_isClicked) + "\n";
301 result += indentation + "- Float Rectangle:" + Recoded::myToString(_rect) + "\n";
302 result += indentation + "- Mouse Info: {\n" + _mouse.getInfo(indent + 1) + indentation + "}\n";
303 return result;
304}
305
306std::ostream &GUI::ECS::Systems::operator<<(std::ostream &os, const GUI::ECS::Systems::Collision &item)
307{
308 os << item.getInfo();
309 return os;
310}
311
319{
320 if (this != &copy) {
321 update(copy);
322 }
323 return *this;
324}
325
330{
331 PRETTY_DEBUG << "Updating the collision between the mouse and the shape." << std::endl;
332 _isHovered = false;
333 _isClicked = false;
334 const std::pair<int, int> &mousePos = _mouse.getMousePosition();
335 const bool inFocus = _mouse.isMouseInFocus();
336
337 if (
338 mousePos.first >= _rect.position.first && mousePos.first <= _rect.position.first + _rect.size.first
339 && mousePos.second >= _rect.position.second && mousePos.second <= _rect.position.second + _rect.size.second
340 && inFocus
341 ) {
342 _isHovered = true;
343 }
344
345 if (_isHovered && (_mouse.isMouseLeftButtonClicked() || _mouse.isMouseRightButtonClicked())) {
346 _isClicked = true;
347 }
348 PRETTY_SUCCESS << "Collision data updated." << std::endl;
349}
350
352{
353 return left.getPosition() == right.getPosition() &&
354 left.getDimension() == right.getDimension();
355};
356
358{
359 return !(left == right);
360};
361
363{
364 return Collision(
365 0,
366 left.getWidth() + right.getWidth(),
367 left.getHeight() + right.getHeight(),
368 left.getPositionX() + right.getPositionX(),
369 left.getPositionY() + right.getPositionY()
370 );
371};
372
374{
375 return Collision(
376 0,
377 left.getWidth() - right.getWidth(),
378 left.getHeight() - right.getHeight(),
379 left.getPositionX() - right.getPositionX(),
380 left.getPositionY() - right.getPositionY()
381 );
382};
383
385{
386 return Collision(
387 0,
388 left.getWidth() * right.getWidth(),
389 left.getHeight() * right.getHeight(),
390 left.getPositionX() * right.getPositionX(),
391 left.getPositionY() * right.getPositionY()
392 );
393};
394
396{
397 left.setWidth(left.getWidth() + right.getWidth());
398 left.setHeight(left.getHeight() + right.getHeight());
399 left.setPositionX(left.getPositionX() + right.getPositionX());
400 left.setPositionY(left.getPositionY() + right.getPositionY());
401 return left;
402};
403
405{
406 left.setWidth(left.getWidth() - right.getWidth());
407 left.setHeight(left.getHeight() - right.getHeight());
408 left.setPositionX(left.getPositionX() - right.getPositionX());
409 left.setPositionY(left.getPositionY() - right.getPositionY());
410 return left;
411};
412
414{
415 left.setWidth(left.getWidth() * right.getWidth());
416 left.setHeight(left.getHeight() * right.getHeight());
417 left.setPositionX(left.getPositionX() * right.getPositionX());
418 left.setPositionY(left.getPositionY() * right.getPositionY());
419 return left;
420};
#define PRETTY_DEBUG
Debug log with details and colour.
#define PRETTY_INFO
Info log with details and colour.
#define PRETTY_SUCCESS
Success log with details and colour.
Represents a rectangular component that can detect collisions and mouse interactions,...
Definition Collision.hpp:39
void setPositionY(const float &posY)
Sets the Y-coordinate of the component's position and updates collision data.
Definition Collision.cpp:73
Recoded::FloatRect _rect
Definition Collision.hpp:97
const std::pair< float, float > getDimension() const
Get the dimension of the item in the form of an sf::Vector2i.
void setHeight(const float &height)
Sets the height of the component and updates collision data.
Definition Collision.cpp:51
const bool isClicked() const
Checks if the component is clicked by the mouse.
void updateMouseInfo(const GUI::ECS::Systems::MouseInfo &mouse)
Updates the mouse info object used for collision checks.
void setWidth(const float &width)
Sets the width of the component and updates collision data.
Definition Collision.cpp:40
void _updateMouseCollisionData()
Updates the mouse collision data, setting hover and click states.
const float getWidth() const
Gets the width of the component.
void setDimension(const std::pair< float, float > &dimension)
Set the dimension of the object.
Definition Collision.cpp:95
void update(const std::pair< int, int > &mousePosition)
Update the mouse info object used for mouse tracking.
const float getHeight() const
Gets the height of the component.
~Collision()
Default destructor.
Definition Collision.cpp:33
const bool isColliding(const Collision &itemTwo) const
Checks if this component is colliding with another Collision.
void setPositionX(const float &posX)
Sets the X-coordinate of the component's position and updates collision data.
Definition Collision.cpp:62
const Recoded::FloatRect getGeometry() const
This is the function in charge of returning the coordinates in the form of an sf::FloatRect.
const std::string getInfo(const unsigned int indent=0) const
This is a function meant for debugging purposes It will dump the current state of the variables upon ...
const std::pair< float, float > getPosition() const
Get the position of the item in the form of an sf::Vector2i.
const float getPositionY() const
Gets the Y-coordinate of the component's position.
void setMousePosition(const std::pair< int, int > &position)
Updates the mouse position for collision checks.
const GUI::ECS::Systems::MouseInfo getMouseInfo() const
This is the function in charge of returning the MouseInfo class instance.
void setGeometry(const Recoded::FloatRect &rect)
Set the dimensions of the object using the FloatRect recode.
Collision(const std::uint32_t entityId=0, const float width=0, const float height=0, const float positionX=0, const float positionY=0)
Constructs a Collision with the specified dimensions and position.
Definition Collision.cpp:24
void setPosition(const std::pair< float, float > &position)
Set the position of the object.
Definition Collision.cpp:84
const float getPositionX() const
Gets the X-coordinate of the component's position.
const bool isHovered() const
Checks if the component is currently hovered by the mouse.
Collision & operator=(const GUI::ECS::Systems::Collision &copy)
This is the overload in charge of allowing the user to update their variables using the = sign.
A generic 2D rectangle class that holds position and size as pairs.
Definition Rect.hpp:38
std::pair< T, T > size
The size of the rectangle (width, height)
Definition Rect.hpp:98
std::pair< T, T > position
the position (top and left corner)
Definition Rect.hpp:97
const Collision operator+(Collision left, Collision right)
Adds two collision components component-wise.
const Collision & operator+=(Collision &left, Collision right)
Adds another collision component to the current collision component component-wise.
const Collision operator-(Collision left, Collision right)
Subtracts two collision components component-wise.
const Collision operator*(Collision left, Collision right)
Multiplies two collision components component-wise.
const bool operator==(Collision left, Collision right)
Compares two collision components for equality.
const bool operator!=(Collision left, Collision right)
Compares two collision components for inequality.
std::ostream & operator<<(std::ostream &os, const Clock &item)
Outputs the clock's info to a stream.
Definition Clock.cpp:73
const Collision & operator*=(Collision &left, Collision right)
Multiplies another collision component with the current collision component component-wise.
const Collision & operator-=(Collision &left, Collision right)
Subtracts another collision component from the current collision component component-wise.
const std::string myToString(const Rect< RectType > &rectangle)
Converts a Rect<T> object to its string representation.
Definition Rect.hpp:223