r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
Asteroid.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 <cmath>
10#include <vector>
11
12#include "Client/GameConfig.hpp"
13#include "ECS/Component.hpp"
14#include "ECS/Registry.hpp"
16
17namespace cli
18{
19
20 class AsteroidSystem final : public eng::ISystem
21 {
22 public:
23 explicit AsteroidSystem(eng::IRenderer &renderer) : m_renderer(renderer) {}
24 ~AsteroidSystem() override = default;
25
26 AsteroidSystem(const AsteroidSystem &) = delete;
30
31 bool isEnable() override { return true; }
32 void setEnable(bool enable) override { (void)enable; }
33
34 void update(ecs::Registry &registry, float dt) override
35 {
36 std::vector<ecs::Entity> asteroidsToRemove;
37
38 for (auto &[entity, asteroid] : registry.getAll<ecs::Asteroid>())
39 {
40 auto *transform = registry.getComponent<ecs::Transform>(entity);
41 auto *velocity = registry.getComponent<ecs::Velocity>(entity);
42 auto *rect = registry.getComponent<ecs::Rect>(entity);
43 auto *texture = registry.getComponent<ecs::Texture>(entity);
44 auto *scale = registry.getComponent<ecs::Scale>(entity);
45 auto *animation = registry.getComponent<ecs::Animation>(entity);
46
47 if (!transform || !velocity || !rect || !texture || !scale || !animation)
48 continue;
49
50 animation->current_time += dt;
51 if (animation->current_time >= animation->frame_duration)
52 {
53 animation->current_time = 0.0f;
54 animation->current_frame = (animation->current_frame + 1) % animation->total_frames;
55
56 const int frame_x =
57 animation->current_frame * static_cast<int>(GameConfig::Asteroid::Small::SPRITE_WIDTH);
58 const int frame_y = 0;
59 rect->pos_x = static_cast<float>(frame_x);
60 rect->pos_y = static_cast<float>(frame_y);
61 }
62
63 transform->x += velocity->x * dt;
64 transform->y += velocity->y * dt;
65 transform->rotation += asteroid.rotation_speed * dt;
66
67 m_renderer.createSprite(texture->id + std::to_string(entity), texture->path,
68 std::round(transform->x), std::round(transform->y), scale->x, scale->y,
69 static_cast<int>(rect->pos_x), static_cast<int>(rect->pos_y),
70 static_cast<int>(rect->size_x), static_cast<int>(rect->size_y));
71 m_renderer.drawSprite(texture->id + std::to_string(entity));
72
73 if (transform->x < GameConfig::Screen::REMOVE_X ||
74 transform->y < GameConfig::Screen::REMOVE_MIN_Y ||
76 {
77 asteroidsToRemove.push_back(entity);
78 }
79 }
80
81 for (ecs::Entity entity : asteroidsToRemove)
82 {
83 if (registry.hasComponent<ecs::Asteroid>(entity))
84 registry.removeComponent<ecs::Asteroid>(entity);
85 if (registry.hasComponent<ecs::Transform>(entity))
86 registry.removeComponent<ecs::Transform>(entity);
87 if (registry.hasComponent<ecs::Velocity>(entity))
88 registry.removeComponent<ecs::Velocity>(entity);
89 if (registry.hasComponent<ecs::Rect>(entity))
90 registry.removeComponent<ecs::Rect>(entity);
91 if (registry.hasComponent<ecs::Texture>(entity))
92 registry.removeComponent<ecs::Texture>(entity);
93 if (registry.hasComponent<ecs::Scale>(entity))
94 registry.removeComponent<ecs::Scale>(entity);
95 if (registry.hasComponent<ecs::Hitbox>(entity))
96 registry.removeComponent<ecs::Hitbox>(entity);
97 }
98 }
99
100 private:
102 }; // class AsteroidSystem
103
104} // namespace cli
This file contains the component definitions.
Configuration constants for the game.
This file contains the IRenderer class declaration.
This file contains the Registry class declaration.
AsteroidSystem(const AsteroidSystem &)=delete
AsteroidSystem(AsteroidSystem &&)=delete
eng::IRenderer & m_renderer
Definition Asteroid.hpp:101
bool isEnable() override
Definition Asteroid.hpp:31
~AsteroidSystem() override=default
void setEnable(bool enable) override
Definition Asteroid.hpp:32
void update(ecs::Registry &registry, float dt) override
Definition Asteroid.hpp:34
AsteroidSystem & operator=(AsteroidSystem &&)=delete
AsteroidSystem(eng::IRenderer &renderer)
Definition Asteroid.hpp:23
AsteroidSystem & operator=(const AsteroidSystem &)=delete
Class for managing entities and their components.
Definition Registry.hpp:25
bool hasComponent(Entity e)
Definition Registry.hpp:79
std::unordered_map< Entity, T > & getAll()
Definition Registry.hpp:77
T * getComponent(Entity e)
Definition Registry.hpp:71
void removeComponent(Entity e)
Definition Registry.hpp:85
Interface for the renderer.
virtual void drawSprite(const std::string &name)=0
virtual void createSprite(const std::string &name, const std::string &textureName, float x, float y, float scale_x=1, float scale_y=1, int fx=0, int fy=0, int fnx=-1, int fny=-1)=0
constexpr float REMOVE_X
constexpr float REMOVE_MIN_Y
constexpr float REMOVE_MAX_Y
std::uint32_t Entity
Definition Entity.hpp:13
std::string id
Definition Component.hpp:15