r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
Enemy.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 EnemySystem final : public eng::ISystem
21 {
22 public:
23 explicit EnemySystem(eng::IRenderer &renderer) : m_renderer(renderer) {}
24 ~EnemySystem() override = default;
25
26 EnemySystem(const EnemySystem &) = delete;
27 EnemySystem &operator=(const EnemySystem &) = 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> enemiesToRemove;
37
38 // Mettre à jour les ennemis existants
39 for (auto &[entity, enemy] : registry.getAll<ecs::Enemy>())
40 {
41 auto *transform = registry.getComponent<ecs::Transform>(entity);
42 auto *velocity = registry.getComponent<ecs::Velocity>(entity);
43 auto *rect = registry.getComponent<ecs::Rect>(entity);
44 auto *texture = registry.getComponent<ecs::Texture>(entity);
45 auto *scale = registry.getComponent<ecs::Scale>(entity);
46 auto *animation = registry.getComponent<ecs::Animation>(entity);
47
48 if (!transform || !velocity || !rect || !texture || !scale)
49 continue;
50
51 // Mettre à jour la position
52 transform->x += velocity->x * dt;
53 transform->y += velocity->y * dt;
54
55 // Animation simple
56 if (animation)
57 {
58 animation->current_time += dt;
59 if (animation->current_time >= animation->frame_duration)
60 {
61 animation->current_time = 0.0f;
62 animation->current_frame = (animation->current_frame + 1) % animation->total_frames;
63
64 const int frame_x =
65 animation->current_frame * static_cast<int>(GameConfig::Enemy::Easy::SPRITE_WIDTH);
66 const int frame_y = 0;
67 rect->pos_x = static_cast<float>(frame_x);
68 rect->pos_y = static_cast<float>(frame_y);
69 }
70 }
71
72 // Dessiner l'ennemi
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),
76 static_cast<int>(rect->size_x), static_cast<int>(rect->size_y));
77 m_renderer.drawSprite(texture->id + std::to_string(entity));
78
79 if (transform->x < GameConfig::Screen::REMOVE_X ||
80 transform->y < GameConfig::Screen::REMOVE_MIN_Y ||
82 {
83 enemiesToRemove.push_back(entity);
84 }
85 }
86
87 for (ecs::Entity entity : enemiesToRemove)
88 {
89 if (registry.hasComponent<ecs::Enemy>(entity))
90 registry.removeComponent<ecs::Enemy>(entity);
91 if (registry.hasComponent<ecs::Transform>(entity))
92 registry.removeComponent<ecs::Transform>(entity);
93 if (registry.hasComponent<ecs::Velocity>(entity))
94 registry.removeComponent<ecs::Velocity>(entity);
95 if (registry.hasComponent<ecs::Rect>(entity))
96 registry.removeComponent<ecs::Rect>(entity);
97 if (registry.hasComponent<ecs::Texture>(entity))
98 registry.removeComponent<ecs::Texture>(entity);
99 if (registry.hasComponent<ecs::Scale>(entity))
100 registry.removeComponent<ecs::Scale>(entity);
101 if (registry.hasComponent<ecs::Animation>(entity))
102 registry.removeComponent<ecs::Animation>(entity);
103 if (registry.hasComponent<ecs::Hitbox>(entity))
104 registry.removeComponent<ecs::Hitbox>(entity);
105 }
106 }
107
108 private:
110 }; // class EnemySystem
111
112} // 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.
EnemySystem & operator=(EnemySystem &&)=delete
bool isEnable() override
Definition Enemy.hpp:31
void update(ecs::Registry &registry, float dt) override
Definition Enemy.hpp:34
void setEnable(bool enable) override
Definition Enemy.hpp:32
EnemySystem(const EnemySystem &)=delete
eng::IRenderer & m_renderer
Definition Enemy.hpp:109
EnemySystem(eng::IRenderer &renderer)
Definition Enemy.hpp:23
~EnemySystem() override=default
EnemySystem & operator=(const EnemySystem &)=delete
EnemySystem(EnemySystem &&)=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