diff --git a/assets/sprites/pacman.png b/assets/sprites/pacman.png index 63e7438..25dfc88 100644 Binary files a/assets/sprites/pacman.png and b/assets/sprites/pacman.png differ diff --git a/src/components.cpp b/src/components.cpp index 84addd9..c9d40d7 100644 --- a/src/components.cpp +++ b/src/components.cpp @@ -7,7 +7,7 @@ Rectangle Pacman::getTextureRect() const{ Rectangle result; - result.x = 0; + result.x = 16*frame; result.y = facing*16; result.height = 16; result.width = 16; @@ -17,6 +17,7 @@ Pacman::Pacman() { this->texture = LoadTexture("assets/sprites/pacman.png"); this->facing = 0; this->position = {0.,0.}; + this->frame = 0; } Pacman::Pacman(Vector2 position) { this->texture = LoadTexture("assets/sprites/pacman.png"); @@ -53,6 +54,18 @@ void Pacman::tick() { } } +void Pacman::process() { + this->time += GetFrameTime(); + if (this->time >= 1.0/this->fps){ + this->time = 0.; + this->animation_tick(); + } +} + +void Pacman::animation_tick() { + this->frame = (this->frame+1)%frame_count; +} + void Pacman::draw() const { // Drawing DrawTextureRec(this->texture, this->getTextureRect(), this->position, WHITE); diff --git a/src/components.h b/src/components.h index 2b31aca..2db18e3 100644 --- a/src/components.h +++ b/src/components.h @@ -19,16 +19,22 @@ class Entity{ class Pacman : public Entity{ const int speed = 16; + const int frame_count = 7; + const int fps = 24; private: Texture2D texture; int facing; - Rectangle getTextureRect() const; public: + unsigned char frame; + float time; + Pacman(); Pacman(Vector2 position); ~Pacman(); void tick() override; + void process() override; + void animation_tick(); void draw() const override; }; diff --git a/src/main.cpp b/src/main.cpp index fb2b3f9..ba4d5b1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,7 +12,7 @@ int main() { SetTargetFPS(60); - world = create_world_with(.25); + world = create_world_with(1./10.); world.entities.push_back(new Pacman({SCREEN_WIDTH/2,SCREEN_HEIGHT/2}));