diff --git a/assets/chicken_knight_spritesheet.png b/assets/chicken_knight_spritesheet.png new file mode 100644 index 0000000..9e6939d Binary files /dev/null and b/assets/chicken_knight_spritesheet.png differ diff --git a/assets/chicken_spritesheet.png b/assets/chicken_spritesheet.png new file mode 100644 index 0000000..ba003eb Binary files /dev/null and b/assets/chicken_spritesheet.png differ diff --git a/include/AnimationHandler.h b/include/AnimationHandler.h new file mode 100644 index 0000000..0473651 --- /dev/null +++ b/include/AnimationHandler.h @@ -0,0 +1,23 @@ +#pragma once + +struct Animation +{ + int index; + int frames; + int speed; + + Animation() {} + + Animation(int i, int f, int s) + { + index = i; + frames = f; + speed = s; + } +}; + +enum AnimationType +{ + IDLE = 0, + WALK = 1 +}; \ No newline at end of file diff --git a/include/KeyboardController.h b/include/KeyboardController.h index 69ec780..b353183 100644 --- a/include/KeyboardController.h +++ b/include/KeyboardController.h @@ -1,9 +1,10 @@ #pragma once -#include "SDL.h" +#include #include "Component.h" #include "Vector2D.h" class TransformComponent; +class SpriteComponent; class KeyboardController : public Component { @@ -16,6 +17,8 @@ public: SDL_Scancode right; SDL_Scancode fire; + SpriteComponent* sprite; + //for attack cooldown in between shots Uint32 lastFireTime; Uint32 fireCooldown = 1000; //in ms can be adjusted to change possible attack-speed diff --git a/include/SpriteComponent.h b/include/SpriteComponent.h index 69610d7..18b849d 100644 --- a/include/SpriteComponent.h +++ b/include/SpriteComponent.h @@ -1,14 +1,31 @@ #pragma once -#include "SDL.h" +#include "AnimationHandler.h" +#include "Component.h" #include "Game.h" +#include class TransformComponent; class SpriteComponent : public Component { + public: + int animationIndex = 0; + + 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); + SpriteComponent(const char* path, bool isAnimated); ~SpriteComponent(); void setTexture(const char* path); @@ -16,10 +33,5 @@ class SpriteComponent : public Component void init() override; void update() override; void draw() override; - - private: - TransformComponent* transform; - SDL_Texture* texture; - SDL_Rect srcRect; - SDL_Rect destRect; + void play(AnimationType type); }; diff --git a/include/TextureManager.h b/include/TextureManager.h index a90c103..4b6e0c1 100644 --- a/include/TextureManager.h +++ b/include/TextureManager.h @@ -1,7 +1,8 @@ #pragma once -#include "SDL_render.h" +#include #include +#include struct cmp_str { @@ -34,5 +35,6 @@ class TextureManager std::map texture_cache; 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); }; \ No newline at end of file diff --git a/src/Game.cpp b/src/Game.cpp index 7533925..40aebc1 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -74,14 +74,14 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo //ecs implementation player.addComponent(80,80,2); //posx, posy, scale - player.addComponent("assets/chicken_neutral_knight.png"); //adds sprite (32x32px), path needed + player.addComponent("assets/chicken_knight_spritesheet.png", true); //adds sprite (32x32px), path needed player.addComponent(SDL_SCANCODE_W, SDL_SCANCODE_S, SDL_SCANCODE_A, SDL_SCANCODE_D, SDL_SCANCODE_E, Vector2D(1, 0));//custom keycontrols can be added player.addComponent("player"); //adds tag (for further use, reference tag) player.addComponent(5, &manager, true); player.addGroup((size_t)GroupLabel::PLAYERS); //tell programm what group it belongs to for rendering order enemy.addComponent(600, 500, 2); - enemy.addComponent("assets/chicken_neutral.png"); + enemy.addComponent("assets/chicken_spritesheet.png", true); enemy.addComponent(SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_RCTRL, Vector2D(-1, 0)); enemy.addComponent("enemy"); enemy.addComponent(5, &manager, false); diff --git a/src/KeyboardController.cpp b/src/KeyboardController.cpp index 15f3c96..698876b 100644 --- a/src/KeyboardController.cpp +++ b/src/KeyboardController.cpp @@ -2,6 +2,7 @@ #include "TransformComponent.h" #include "Entity.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) { @@ -15,6 +16,7 @@ KeyboardController::KeyboardController(SDL_Scancode up, SDL_Scancode down, SDL_S void KeyboardController::init() { + sprite = &entity->getComponent(); transform = &entity->getComponent(); } @@ -22,18 +24,23 @@ void KeyboardController::update() { transform->velocity.x = 0; transform->velocity.y = 0; + sprite->play(IDLE); if (keystates[this->up]) { transform->velocity.y = -1; + sprite->play(WALK); } 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); } if (keystates[this->fire]) { diff --git a/src/SpriteComponent.cpp b/src/SpriteComponent.cpp index 16b3420..1afe232 100644 --- a/src/SpriteComponent.cpp +++ b/src/SpriteComponent.cpp @@ -1,13 +1,28 @@ +#include "AnimationHandler.h" #include "TransformComponent.h" -#include "SpriteComponent.h" -#include "TextureManager.h" #include "Entity.h" +#include "TextureManager.h" SpriteComponent::SpriteComponent(const char* path) { setTexture(path); } +SpriteComponent::SpriteComponent(const char* path, bool isAnimated) +{ + animated = isAnimated; + + 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); + + play(IDLE); + + setTexture(path); +} + SpriteComponent::~SpriteComponent() { // SDL_DestroyTexture(this->texture); @@ -29,6 +44,12 @@ void SpriteComponent::init() void SpriteComponent::update() { + if (animated) { + srcRect.x = srcRect.w * static_cast((SDL_GetTicks() / speed) % frames); + } + + srcRect.y = animationIndex * transform->height; + this->destRect.x = this->transform->position.x; this->destRect.y = this->transform->position.y; this->destRect.w = transform->width * transform->scale; @@ -40,4 +61,9 @@ void SpriteComponent::draw() TextureManager::get().draw(this->texture, this->srcRect, this->destRect); } - +void SpriteComponent::play(AnimationType type) +{ + animationIndex = animations.at(type)->index; + frames = animations.at(type)->frames; + speed = animations.at(type)->speed; +}