r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
LoadingAnimation.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "ECS/Component.hpp"
5#include "ECS/Registry.hpp"
7
8namespace gme
9{
10
11 class LoadingAnimationSystem final : public ecs::ASystem
12 {
13 public:
14 explicit LoadingAnimationSystem(const std::shared_ptr<eng::IRenderer> &renderer) : m_renderer(renderer) {}
15 ~LoadingAnimationSystem() override = default;
16
21
22 void update(ecs::Registry &registry, float dt) override
23 {
24 std::string spriteName;
25 spriteName.reserve(64);
26
27 for (auto &[entity, animation] : registry.getAll<ecs::LoadingAnimation>())
28 {
29 const auto *transform = registry.getComponent<ecs::Transform>(entity);
30 auto *rect = registry.getComponent<ecs::Rect>(entity);
31 const auto *texture = registry.getComponent<ecs::Texture>(entity);
32
33 if (!transform || !rect || !texture)
34 {
35 continue;
36 }
37
38 animation.current_time += dt;
39 if (animation.current_time >= animation.frame_duration)
40 {
41 animation.current_time = 0.0f;
42 animation.current_frame = (animation.current_frame + 1) % animation.total_frames;
43
44 const int frame_x = (animation.current_frame % animation.frames_per_row) *
45 static_cast<int>(animation.frame_width);
46 const int frame_y = (animation.current_frame / animation.frames_per_row) *
47 static_cast<int>(animation.frame_height);
48
49 rect->pos_x = static_cast<float>(frame_x);
50 rect->pos_y = static_cast<float>(frame_y);
51 }
52
53 spriteName.clear();
54 spriteName = texture->id;
55 spriteName += std::to_string(entity);
56
57 m_renderer->setSpriteTexture(spriteName, texture->path);
58 m_renderer->setSpritePosition(spriteName, transform->x, transform->y);
59 m_renderer->setSpriteFrame(spriteName, static_cast<int>(rect->pos_x), static_cast<int>(rect->pos_y),
60 rect->size_x, rect->size_y);
61 m_renderer->drawSprite(spriteName);
62 }
63 }
64
65 private:
66 const std::shared_ptr<eng::IRenderer> &m_renderer;
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 & operator=(const LoadingAnimationSystem &&)=delete
LoadingAnimationSystem(const LoadingAnimationSystem &)=delete
std::string id
Definition Component.hpp:15