Separated interfaces

This commit is contained in:
Rendo 2026-03-10 21:51:01 +05:00
commit 9b1acbbe28
3 changed files with 58 additions and 6 deletions

View file

@ -2,6 +2,8 @@
#include <raymath.h> #include <raymath.h>
#include "components.h" #include "components.h"
// Pacman definitions
Rectangle Pacman::getTextureRect(){ Rectangle Pacman::getTextureRect(){
Rectangle result; Rectangle result;
result.x = 0; result.x = 0;
@ -36,7 +38,29 @@ void Pacman::process() {
double angle = PI/2.0*facing; double angle = PI/2.0*facing;
Vector2 direction = {(float)(cos(angle)),(float)(sin(-angle))}; Vector2 direction = {(float)(cos(angle)),(float)(sin(-angle))};
this->position = Vector2Add(this->position,Vector2Scale(direction, delta * (float)speed)); this->position = Vector2Add(this->position,Vector2Scale(direction, delta * (float)speed));
}
void Pacman::draw() {
// Drawing // Drawing
DrawTextureRec(this->texture, this->getTextureRect(), this->position, WHITE); DrawTextureRec(this->texture, this->getTextureRect(), this->position, WHITE);
} }
// Wall definitions
Wall::Wall() {
this->texture = LoadTexture("assets/sprites/walls.png");
}
Wall::~Wall() {
UnloadTexture(this->texture);
}
void Wall::ready() {
// Set neigbors depending on state
}
void Wall::draw() {
// Draw based on neighbors
}

View file

@ -4,13 +4,26 @@
#include <raylib.h> #include <raylib.h>
#include <raymath.h> #include <raymath.h>
class IComponent { class IProcessable {
public: public:
virtual ~IComponent() {} virtual ~IProcessable() {}
// Called every frame
virtual void process() = 0; virtual void process() = 0;
}; };
class IReadible {
public:
virtual ~IReadible() {}
// Called when the level is set up
virtual void ready() = 0;
};
class Pacman : IComponent { class IDrawable {
public:
virtual ~IDrawable() {}
virtual void draw() = 0;
};
class Pacman : public IProcessable, public IDrawable {
const int speed = 64; const int speed = 64;
private: private:
Texture2D texture; Texture2D texture;
@ -23,6 +36,17 @@ class Pacman : IComponent {
Pacman(Vector2 position); Pacman(Vector2 position);
~Pacman(); ~Pacman();
virtual void process(); virtual void process();
virtual void draw();
};
class Wall : public IReadible, public IDrawable {
private:
Texture2D texture;
public:
Wall();
~Wall();
virtual void ready();
virtual void draw();
}; };
#endif #endif

View file

@ -13,11 +13,15 @@ int main() {
Pacman pacman = Pacman(Vector2{screen_width/2.0,screen_height/2.0}); Pacman pacman = Pacman(Vector2{screen_width/2.0,screen_height/2.0});
while (WindowShouldClose() == false) { while (WindowShouldClose() == false) {
pacman.process();
BeginDrawing(); BeginDrawing();
ClearBackground(BLACK); ClearBackground(BLACK);
pacman.process(); pacman.draw();
EndDrawing(); EndDrawing();
} }