From 9ff73a95e3652e0b6c8ee924a72aa9372d274b2a Mon Sep 17 00:00:00 2001 From: theoleuthardt Date: Thu, 13 Mar 2025 10:55:01 +0100 Subject: [PATCH] feat: cactus class for drawing, updating and spawning the cacti on the desert --- src/Cactus.cpp | 22 ++++++++++++++++++++++ src/Cactus.hpp | 19 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 src/Cactus.cpp create mode 100644 src/Cactus.hpp diff --git a/src/Cactus.cpp b/src/Cactus.cpp new file mode 100644 index 0000000..3d81124 --- /dev/null +++ b/src/Cactus.cpp @@ -0,0 +1,22 @@ +#include "Cactus.hpp" + +Cactus::Cactus(Texture2D texture, float x) : texture(texture) { + rect = {static_cast(x), static_cast(300 - texture.height), + static_cast(texture.width), static_cast(texture.height)}; +} + +void Cactus::Update() { + rect.x -= SPEED; +} + +void Cactus::Draw() { + DrawTexture(texture, rect.x, rect.y, WHITE); +} + +bool Cactus::IsOffScreen() { + return rect.x + rect.width < 0; +} + +Rectangle Cactus::GetRect() { + return rect; +} diff --git a/src/Cactus.hpp b/src/Cactus.hpp new file mode 100644 index 0000000..7bdfe90 --- /dev/null +++ b/src/Cactus.hpp @@ -0,0 +1,19 @@ +#ifndef CACTUS_H +#define CACTUS_H +#include "raylib.h" + +class Cactus { +public: + Cactus(Texture2D texture, float x); + void Update(); + void Draw(); + bool IsOffScreen(); + Rectangle GetRect(); + +private: + Texture2D texture; + Rectangle rect; + static constexpr float SPEED = 5; +}; + +#endif \ No newline at end of file