Fixed modularity issues

This commit is contained in:
Rendo 2026-03-10 16:13:58 +05:00
commit 7fe086a477
2 changed files with 43 additions and 52 deletions

View file

@ -1,56 +1,42 @@
#include <raylib.h> #include <raylib.h>
#include <raymath.h> #include <raymath.h>
#include "components.h"
class IComponent { Rectangle Pacman::getTextureRect(){
public: Rectangle result;
virtual ~IComponent() {} result.x = 0;
virtual void process() = 0; result.y = facing*16;
}; result.height = 16;
result.width = 16;
return result;
}
Pacman::Pacman() {
this->texture = LoadTexture("assets/sprites/pacman.png");
this->facing = 0;
this->position = {0.,0.};
}
Pacman::Pacman(Vector2 position) {
this->texture = LoadTexture("assets/sprites/pacman.png");
this->facing = 0;
this->position = position;
}
Pacman::~Pacman() {
UnloadTexture(this->texture);
}
void Pacman::process() {
float delta = GetFrameTime();
// Input handling
if (IsKeyDown(KEY_W) || IsKeyDown(KEY_UP)) {this->facing=1;}
if (IsKeyDown(KEY_A) || IsKeyDown(KEY_LEFT)) {this->facing=2;}
if (IsKeyDown(KEY_S) || IsKeyDown(KEY_DOWN)) {this->facing=3;}
if (IsKeyDown(KEY_D) || IsKeyDown(KEY_RIGHT)) {this->facing=0;}
class Pacman : IComponent { // Movement in direction
const int speed = 64; double angle = PI/2.0*facing;
private: Vector2 direction = {(float)(cos(angle)),(float)(sin(-angle))};
Texture2D texture; this->position = Vector2Add(this->position,Vector2Scale(direction, delta * (float)speed));
Vector2 position;
int facing; // Drawing
DrawTextureRec(this->texture, this->getTextureRect(), this->position, WHITE);
Rectangle getTextureRect(){ }
Rectangle result;
result.x = 0;
result.y = facing*16;
result.height = 16;
result.width = 16;
return result;
}
public:
Pacman() {
this->texture = LoadTexture("assets/sprites/pacman.png");
this->facing = 0;
this->position = {0.,0.};
}
Pacman(Vector2 position) {
this->texture = LoadTexture("assets/sprites/pacman.png");
this->facing = 0;
this->position = position;
}
~Pacman() {
UnloadTexture(this->texture);
}
virtual void process() {
float delta = GetFrameTime();
// Input handling
if (IsKeyDown(KEY_W) || IsKeyDown(KEY_UP)) {this->facing=1;}
if (IsKeyDown(KEY_A) || IsKeyDown(KEY_LEFT)) {this->facing=2;}
if (IsKeyDown(KEY_S) || IsKeyDown(KEY_DOWN)) {this->facing=3;}
if (IsKeyDown(KEY_D) || IsKeyDown(KEY_RIGHT)) {this->facing=0;}
// Movement in direction
double angle = PI/2.0*facing;
Vector2 direction = {(float)(cos(angle)),(float)(sin(-angle))};
this->position = Vector2Add(this->position,Vector2Scale(direction, delta * (float)speed));
// Drawing
DrawTextureRec(this->texture, this->getTextureRect(), this->position, WHITE);
}
};

View file

@ -1,3 +1,6 @@
#ifndef COMPONENTS_H
#define COMPONENTS_H
#include <raylib.h> #include <raylib.h>
#include <raymath.h> #include <raymath.h>
@ -21,3 +24,5 @@ class Pacman : IComponent {
~Pacman(); ~Pacman();
virtual void process(); virtual void process();
}; };
#endif