r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
LoadingAnimation.hpp
Go to the documentation of this file.
1///
2/// @file Systems.hpp
3/// @brief This file contains the system definitions
4/// @namespace cli
5///
6
7#pragma once
8
9#include "ECS/Component.hpp"
10#include "ECS/Registry.hpp"
12
13namespace cli
14{
15
17 {
18 public:
19 explicit LoadingAnimationSystem(eng::IRenderer &renderer) : m_renderer(renderer) {}
20 ~LoadingAnimationSystem() override = default;
21
26
27 void update(ecs::Registry &registry, float dt) override
28 {
29 for (auto &[entity, animation] : registry.getAll<ecs::LoadingAnimation>())
30 {
31 const auto *transform = registry.getComponent<ecs::Transform>(entity);
32 auto *rect = registry.getComponent<ecs::Rect>(entity);
33 const auto *texture = registry.getComponent<ecs::Texture>(entity);
34
35 if (!transform || !rect || !texture)
36 continue;
37
38 // Mettre à jour l'animation
39 animation.current_time += dt;
40 if (animation.current_time >= animation.frame_duration)
41 {
42 animation.current_time = 0.0f;
43 animation.current_frame = (animation.current_frame + 1) % animation.total_frames;
44
45 // Mettre à jour le rectangle de texture
46 int frame_x = (animation.current_frame % animation.frames_per_row) *
47 static_cast<int>(animation.frame_width);
48 int frame_y = (animation.current_frame / animation.frames_per_row) *
49 static_cast<int>(animation.frame_height);
50
51 rect->pos_x = static_cast<float>(frame_x);
52 rect->pos_y = static_cast<float>(frame_y);
53 }
54
55 // Dessiner l'animation
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:
66 }; // class LoadingAnimationSystem
67
68} // namespace cli
This file contains the component definitions.
This file contains the IRenderer class declaration.
This file contains the Registry class declaration.
LoadingAnimationSystem(eng::IRenderer &renderer)
LoadingAnimationSystem(LoadingAnimationSystem &&)=delete
void update(ecs::Registry &registry, float dt) override
LoadingAnimationSystem & operator=(const LoadingAnimationSystem &)=delete
LoadingAnimationSystem(const LoadingAnimationSystem &)=delete
LoadingAnimationSystem & operator=(LoadingAnimationSystem &&)=delete
~LoadingAnimationSystem() override=default
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
Interface for the renderer.
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
std::string id
Definition Component.hpp:15