0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 12:33:43 +00:00

fixed walk/idle animation

This commit is contained in:
Benedikt Galbavy 2024-01-24 01:50:33 +01:00
parent 76f4ae79ed
commit 3766fefb13
4 changed files with 23 additions and 21 deletions

View File

@ -18,6 +18,6 @@ struct Animation
enum AnimationType
{
idle = 0,
walk = 1
IDLE = 0,
WALK = 1
};

View File

@ -10,6 +10,3 @@
#define MAP_SIZE_X 25
#define MAP_SIZE_Y 20
#define IDLE "idle"
#define WALK "walk"

View File

@ -11,8 +11,18 @@ class SpriteComponent : public Component
public:
int animationIndex = 0;
std::map<const char*, Animation> animations;
std::map<AnimationType, Animation*> 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;
};

View File

@ -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);
}
}