mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 07:53:43 +00:00
Merge branch 'Animations' into 'main'
This commit is contained in:
commit
317dc9d3bc
BIN
assets/chicken_knight_spritesheet.png
Normal file
BIN
assets/chicken_knight_spritesheet.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.8 KiB |
BIN
assets/chicken_spritesheet.png
Normal file
BIN
assets/chicken_spritesheet.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.4 KiB |
23
include/AnimationHandler.h
Normal file
23
include/AnimationHandler.h
Normal 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
|
||||
};
|
||||
@ -1,9 +1,10 @@
|
||||
#pragma once
|
||||
#include "SDL.h"
|
||||
#include <SDL.h>
|
||||
#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
|
||||
|
||||
@ -1,14 +1,31 @@
|
||||
#pragma once
|
||||
#include "SDL.h"
|
||||
#include "AnimationHandler.h"
|
||||
#include "Component.h"
|
||||
#include "Game.h"
|
||||
#include <map>
|
||||
|
||||
class TransformComponent;
|
||||
|
||||
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:
|
||||
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);
|
||||
};
|
||||
|
||||
@ -1,7 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include "SDL_render.h"
|
||||
#include <SDL_render.h>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
struct cmp_str
|
||||
{
|
||||
@ -34,5 +35,6 @@ class TextureManager
|
||||
std::map<const char*, SDL_Texture*, cmp_str> texture_cache;
|
||||
|
||||
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);
|
||||
};
|
||||
@ -74,14 +74,14 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo
|
||||
//ecs implementation
|
||||
|
||||
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<ColliderComponent>("player"); //adds tag (for further use, reference tag)
|
||||
player.addComponent<HealthComponent>(5, &manager, true);
|
||||
player.addGroup((size_t)GroupLabel::PLAYERS); //tell programm what group it belongs to for rendering order
|
||||
|
||||
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<ColliderComponent>("enemy");
|
||||
enemy.addComponent<HealthComponent>(5, &manager, false);
|
||||
|
||||
@ -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<SpriteComponent>();
|
||||
transform = &entity->getComponent<TransformComponent>();
|
||||
}
|
||||
|
||||
@ -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]) {
|
||||
|
||||
@ -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<int>((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;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user