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 "components.h"
// Pacman definitions
Rectangle Pacman::getTextureRect(){
Rectangle result;
result.x = 0;
@ -36,7 +38,29 @@ void Pacman::process() {
double angle = PI/2.0*facing;
Vector2 direction = {(float)(cos(angle)),(float)(sin(-angle))};
this->position = Vector2Add(this->position,Vector2Scale(direction, delta * (float)speed));
}
void Pacman::draw() {
// Drawing
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 <raymath.h>
class IComponent {
class IProcessable {
public:
virtual ~IComponent() {}
virtual ~IProcessable() {}
// Called every frame
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;
private:
Texture2D texture;
@ -23,6 +36,17 @@ class Pacman : IComponent {
Pacman(Vector2 position);
~Pacman();
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

View file

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