From 7fe086a47760bb1b92993f2e16b92c83f3706d00 Mon Sep 17 00:00:00 2001 From: Rendo Date: Tue, 10 Mar 2026 16:13:58 +0500 Subject: [PATCH] Fixed modularity issues --- src/components.cpp | 90 ++++++++++++++++++++-------------------------- src/components.h | 5 +++ 2 files changed, 43 insertions(+), 52 deletions(-) diff --git a/src/components.cpp b/src/components.cpp index 2eccc6d..15413f6 100644 --- a/src/components.cpp +++ b/src/components.cpp @@ -1,56 +1,42 @@ #include #include +#include "components.h" -class IComponent { - public: - virtual ~IComponent() {} - virtual void process() = 0; -}; +Rectangle Pacman::getTextureRect(){ + Rectangle result; + result.x = 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 { - 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); - } -}; + // 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); +} diff --git a/src/components.h b/src/components.h index ee8e70b..d515a45 100644 --- a/src/components.h +++ b/src/components.h @@ -1,3 +1,6 @@ +#ifndef COMPONENTS_H +#define COMPONENTS_H + #include #include @@ -21,3 +24,5 @@ class Pacman : IComponent { ~Pacman(); virtual void process(); }; + +#endif