r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
LoadingAnimation.hpp
Go to the documentation of this file.
1///
2/// @file LoadingAnimation.hpp
3/// @brief This file contains the loading animation system definitions
4/// @namespace gme
5///
6
7#pragma once
8
9#include "ECS/Component.hpp"
11#include "ECS/Registry.hpp"
13
14namespace gme
15{
16
18 {
19 public:
20 explicit LoadingAnimationSystem(const std::shared_ptr<eng::IRenderer> &renderer) : m_renderer(renderer) {}
21 ~LoadingAnimationSystem() override = default;
22
27
28 void update(ecs::Registry &registry, float dt) override
29 {
30 for (auto &[entity, animation] : registry.getAll<ecs::LoadingAnimation>())
31 {
32 const auto *transform = registry.getComponent<ecs::Transform>(entity);
33 auto *rect = registry.getComponent<ecs::Rect>(entity);
34 const auto *texture = registry.getComponent<ecs::Texture>(entity);
35
36 if (!transform || !rect || !texture)
37 {
38 continue;
39 }
40
41 animation.current_time += dt;
42 if (animation.current_time >= animation.frame_duration)
43 {
44 animation.current_time = 0.0f;
45 animation.current_frame = (animation.current_frame + 1) % animation.total_frames;
46
47 int frame_x = (animation.current_frame % animation.frames_per_row) *
48 static_cast<int>(animation.frame_width);
49 int frame_y = (animation.current_frame / animation.frames_per_row) *
50 static_cast<int>(animation.frame_height);
51
52 rect->pos_x = static_cast<float>(frame_x);
53 rect->pos_y = static_cast<float>(frame_y);
54 }
55
56 m_renderer->setSpriteTexture(texture->id + std::to_string(entity), texture->path);
57 m_renderer->setSpritePosition(texture->id + std::to_string(entity), transform->x, transform->y);
58 m_renderer->setSpriteFrame(texture->id + std::to_string(entity), static_cast<int>(rect->pos_x),
59 static_cast<int>(rect->pos_y), rect->size_x, rect->size_y);
60 m_renderer->drawSprite(texture->id + std::to_string(entity));
61 }
62 }
63
64 private:
65 const std::shared_ptr<eng::IRenderer> &m_renderer;
66 }; // class LoadingAnimationSystem
67
68} // namespace gme
This file contains the component definitions.
This file contains the IRenderer class declaration.
This file contains the interface for systems.
This file contains the Registry class declaration.
Abstract class for system.
Definition ISystems.hpp:34
Class for managing entities and their components.
Definition Registry.hpp:25
std::unordered_map< Entity, T > & getAll()
Definition Registry.hpp:77
T * getComponent(Entity e)
Definition Registry.hpp:71
virtual void drawSprite(const std::string &name)=0
virtual void setSpritePosition(const std::string &name, float x, float y)=0
virtual void setSpriteTexture(const std::string &name, const std::string &path)=0
virtual void setSpriteFrame(const std::string &name, int fx, int fy, int fnx, int fny)=0
LoadingAnimationSystem(LoadingAnimationSystem &&)=delete
LoadingAnimationSystem(const std::shared_ptr< eng::IRenderer > &renderer)
const std::shared_ptr< eng::IRenderer > & m_renderer
void update(ecs::Registry &registry, float dt) override
LoadingAnimationSystem & operator=(const LoadingAnimationSystem &)=delete
~LoadingAnimationSystem() override=default
LoadingAnimationSystem(const LoadingAnimationSystem &)=delete
LoadingAnimationSystem & operator=(LoadingAnimationSystem &&)=delete
std::string id
Definition Component.hpp:15