5#ifndef CASSE_BRIQUE_COLLIDINGOBJECT_H
6#define CASSE_BRIQUE_COLLIDINGOBJECT_H
11#include <SDL2/SDL_timer.h>
57 return (
x <= num &&
y <= num);
67 return {
x + other.
x,
y + other.
y};
77 return {
x - other.
x,
y - other.
y};
87 return {
x * f,
y * f};
126 [[nodiscard]]
virtual std::tuple<bool, float, float>
collided(
const Collider& other)
const = 0;
133 [[nodiscard]]
virtual std::string
getType()
const = 0;
142 virtual void render(std::shared_ptr<SDL_Renderer> renderer, SDL_Color color1, SDL_Color color2) = 0;
167 this->
id = std::move(
id);
176 [[nodiscard]] std::tuple<bool, float, float>
collided(
const Collider& other)
const override;
183 [[nodiscard]] std::string
getType()
const override;
192 void render(std::shared_ptr<SDL_Renderer> renderer, SDL_Color color1, SDL_Color color2)
override;
222 corner.push_back(p1);
233 [[nodiscard]] std::tuple<bool, float, float>
collided(
const Collider& other)
const override;
240 [[nodiscard]] std::string
getType()
const override;
247 void fillTriangle(
const std::shared_ptr<SDL_Renderer>& renderer);
256 void render(std::shared_ptr<SDL_Renderer> renderer, SDL_Color color1, SDL_Color color2)
override;
291 [[nodiscard]] std::tuple<bool, float, float>
collided(
const Collider& other)
const override;
298 [[nodiscard]] std::string
getType()
const override;
305 void fillHexagon(
const std::shared_ptr<SDL_Renderer>& renderer);
314 void render(std::shared_ptr<SDL_Renderer> renderer, SDL_Color color1, SDL_Color color2)
override;
336 this->
id = std::move(
id);
353 [[nodiscard]] std::tuple<bool, float, float>
collided(
const Collider& other)
const override;
360 [[nodiscard]] std::string
getType()
const override;
369 void render(std::shared_ptr<SDL_Renderer> renderer, SDL_Color color1, SDL_Color color2)
override;
391 collider = std::make_shared<CircleCollider>(
392 point({
static_cast<float>(x),
static_cast<float>(y)}),radius,
id);
405 collider = std::make_shared<RectCollider>(
406 point({
static_cast<float>(x),
static_cast<float>(y)}),width,height,
id);
419 CollidingObject(
int x,
int y,
int width,
int height,
bool isTop,
const std::string&
id){
420 point p{
static_cast<float>(x+width/2),
static_cast<float>(y+height/2)};
421 point p1{},p2{},p3{};
423 p1 = {
static_cast<float>(x+width/2),
static_cast<float>(y)};
424 p2 = {
static_cast<float>(x),
static_cast<float>(y+height)};
425 p3 = {
static_cast<float>(x+width),
static_cast<float>(y+height)};
427 p1 = {
static_cast<float>(x+width/2),
static_cast<float>(y+height)};
428 p2 = {
static_cast<float>(x),
static_cast<float>(y)};
429 p3 = {
static_cast<float>(x+width),
static_cast<float>(y)};
431 collider = std::make_shared<TriangleCollider>(p,width,height,p1,p2,p3,
id);
444 CollidingObject(
int x,
int y,
int width,
int height,
int edges,
const std::string&
id){
445 point p{
static_cast<float>(x),
static_cast<float>(y)};
446 std::vector<point> corner;
447 for (
int i=0; i<edges; i++) {
448 double angleRad = 2*M_PI / edges*i;
449 int xpos = x+(width/2)*cos(angleRad);
450 int ypos = y+(width/2)*sin(angleRad);
452 corner.push_back({
static_cast<float>(xpos),
static_cast<float>(ypos)});
454 collider = std::make_shared<HexagonCollider>(p, height, width, corner,
id);
478 virtual void render(std::shared_ptr<SDL_Renderer> renderer, SDL_Color color1, SDL_Color color2) = 0;
512 MovingObject(
int x,
int y,
int width,
int height,
const std::string&
id)
The CircleCollider class represents a circular collider object.
int getRadius() const
Gets the radius of the circular collider.
std::string getType() const override
Gets the type of the collider.
int radius
The radius of the circular collider.
std::tuple< bool, float, float > collided(const Collider &other) const override
Checks for collision with another collider.
void render(std::shared_ptr< SDL_Renderer > renderer, SDL_Color color1, SDL_Color color2) override
Renders the collider on the screen.
CircleCollider(point mid, int radius, std::string id)
Constructs a new CircleCollider object with specified parameters.
The Collider class represents a generic collider object.
void moveTo(point newPoint)
Moves the collider to an absolute position.
virtual std::tuple< bool, float, float > collided(const Collider &other) const =0
Checks for collision with another collider.
std::string id
The unique identifier of the collider.
virtual ~Collider()=default
Virtual destructor for the Collider class.
virtual std::string getType() const =0
Gets the type of the collider.
virtual void render(std::shared_ptr< SDL_Renderer > renderer, SDL_Color color1, SDL_Color color2)=0
Renders the collider on the screen.
point p
The position of the collider.
void move(point newPoint)
Moves the collider to a new position.
The CollidingObject class represents a generic colliding object.
virtual void handleCollisions(point collision)=0
Handles collisions with other objects.
std::shared_ptr< Collider > collider
The collider associated with the object.
std::shared_ptr< Collider > getCollider()
Gets the collider associated with the object.
CollidingObject(int x, int y, int width, int height, int edges, const std::string &id)
Constructs a new CollidingObject with a hexagonal collider.
CollidingObject(int x, int y, int width, int height, bool isTop, const std::string &id)
Constructs a new CollidingObject with a triangular collider.
CollidingObject(int x, int y, int radius, const std::string &id)
Constructs a new CollidingObject with a circular collider.
CollidingObject(int x, int y, int width, int height, const std::string &id)
Constructs a new CollidingObject with a rectangular collider.
virtual void render(std::shared_ptr< SDL_Renderer > renderer, SDL_Color color1, SDL_Color color2)=0
Renders the object on the screen.
The HexagonCollider class represents a hexagonal collider object.
HexagonCollider(point p, int h, int w, std::vector< point > corner, std::string id)
Constructs a new HexagonCollider object with specified parameters.
int height
The height of the hexagonal collider.
int width
The width of the hexagonal collider.
void render(std::shared_ptr< SDL_Renderer > renderer, SDL_Color color1, SDL_Color color2) override
Renders the collider on the screen.
std::tuple< bool, float, float > collided(const Collider &other) const override
Checks for collision with another collider.
std::vector< point > corner
The vertices of the hexagonal collider.
void fillHexagon(const std::shared_ptr< SDL_Renderer > &renderer)
Fills the hexagon shape with the current renderer.
std::string getType() const override
Gets the type of the collider.
The MovingObject class represents a moving colliding object.
virtual void move()=0
Moves the object.
point Velocity
The velocity of the object.
MovingObject(int x, int y, int width, int height, const std::string &id)
Constructs a new MovingObject with a rectangular collider.
MovingObject(int x, int y, int radius, const std::string &id)
Constructs a new MovingObject with a circular collider.
The RectCollider class represents a rectangular collider object.
RectCollider(point mid, int width, int height, std::string id)
Constructs a new RectCollider object with specified parameters.
int width
The width of the rectangular collider.
std::tuple< bool, float, float > collided(const Collider &other) const override
Checks for collision with another collider.
void render(std::shared_ptr< SDL_Renderer > renderer, SDL_Color color1, SDL_Color color2) override
Renders the collider on the screen.
std::string getType() const override
Gets the type of the collider.
int height
The height of the rectangular collider.
The TriangleCollider class represents a triangular collider object.
std::string getType() const override
Gets the type of the collider.
std::vector< point > corner
The vertices of the triangular collider.
TriangleCollider(point p, int h, int w, point p1, point p2, point p3, std::string id)
Constructs a new TriangleCollider object with specified parameters.
int width
The width of the triangular collider.
int height
The height of the triangular collider.
void fillTriangle(const std::shared_ptr< SDL_Renderer > &renderer)
Fills the triangle shape with the current renderer.
std::tuple< bool, float, float > collided(const Collider &other) const override
Checks for collision with another collider.
void render(std::shared_ptr< SDL_Renderer > renderer, SDL_Color color1, SDL_Color color2) override
Renders the collider on the screen.
The point struct represents a point in 2D space.
point & operator+=(const point &other)
Overloaded compound assignment operator for addition.
const bool operator<=(const float num) const
Overloaded less than or equal to comparison operator.
float y
The y-coordinate of the point.
point & operator*=(const point &other)
Overloaded compound assignment operator for element-wise multiplication.
float x
The x-coordinate of the point.
point operator+(const point &other)
Overloaded addition operator.
point operator*(float f)
Overloaded multiplication operator.
point operator-(const point &other)
Overloaded subtraction operator.