mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 10:13:42 +00:00
animation wip
This commit is contained in:
parent
091ffb4a33
commit
552168dab3
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
|
||||||
|
};
|
||||||
@ -10,3 +10,6 @@
|
|||||||
#define MAP_SIZE_X 25
|
#define MAP_SIZE_X 25
|
||||||
#define MAP_SIZE_Y 20
|
#define MAP_SIZE_Y 20
|
||||||
|
|
||||||
|
#define IDLE "idle"
|
||||||
|
#define WALK "walk"
|
||||||
|
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
#include "Game.h"
|
#include "Game.h"
|
||||||
#include "ECS.h"
|
#include "ECS.h"
|
||||||
#include "Components.h"
|
#include "Components.h"
|
||||||
|
#include "Defines.h"
|
||||||
|
|
||||||
class KeyboardController : public Component
|
class KeyboardController : public Component
|
||||||
{
|
{
|
||||||
@ -14,6 +15,8 @@ public:
|
|||||||
SDL_Scancode right;
|
SDL_Scancode right;
|
||||||
//SDL_Scancode action;
|
//SDL_Scancode action;
|
||||||
|
|
||||||
|
SpriteComponent* sprite;
|
||||||
|
|
||||||
KeyboardController();
|
KeyboardController();
|
||||||
KeyboardController(SDL_Scancode up, SDL_Scancode down, SDL_Scancode left, SDL_Scancode right/*, SDL_Scancode action*/);
|
KeyboardController(SDL_Scancode up, SDL_Scancode down, SDL_Scancode left, SDL_Scancode right/*, SDL_Scancode action*/);
|
||||||
|
|
||||||
|
|||||||
@ -2,16 +2,38 @@
|
|||||||
#include "Components.h"
|
#include "Components.h"
|
||||||
#include "SDL.h"
|
#include "SDL.h"
|
||||||
#include "TextureManager.h"
|
#include "TextureManager.h"
|
||||||
|
#include "AnimationHandler.h"
|
||||||
|
#include "Defines.h"
|
||||||
|
#include <map>
|
||||||
|
|
||||||
class SpriteComponent : public Component
|
class SpriteComponent : public Component
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
int animationIndex = 0;
|
||||||
|
|
||||||
|
std::map<const char*, Animation> animations;
|
||||||
|
|
||||||
SpriteComponent() = default;
|
SpriteComponent() = default;
|
||||||
SpriteComponent(const char* path)
|
SpriteComponent(const char* path)
|
||||||
{
|
{
|
||||||
setTexture(path);
|
setTexture(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SpriteComponent(const char* path, bool isAnimated)
|
||||||
|
{
|
||||||
|
animated = isAnimated;
|
||||||
|
|
||||||
|
Animation idle = Animation((int)AnimationType::idle, 2, 200);
|
||||||
|
Animation walk = Animation((int)AnimationType::walk, 2, 200);
|
||||||
|
|
||||||
|
animations.emplace(IDLE, idle);
|
||||||
|
animations.emplace(WALK, walk);
|
||||||
|
|
||||||
|
play(IDLE);
|
||||||
|
|
||||||
|
setTexture(path);
|
||||||
|
}
|
||||||
|
|
||||||
~SpriteComponent()
|
~SpriteComponent()
|
||||||
{
|
{
|
||||||
SDL_DestroyTexture(this->texture);
|
SDL_DestroyTexture(this->texture);
|
||||||
@ -29,12 +51,19 @@ class SpriteComponent : public Component
|
|||||||
this->srcRect.x = this->srcRect.y = 0;
|
this->srcRect.x = this->srcRect.y = 0;
|
||||||
this->srcRect.w = transform->width;
|
this->srcRect.w = transform->width;
|
||||||
this->srcRect.h = transform->height;
|
this->srcRect.h = transform->height;
|
||||||
;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void update() override
|
void update() override
|
||||||
{
|
{
|
||||||
|
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;
|
||||||
@ -46,9 +75,19 @@ class SpriteComponent : public Component
|
|||||||
TextureManager::get().draw(this->texture, this->srcRect, this->destRect);
|
TextureManager::get().draw(this->texture, this->srcRect, this->destRect);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void play(const char* animationName)
|
||||||
|
{
|
||||||
|
animationIndex = animations[animationName].index;
|
||||||
|
frames = animations[animationName].frames;
|
||||||
|
speed = animations[animationName].speed;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
TransformComponent* transform;
|
TransformComponent* transform;
|
||||||
SDL_Texture* texture;
|
SDL_Texture* texture;
|
||||||
SDL_Rect srcRect, destRect;
|
SDL_Rect srcRect, destRect;
|
||||||
|
|
||||||
|
bool animated = false;
|
||||||
|
int frames = 0;
|
||||||
|
int speed = 100;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -35,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);
|
||||||
};
|
};
|
||||||
@ -69,13 +69,13 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo
|
|||||||
//ecs implementation
|
//ecs implementation
|
||||||
|
|
||||||
player.addComponent<TransformComponent>(0,0,2); //posx, posy, scale
|
player.addComponent<TransformComponent>(0,0,2); //posx, posy, scale
|
||||||
player.addComponent<SpriteComponent>("assets/chicken_neutral_knight.png"); //adds sprite (32x32px), path needed
|
player.addComponent<SpriteComponent>("assets/chicken_neutral_knight.png", true); //adds sprite (32x32px), path needed
|
||||||
player.addComponent<KeyboardController>(SDL_SCANCODE_W, SDL_SCANCODE_S, SDL_SCANCODE_A, SDL_SCANCODE_D);//custom keycontrols can be added
|
player.addComponent<KeyboardController>(SDL_SCANCODE_W, SDL_SCANCODE_S, SDL_SCANCODE_A, SDL_SCANCODE_D);//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.addGroup(GROUP_PLAYERS); //tell programm what group it belongs to for rendering order
|
player.addGroup(GROUP_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);
|
enemy.addComponent<KeyboardController>(SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT);
|
||||||
enemy.addComponent<ColliderComponent>("enemy");
|
enemy.addComponent<ColliderComponent>("enemy");
|
||||||
enemy.addGroup(GROUP_ENEMIES);
|
enemy.addGroup(GROUP_ENEMIES);
|
||||||
|
|||||||
@ -20,6 +20,7 @@ KeyboardController::~KeyboardController()
|
|||||||
|
|
||||||
void KeyboardController::init()
|
void KeyboardController::init()
|
||||||
{
|
{
|
||||||
|
sprite = &entity->getComponent<SpriteComponent>();
|
||||||
transform = &entity->getComponent<TransformComponent>();
|
transform = &entity->getComponent<TransformComponent>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,6 +31,7 @@ void KeyboardController::update()
|
|||||||
|
|
||||||
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;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user