modularity problem

This commit is contained in:
Rendo 2026-03-10 15:20:40 +05:00
commit b628beafb9
4 changed files with 74 additions and 57 deletions

56
src/components.cpp Normal file
View file

@ -0,0 +1,56 @@
#include <raylib.h>
#include <raymath.h>
class IComponent {
public:
virtual ~IComponent() {}
virtual void process() = 0;
};
class Pacman : IComponent {
const int speed = 64;
private:
Texture2D texture;
Vector2 position;
int facing;
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 @@
#include <raylib.h>
#include <raymath.h>
class IComponent {
public:
virtual ~IComponent() {}
@ -5,8 +8,16 @@ class IComponent {
};
class Pacman : IComponent {
public:
virtual void process() {
const int speed = 64;
private:
Texture2D texture;
Vector2 position;
int facing;
}
Rectangle getTextureRect();
public:
Pacman();
Pacman(Vector2 position);
~Pacman();
virtual void process();
};

View file

@ -1,57 +1,6 @@
#include <raylib.h>
#include <cmath>
#include <raymath.h>
// Player controlled entity
class Pacman {
const int speed = 64;
private:
Texture2D texture;
Vector2 position;
int facing;
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);
}
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);
}
};
#include "components.h"
int main() {
const int screen_height = 320;