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