r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
Projectile.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 <vector>
10
11#include "ECS/Component.hpp"
12#include "ECS/Registry.hpp"
14
15namespace cli
16{
17
18 class ProjectileSystem final : public eng::ASystem
19 {
20 public:
21 explicit ProjectileSystem(eng::IRenderer & /* renderer */) {}
22 ~ProjectileSystem() override = default;
23
28
29 void update(ecs::Registry &registry, float dt) override
30 {
31 std::vector<ecs::Entity> entitiesToRemove;
32
33 for (auto &[entity, projectile] : registry.getAll<ecs::Projectile>())
34 {
35 projectile.current_lifetime += dt;
36 if (projectile.current_lifetime >= projectile.lifetime)
37 {
38 entitiesToRemove.push_back(entity);
39 continue;
40 }
41 auto *transform = registry.getComponent<ecs::Transform>(entity);
42 auto *velocity = registry.getComponent<ecs::Velocity>(entity);
43
44 if (transform && velocity)
45 {
46 transform->x += velocity->x * dt;
47 transform->y += velocity->y * dt;
48 }
49 }
50
51 for (const auto &entity : entitiesToRemove)
52 {
53 if (registry.hasComponent<ecs::Projectile>(entity))
54 registry.removeComponent<ecs::Projectile>(entity);
55 if (registry.hasComponent<ecs::Transform>(entity))
56 registry.removeComponent<ecs::Transform>(entity);
57 if (registry.hasComponent<ecs::Velocity>(entity))
58 registry.removeComponent<ecs::Velocity>(entity);
59 if (registry.hasComponent<ecs::Rect>(entity))
60 registry.removeComponent<ecs::Rect>(entity);
61 if (registry.hasComponent<ecs::Scale>(entity))
62 registry.removeComponent<ecs::Scale>(entity);
63 if (registry.hasComponent<ecs::Texture>(entity))
64 registry.removeComponent<ecs::Texture>(entity);
65 if (registry.hasComponent<ecs::Animation>(entity))
66 registry.removeComponent<ecs::Animation>(entity);
67 }
68 }
69
70 }; // class ProjectileSystem
71} // namespace cli
This file contains the component definitions.
This file contains the IRenderer class declaration.
This file contains the Registry class declaration.
void update(ecs::Registry &registry, float dt) override
ProjectileSystem(ProjectileSystem &&)=delete
ProjectileSystem & operator=(const ProjectileSystem &)=delete
ProjectileSystem(const ProjectileSystem &)=delete
~ProjectileSystem() override=default
ProjectileSystem & operator=(ProjectileSystem &&)=delete
ProjectileSystem(eng::IRenderer &)
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.