diff --git a/assets/chicken_knight_spritesheet.png b/assets/chicken_knight_spritesheet.png index e9bd505..2304ae6 100644 Binary files a/assets/chicken_knight_spritesheet.png and b/assets/chicken_knight_spritesheet.png differ diff --git a/assets/chicken_spritesheet.png b/assets/chicken_spritesheet.png index 0e69278..ba003eb 100644 Binary files a/assets/chicken_spritesheet.png and b/assets/chicken_spritesheet.png differ diff --git a/include/AnimationHandler.h b/include/AnimationHandler.h index 1fc1024..6fc8081 100644 --- a/include/AnimationHandler.h +++ b/include/AnimationHandler.h @@ -19,8 +19,7 @@ struct Animation enum AnimationType //TODO enum class { IDLE = 0, - WALK_R = 1, - WALK_L = 2 + WALK = 1 }; diff --git a/include/SpriteComponent.h b/include/SpriteComponent.h index d110922..109d824 100644 --- a/include/SpriteComponent.h +++ b/include/SpriteComponent.h @@ -8,6 +8,12 @@ class TransformComponent; +enum SpriteDirection +{ + LEFT, + RIGHT +}; + class SpriteComponent : public Component { public: @@ -23,6 +29,7 @@ private: bool animated = false; uint8_t frames = 0; uint8_t speed = 100; + bool flipped; public: SpriteComponent() = default; @@ -35,5 +42,6 @@ public: void init() override; void update() override; void draw() override; - void play(AnimationType type); + void playAnimation(AnimationType type); + void setDirection(SpriteDirection direction); }; diff --git a/include/TextureManager.h b/include/TextureManager.h index 4b6e0c1..5721f67 100644 --- a/include/TextureManager.h +++ b/include/TextureManager.h @@ -36,5 +36,6 @@ class TextureManager SDL_Texture* loadTexture(const char* fileName); static std::vector splitSpriteSheet(SDL_Texture* spriteSheet, int width, int height, int spritesOnSheet); - static void draw(SDL_Texture* texture, SDL_Rect src, SDL_Rect dest); + static void draw(SDL_Texture* texture, SDL_Rect src, SDL_Rect dest); // defaults to flipped false -> legacy + static void draw(SDL_Texture* texture, SDL_Rect src, SDL_Rect dest, bool flipped); }; \ No newline at end of file diff --git a/src/KeyboardController.cpp b/src/KeyboardController.cpp index 6c7fc5a..2880fe0 100644 --- a/src/KeyboardController.cpp +++ b/src/KeyboardController.cpp @@ -3,6 +3,7 @@ #include "Game.h" #include "Components.h" #include "AssetManager.h" +#include "SpriteComponent.h" KeyboardController::KeyboardController(SDL_Scancode up, SDL_Scancode down, SDL_Scancode left, SDL_Scancode right, SDL_Scancode fire, Vector2D fireVelocity) { @@ -24,23 +25,25 @@ void KeyboardController::update() { transform->velocity.x = 0; transform->velocity.y = 0; - sprite->play(IDLE); + sprite->playAnimation(IDLE); if (keystates[this->up]) { transform->velocity.y = -1; - sprite->play(WALK_R); + sprite->playAnimation(WALK); } if (keystates[this->left]) { transform->velocity.x = -1; - sprite->play(WALK_L); + sprite->playAnimation(WALK); + sprite->setDirection(LEFT); } if (keystates[this->down]) { transform->velocity.y = 1; - sprite->play(WALK_R); + sprite->playAnimation(WALK); } if (keystates[this->right]) { transform->velocity.x = 1; - sprite->play(WALK_R); + sprite->playAnimation(WALK); + sprite->setDirection(RIGHT); } if (keystates[this->fire]) { @@ -54,10 +57,12 @@ void KeyboardController::update() //checks player source via the firing velocity //TODO: adding actual projectile textures if (fireVelocity.x > 0) { + sprite->setDirection(RIGHT); Game::assets->createProjectile(Vector2D(player->position.x, player->position.y), fireVelocity, false, 1, 180, 1, "assets/egg.png"); } else { + sprite->setDirection(LEFT); Game::assets->createProjectile(Vector2D(player->position.x, player->position.y), fireVelocity, true, 1, 180, 1, "assets/egg.png"); } diff --git a/src/SpriteComponent.cpp b/src/SpriteComponent.cpp index c0a279b..6af7d6b 100644 --- a/src/SpriteComponent.cpp +++ b/src/SpriteComponent.cpp @@ -16,14 +16,12 @@ SpriteComponent::SpriteComponent(const char* path, bool isAnimated) animated = isAnimated; Animation* idle = new Animation((uint8_t)AnimationType::IDLE, 2, 200); - Animation* walkR = new Animation((uint8_t)AnimationType::WALK_R, 2, 200); - Animation* walkL = new Animation((uint8_t)AnimationType::WALK_L, 2, 200); + Animation* walk = new Animation((uint8_t)AnimationType::WALK, 2, 200); animations.emplace(IDLE, idle); - animations.emplace(WALK_R, walkR); - animations.emplace(WALK_L, walkL); + animations.emplace(WALK, walk); - play(IDLE); + playAnimation(IDLE); setTexture(path); } @@ -63,12 +61,22 @@ void SpriteComponent::update() void SpriteComponent::draw() { - TextureManager::get().draw(this->texture, this->srcRect, this->destRect); + TextureManager::get().draw(this->texture, this->srcRect, this->destRect, this->animated && this->flipped); } -void SpriteComponent::play(AnimationType type) +void SpriteComponent::playAnimation(AnimationType type) { this->animationIndex = animations.at(type)->index; this->frames = animations.at(type)->frames; this->speed = animations.at(type)->speed; } + +void SpriteComponent::setDirection(SpriteDirection direction) +{ + if (direction == RIGHT) { + this->flipped = true; + return; + } + + this->flipped = false; +} \ No newline at end of file diff --git a/src/TextureManager.cpp b/src/TextureManager.cpp index b364214..13814bf 100644 --- a/src/TextureManager.cpp +++ b/src/TextureManager.cpp @@ -20,5 +20,11 @@ SDL_Texture* TextureManager::loadTexture(const char* fileName) void TextureManager::draw(SDL_Texture* texture, SDL_Rect src, SDL_Rect dest) { - SDL_RenderCopy(Game::renderer, texture, &src, &dest); + draw(texture, src, dest, false); +} + +void TextureManager::draw(SDL_Texture* texture, SDL_Rect src, SDL_Rect dest, bool flipped) +{ + SDL_RendererFlip flip = flipped ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE; + SDL_RenderCopyEx(Game::renderer, texture, &src, &dest, 0, NULL, flip); } \ No newline at end of file