From 3766fefb132330d42c3717a64cf622b2339123a8 Mon Sep 17 00:00:00 2001 From: Benedikt Galbavy Date: Wed, 24 Jan 2024 01:50:33 +0100 Subject: [PATCH] fixed walk/idle animation --- include/AnimationHandler.h | 4 ++-- include/Defines.h | 3 --- include/SpriteComponent.h | 33 +++++++++++++++++---------------- src/KeyboardController.cpp | 4 ++++ 4 files changed, 23 insertions(+), 21 deletions(-) diff --git a/include/AnimationHandler.h b/include/AnimationHandler.h index ea77855..0473651 100644 --- a/include/AnimationHandler.h +++ b/include/AnimationHandler.h @@ -18,6 +18,6 @@ struct Animation enum AnimationType { - idle = 0, - walk = 1 + IDLE = 0, + WALK = 1 }; \ No newline at end of file diff --git a/include/Defines.h b/include/Defines.h index 3ee280a..da72770 100644 --- a/include/Defines.h +++ b/include/Defines.h @@ -10,6 +10,3 @@ #define MAP_SIZE_X 25 #define MAP_SIZE_Y 20 -#define IDLE "idle" -#define WALK "walk" - diff --git a/include/SpriteComponent.h b/include/SpriteComponent.h index c3b272e..e3fadb1 100644 --- a/include/SpriteComponent.h +++ b/include/SpriteComponent.h @@ -11,8 +11,18 @@ class SpriteComponent : public Component public: int animationIndex = 0; - std::map animations; + std::map animations; + private: + TransformComponent* transform; + SDL_Texture* texture; + SDL_Rect srcRect, destRect; + + bool animated = false; + int frames = 0; + int speed = 100; + + public: SpriteComponent() = default; SpriteComponent(const char* path) { @@ -23,8 +33,8 @@ class SpriteComponent : public Component { animated = isAnimated; - Animation idle = Animation((int)AnimationType::idle, 2, 200); - Animation walk = Animation((int)AnimationType::walk, 2, 200); + Animation* idle = new Animation((int)AnimationType::IDLE, 2, 200); + Animation* walk = new Animation((int)AnimationType::WALK, 2, 200); animations.emplace(IDLE, idle); animations.emplace(WALK, walk); @@ -75,19 +85,10 @@ class SpriteComponent : public Component TextureManager::get().draw(this->texture, this->srcRect, this->destRect); } - void play(const char* animationName) + void play(AnimationType type) { - animationIndex = animations[animationName].index; - frames = animations[animationName].frames; - speed = animations[animationName].speed; + animationIndex = animations.at(type)->index; + frames = animations.at(type)->frames; + speed = animations.at(type)->speed; } - - private: - TransformComponent* transform; - SDL_Texture* texture; - SDL_Rect srcRect, destRect; - - bool animated = false; - int frames = 0; - int speed = 100; }; diff --git a/src/KeyboardController.cpp b/src/KeyboardController.cpp index e5e367e..8603df5 100644 --- a/src/KeyboardController.cpp +++ b/src/KeyboardController.cpp @@ -28,6 +28,7 @@ void KeyboardController::update() { transform->velocity.x = 0; transform->velocity.y = 0; + sprite->play(IDLE); if (keystates[this->up]) { transform->velocity.y = -1; @@ -35,11 +36,14 @@ void KeyboardController::update() } if (keystates[this->left]) { transform->velocity.x = -1; + sprite->play(WALK); } if (keystates[this->down]) { transform->velocity.y = 1; + sprite->play(WALK); } if (keystates[this->right]) { transform->velocity.x = 1; + sprite->play(WALK); } } \ No newline at end of file