r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
Animation.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
16 class AnimationSystem final : public eng::ASystem
17 {
18 public:
19 explicit AnimationSystem(eng::IRenderer & /* renderer */) {}
20 ~AnimationSystem() override = default;
21
26
27 void update(ecs::Registry &registry, float dt) override
28 {
29 for (auto &[entity, animation] : registry.getAll<ecs::Animation>())
30 {
31 animation.current_time += dt;
32
33 if (animation.current_time >= animation.frame_duration)
34 {
35 animation.current_time = 0.0f;
36 animation.current_frame = (animation.current_frame + 1) % animation.total_frames;
37 }
38
39 auto *rect = registry.getComponent<ecs::Rect>(entity);
40
41 if (rect)
42 {
43 // Calculer la position du frame dans la spritesheet
44 int frame_x = (animation.current_frame % animation.frames_per_row) * animation.frame_width;
45 int frame_y = (animation.current_frame / animation.frames_per_row) * animation.frame_height;
46 if (rect->pos_x != static_cast<float>(frame_x) || rect->pos_y != static_cast<float>(frame_y))
47 {
48 rect->pos_x = static_cast<float>(frame_x);
49 rect->pos_y = static_cast<float>(frame_y);
50 rect->size_x = animation.frame_width;
51 rect->size_y = animation.frame_height;
52 }
53 }
54 }
55 }
56
57 }; // class AnimationSystem
58
59} // namespace cli
This file contains the component definitions.
This file contains the IRenderer class declaration.
This file contains the Registry class declaration.
AnimationSystem(const AnimationSystem &)=delete
AnimationSystem(AnimationSystem &&)=delete
void update(ecs::Registry &registry, float dt) override
Definition Animation.hpp:27
AnimationSystem & operator=(AnimationSystem &&)=delete
AnimationSystem & operator=(const AnimationSystem &)=delete
AnimationSystem(eng::IRenderer &)
Definition Animation.hpp:19
~AnimationSystem() 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.