casse-brick latest
Ce projet est réalisé dans le cadre du cours de Programmation Avancée en M1 Informatique de l'université de Strasbourg.
Loading...
Searching...
No Matches
CollidingObject.h
Go to the documentation of this file.
1//
2// Created by nicolas elfering on 08.05.24.
3//
4
5#ifndef CASSE_BRIQUE_COLLIDINGOBJECT_H
6#define CASSE_BRIQUE_COLLIDINGOBJECT_H
7
8#include <utility>
9#include <SDL2/SDL.h>
10#include <SDL_image.h>
11#include <SDL2/SDL_timer.h>
12#include <memory>
13#include <algorithm>
14#include <vector>
15#include "iostream"
16
22struct point {
23 float x;
24 float y;
25
32 point& operator+=(const point& other) {
33 x += other.x;
34 y += other.y;
35 return *this;
36 }
37
44 point& operator*=(const point& other) {
45 x *= other.x;
46 y *= other.y;
47 return *this;
48 }
49
56 const bool operator <= (const float num) const {
57 return (x <= num && y <= num);
58 }
59
66 point operator+(const point& other) {
67 return {x + other.x, y + other.y};
68 }
69
76 point operator-(const point& other) {
77 return {x - other.x, y - other.y};
78 }
79
86 point operator*(float f) {
87 return {x * f, y * f};
88 }
89};
90
96class Collider {
97public:
98 std::string id;
100
106 void move(point newPoint);
107
113 void moveTo(point newPoint);
114
118 virtual ~Collider() = default;
119
126 [[nodiscard]] virtual std::tuple<bool, float, float> collided(const Collider& other) const = 0;
127
133 [[nodiscard]] virtual std::string getType() const = 0;
134
142 virtual void render(std::shared_ptr<SDL_Renderer> renderer, SDL_Color color1, SDL_Color color2) = 0;
143};
144
145
151class RectCollider : public Collider {
152public:
153 int height;
154 int width;
155
164 RectCollider(point mid, int width, int height, std::string id)
165 : width(width), height(height) {
166 p = mid;
167 this->id = std::move(id);
168 };
169
176 [[nodiscard]] std::tuple<bool, float, float> collided(const Collider& other) const override;
177
183 [[nodiscard]] std::string getType() const override;
184
192 void render(std::shared_ptr<SDL_Renderer> renderer, SDL_Color color1, SDL_Color color2) override;
193};
194
195
202public:
203 std::vector<point> corner;
204 int height;
205 int width;
206
218 [[maybe_unused]] TriangleCollider(point p, int h, int w, point p1, point p2, point p3, std::string id)
219 : height(h), width(w) {
220 this->p = p;
221 this->id = id;
222 corner.push_back(p1);
223 corner.push_back(p2);
224 corner.push_back(p3);
225 };
226
233 [[nodiscard]] std::tuple<bool, float, float> collided(const Collider& other) const override;
234
240 [[nodiscard]] std::string getType() const override;
241
247 void fillTriangle(const std::shared_ptr<SDL_Renderer>& renderer);
248
256 void render(std::shared_ptr<SDL_Renderer> renderer, SDL_Color color1, SDL_Color color2) override;
257};
258
259
265class HexagonCollider : public Collider {
266public:
267 std::vector<point> corner;
268 int height;
269 int width;
270
280 [[maybe_unused]] HexagonCollider(point p, int h, int w, std::vector<point> corner, std::string id)
281 : height(h), width(w), corner(std::move(corner)) {
282 this->p=p;
283 }
284
291 [[nodiscard]] std::tuple<bool, float, float> collided(const Collider& other) const override;
292
298 [[nodiscard]] std::string getType() const override;
299
305 void fillHexagon(const std::shared_ptr<SDL_Renderer>& renderer);
306
314 void render(std::shared_ptr<SDL_Renderer> renderer, SDL_Color color1, SDL_Color color2) override;
315};
316
317
323class CircleCollider : public Collider {
324private:
325 int radius;
326
327public:
335 [[maybe_unused]] CircleCollider(point mid, int radius, std::string id) : radius(radius) {
336 this->id = std::move(id);
337 p=mid;
338 };
339
345 int getRadius() const;
346
353 [[nodiscard]] std::tuple<bool, float, float> collided(const Collider& other) const override;
354
360 [[nodiscard]] std::string getType() const override;
361
369 void render(std::shared_ptr<SDL_Renderer> renderer, SDL_Color color1, SDL_Color color2) override;
370};
371
378protected:
379 std::shared_ptr<Collider> collider;
380
381public:
390 CollidingObject(int x, int y, int radius, const std::string& id){
391 collider = std::make_shared<CircleCollider>(
392 point({static_cast<float>(x),static_cast<float>(y)}),radius,id);
393 };
394
404 CollidingObject(int x, int y, int width, int height, const std::string& id){
405 collider = std::make_shared<RectCollider>(
406 point({static_cast<float>(x),static_cast<float>(y)}),width,height,id);
407 };
408
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{};
422 if (isTop) {
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)};
426 } else {
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)};
430 }
431 collider = std::make_shared<TriangleCollider>(p,width,height,p1,p2,p3,id);
432 };
433
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);
451
452 corner.push_back({static_cast<float>(xpos),static_cast<float>(ypos)});
453 }
454 collider = std::make_shared<HexagonCollider>(p, height, width, corner, id);
455 };
456
462 std::shared_ptr<Collider> getCollider();
463
469 [[maybe_unused]] virtual void handleCollisions(point collision) = 0;
470
478 virtual void render(std::shared_ptr<SDL_Renderer> renderer, SDL_Color color1, SDL_Color color2) = 0;
479};
480
487public:
489
498 MovingObject(int x, int y, int radius, const std::string& id)
499 : CollidingObject(x, y,radius,id), Velocity({0,0}){
500
501 };
502
512 MovingObject(int x, int y, int width, int height, const std::string& id)
513 : CollidingObject(x, y, width, height, id), Velocity({0,0}) {
514
515 };
516
522 virtual void move() = 0;
523};
524
525
526
527
528
529#endif //CASSE_BRIQUE_COLLIDINGOBJECT_H
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.