0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 21:23:41 +00:00

Merge branch 'Animations' into 'main'

This commit is contained in:
Benedikt Galbavy 2024-01-24 02:07:36 +01:00
commit 317dc9d3bc
9 changed files with 87 additions and 14 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

View File

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

View File

@ -1,9 +1,10 @@
#pragma once #pragma once
#include "SDL.h" #include <SDL.h>
#include "Component.h" #include "Component.h"
#include "Vector2D.h" #include "Vector2D.h"
class TransformComponent; class TransformComponent;
class SpriteComponent;
class KeyboardController : public Component class KeyboardController : public Component
{ {
@ -16,6 +17,8 @@ public:
SDL_Scancode right; SDL_Scancode right;
SDL_Scancode fire; SDL_Scancode fire;
SpriteComponent* sprite;
//for attack cooldown in between shots //for attack cooldown in between shots
Uint32 lastFireTime; Uint32 lastFireTime;
Uint32 fireCooldown = 1000; //in ms can be adjusted to change possible attack-speed Uint32 fireCooldown = 1000; //in ms can be adjusted to change possible attack-speed

View File

@ -1,14 +1,31 @@
#pragma once #pragma once
#include "SDL.h" #include "AnimationHandler.h"
#include "Component.h"
#include "Game.h" #include "Game.h"
#include <map>
class TransformComponent; class TransformComponent;
class SpriteComponent : public Component class SpriteComponent : public Component
{ {
public:
int animationIndex = 0;
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: public:
SpriteComponent() = default; SpriteComponent() = default;
SpriteComponent(const char* path); SpriteComponent(const char* path);
SpriteComponent(const char* path, bool isAnimated);
~SpriteComponent(); ~SpriteComponent();
void setTexture(const char* path); void setTexture(const char* path);
@ -16,10 +33,5 @@ class SpriteComponent : public Component
void init() override; void init() override;
void update() override; void update() override;
void draw() override; void draw() override;
void play(AnimationType type);
private:
TransformComponent* transform;
SDL_Texture* texture;
SDL_Rect srcRect;
SDL_Rect destRect;
}; };

View File

@ -1,7 +1,8 @@
#pragma once #pragma once
#include "SDL_render.h" #include <SDL_render.h>
#include <map> #include <map>
#include <vector>
struct cmp_str struct cmp_str
{ {
@ -34,5 +35,6 @@ class TextureManager
std::map<const char*, SDL_Texture*, cmp_str> texture_cache; std::map<const char*, SDL_Texture*, cmp_str> texture_cache;
SDL_Texture* loadTexture(const char* fileName); SDL_Texture* loadTexture(const char* fileName);
static std::vector<SDL_Rect> 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);
}; };

View File

@ -74,14 +74,14 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo
//ecs implementation //ecs implementation
player.addComponent<TransformComponent>(80,80,2); //posx, posy, scale player.addComponent<TransformComponent>(80,80,2); //posx, posy, scale
player.addComponent<SpriteComponent>("assets/chicken_neutral_knight.png"); //adds sprite (32x32px), path needed player.addComponent<SpriteComponent>("assets/chicken_knight_spritesheet.png", true); //adds sprite (32x32px), path needed
player.addComponent<KeyboardController>(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<KeyboardController>(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<ColliderComponent>("player"); //adds tag (for further use, reference tag) player.addComponent<ColliderComponent>("player"); //adds tag (for further use, reference tag)
player.addComponent<HealthComponent>(5, &manager, true); player.addComponent<HealthComponent>(5, &manager, true);
player.addGroup((size_t)GroupLabel::PLAYERS); //tell programm what group it belongs to for rendering order player.addGroup((size_t)GroupLabel::PLAYERS); //tell programm what group it belongs to for rendering order
enemy.addComponent<TransformComponent>(600, 500, 2); enemy.addComponent<TransformComponent>(600, 500, 2);
enemy.addComponent<SpriteComponent>("assets/chicken_neutral.png"); enemy.addComponent<SpriteComponent>("assets/chicken_spritesheet.png", true);
enemy.addComponent<KeyboardController>(SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_RCTRL, Vector2D(-1, 0)); enemy.addComponent<KeyboardController>(SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_RCTRL, Vector2D(-1, 0));
enemy.addComponent<ColliderComponent>("enemy"); enemy.addComponent<ColliderComponent>("enemy");
enemy.addComponent<HealthComponent>(5, &manager, false); enemy.addComponent<HealthComponent>(5, &manager, false);

View File

@ -2,6 +2,7 @@
#include "TransformComponent.h" #include "TransformComponent.h"
#include "Entity.h" #include "Entity.h"
#include "AssetManager.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) 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() void KeyboardController::init()
{ {
sprite = &entity->getComponent<SpriteComponent>();
transform = &entity->getComponent<TransformComponent>(); transform = &entity->getComponent<TransformComponent>();
} }
@ -22,18 +24,23 @@ void KeyboardController::update()
{ {
transform->velocity.x = 0; transform->velocity.x = 0;
transform->velocity.y = 0; transform->velocity.y = 0;
sprite->play(IDLE);
if (keystates[this->up]) { if (keystates[this->up]) {
transform->velocity.y = -1; transform->velocity.y = -1;
sprite->play(WALK);
} }
if (keystates[this->left]) { if (keystates[this->left]) {
transform->velocity.x = -1; transform->velocity.x = -1;
sprite->play(WALK);
} }
if (keystates[this->down]) { if (keystates[this->down]) {
transform->velocity.y = 1; transform->velocity.y = 1;
sprite->play(WALK);
} }
if (keystates[this->right]) { if (keystates[this->right]) {
transform->velocity.x = 1; transform->velocity.x = 1;
sprite->play(WALK);
} }
if (keystates[this->fire]) { if (keystates[this->fire]) {

View File

@ -1,13 +1,28 @@
#include "AnimationHandler.h"
#include "TransformComponent.h" #include "TransformComponent.h"
#include "SpriteComponent.h"
#include "TextureManager.h"
#include "Entity.h" #include "Entity.h"
#include "TextureManager.h"
SpriteComponent::SpriteComponent(const char* path) SpriteComponent::SpriteComponent(const char* path)
{ {
setTexture(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() SpriteComponent::~SpriteComponent()
{ {
// SDL_DestroyTexture(this->texture); // SDL_DestroyTexture(this->texture);
@ -29,6 +44,12 @@ void SpriteComponent::init()
void SpriteComponent::update() void SpriteComponent::update()
{ {
if (animated) {
srcRect.x = srcRect.w * static_cast<int>((SDL_GetTicks() / speed) % frames);
}
srcRect.y = animationIndex * transform->height;
this->destRect.x = this->transform->position.x; this->destRect.x = this->transform->position.x;
this->destRect.y = this->transform->position.y; this->destRect.y = this->transform->position.y;
this->destRect.w = transform->width * transform->scale; this->destRect.w = transform->width * transform->scale;
@ -40,4 +61,9 @@ void SpriteComponent::draw()
TextureManager::get().draw(this->texture, this->srcRect, this->destRect); 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;
}