r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
Enemy.hpp
Go to the documentation of this file.
1///
2/// @file Enemy.hpp
3/// @brief This file contains the enemy system definitions
4/// @namespace gme
5///
6
7#pragma once
8
9#include <cmath>
10#include <ranges>
11#include <vector>
12
13#include "ECS/Component.hpp"
15#include "ECS/Registry.hpp"
18
19namespace gme
20{
21
22 class EnemySystem final : public ecs::ASystem
23 {
24 public:
25 explicit EnemySystem(const std::shared_ptr<eng::IRenderer> &renderer) : m_renderer(renderer) {}
26 ~EnemySystem() override = default;
27
28 EnemySystem(const EnemySystem &) = delete;
29 EnemySystem &operator=(const EnemySystem &) = delete;
32
33 bool isEnable() override { return true; }
34 void setEnable(const bool enable) override { (void)enable; }
35
36 void update(ecs::Registry &registry, const float dt) override
37 {
38 std::vector<ecs::Entity> enemiesToRemove;
39
40 for (const auto &entity : registry.getAll<ecs::Enemy>() | std::views::keys)
41 {
42 auto *transform = registry.getComponent<ecs::Transform>(entity);
43 const auto *velocity = registry.getComponent<ecs::Velocity>(entity);
44 auto *rect = registry.getComponent<ecs::Rect>(entity);
45 const auto *texture = registry.getComponent<ecs::Texture>(entity);
46 const auto *scale = registry.getComponent<ecs::Scale>(entity);
47 auto *animation = registry.getComponent<ecs::Animation>(entity);
48
49 if ((transform == nullptr) || (velocity == nullptr) || (rect == nullptr) || (texture == nullptr) ||
50 (scale == nullptr))
51 {
52 continue;
53 }
54
55 transform->x += velocity->x * dt;
56 transform->y += velocity->y * dt;
57
58 if (animation != nullptr)
59 {
60 animation->current_time += dt;
61 if (animation->current_time >= animation->frame_duration)
62 {
63 animation->current_time = 0.0F;
64 animation->current_frame = (animation->current_frame + 1) % animation->total_frames;
65
66 const int frame_x =
67 animation->current_frame * static_cast<int>(utl::GameConfig::Enemy::Easy::SPRITE_WIDTH);
68 rect->pos_x = static_cast<float>(frame_x);
69 rect->pos_y = static_cast<float>(0);
70 }
71 }
72
73 m_renderer->createSprite(texture->id + std::to_string(entity), texture->path,
74 std::round(transform->x), std::round(transform->y), scale->x, scale->y,
75 static_cast<int>(rect->pos_x), static_cast<int>(rect->pos_y), rect->size_x,
76 rect->size_y);
77 m_renderer->drawSprite(texture->id + std::to_string(entity));
78
79 if (transform->x < utl::GameConfig::Screen::REMOVE_X ||
82 {
83 enemiesToRemove.push_back(entity);
84 }
85 }
86
87 for (const ecs::Entity entity : enemiesToRemove)
88 {
89 if (registry.hasComponent<ecs::Enemy>(entity))
90 {
91 registry.removeComponent<ecs::Enemy>(entity);
92 }
93 if (registry.hasComponent<ecs::Transform>(entity))
94 {
95 registry.removeComponent<ecs::Transform>(entity);
96 }
97 if (registry.hasComponent<ecs::Velocity>(entity))
98 {
99 registry.removeComponent<ecs::Velocity>(entity);
100 }
101 if (registry.hasComponent<ecs::Rect>(entity))
102 {
103 registry.removeComponent<ecs::Rect>(entity);
104 }
105 if (registry.hasComponent<ecs::Texture>(entity))
106 {
107 registry.removeComponent<ecs::Texture>(entity);
108 }
109 if (registry.hasComponent<ecs::Scale>(entity))
110 {
111 registry.removeComponent<ecs::Scale>(entity);
112 }
113 if (registry.hasComponent<ecs::Animation>(entity))
114 {
115 registry.removeComponent<ecs::Animation>(entity);
116 }
117 if (registry.hasComponent<ecs::Hitbox>(entity))
118 {
119 registry.removeComponent<ecs::Hitbox>(entity);
120 }
121 }
122 }
123
124 private:
125 const std::shared_ptr<eng::IRenderer> &m_renderer;
126 }; // class EnemySystem
127
128} // namespace gme
This file contains the component definitions.
Configuration constants for the multiplayer game.
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
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
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
EnemySystem(const EnemySystem &)=delete
EnemySystem & operator=(EnemySystem &&)=delete
EnemySystem(const std::shared_ptr< eng::IRenderer > &renderer)
Definition Enemy.hpp:25
void setEnable(const bool enable) override
Definition Enemy.hpp:34
EnemySystem(EnemySystem &&)=delete
void update(ecs::Registry &registry, const float dt) override
Definition Enemy.hpp:36
~EnemySystem() override=default
bool isEnable() override
Definition Enemy.hpp:33
const std::shared_ptr< eng::IRenderer > & m_renderer
Definition Enemy.hpp:125
EnemySystem & operator=(const EnemySystem &)=delete
std::uint32_t Entity
Definition Entity.hpp:13
constexpr float REMOVE_MIN_Y
constexpr float REMOVE_MAX_Y
constexpr float REMOVE_X
std::string id
Definition Component.hpp:15