Files
aee-arcade/source/core/rendering/sprite/animated_sprite.hpp
T

45 lines
1.2 KiB
C++

#pragma once
#include <SDL3/SDL.h>
#include <string>
#include <unordered_map>
#include <vector>
#include "core/rendering/sprite/moving_sprite.hpp"
#include "core/resources/resource_types.hpp"
class AnimatedSprite : public MovingSprite {
public:
struct AnimationData {
std::string name;
std::vector<SDL_FRect> frames;
std::vector<float> speeds;
int loop_from{-1};
bool completed{false};
int current_frame{0};
float accumulated_time{0.0F};
};
explicit AnimatedSprite(const AnimationResource& cached_data);
~AnimatedSprite() override = default;
void update(float delta_time) override;
auto animationIsCompleted() -> bool;
auto getCurrentAnimationSize() -> int { return static_cast<int>(animations_[current_animation_].frames.size()); }
void setCurrentAnimation(const std::string& name = "default");
void setCurrentAnimation(int index = 0);
void resetAnimation();
void setCurrentAnimationFrame(int num);
void animate(float delta_time);
private:
void buildNameIndex();
std::vector<AnimationData> animations_;
std::unordered_map<std::string, int> animation_index_;
int current_animation_{0};
};