R-Type 0.1
Loading...
Searching...
No Matches
EnemyGarbageCollectorSystem.hpp
1#pragma once
2
3#include <vector>
4
5#include <ISystem.hpp>
6
7#include "components/Transform.hpp"
8#include "events/busEvents.hpp"
9
10#include "components/Dead.hpp"
11#include "components/Enemy.hpp"
12#include "components/EnemyEvent.hpp"
13
14namespace rtk::systems {
23class EnemyGCSystem final : public rtk::ecs::ISystem {
24public:
29 EnemyGCSystem(busEvent &eventBus) : _eventBus(eventBus) {};
30 ~EnemyGCSystem() override = default;
31
35 void onStart(rtk::ecs::Registry &) override {}
36
41 void update(rtk::ecs::Registry &reg, float) override {
42 auto view = reg.view<component::Transform, component::Enemy>();
43 auto &transforms = reg.get_components<component::Transform>();
44
45 std::vector<std::size_t> entitiesToDestroy;
46
47 for (auto entity : view) {
48 auto *transform = transforms.get(entity);
49 if (!transform)
50 continue;
51
52 if (transform->position.x < -100.0f) {
53 entitiesToDestroy.push_back(entity);
54 }
55 }
56
57 for (auto entity : entitiesToDestroy) {
58 reg.get_components<component::Dead>().insert_at(entity, {});
60 }
61 }
62
66 void onStop(rtk::ecs::Registry &) override {}
67
68private:
69 busEvent &_eventBus;
70};
71
73} // namespace rtk::systems
Type indexed publish and subscribe event bus.
Definition busEvents.hpp:20
void publish(const E &event)
Calls every handler registered for the event type.
Definition busEvents.hpp:52
Removes enemies that left the screen on the left side.
Definition EnemyGarbageCollectorSystem.hpp:23
void update(rtk::ecs::Registry &reg, float) override
Marks enemies with x below -100 as dead and publishes an event.
Definition EnemyGarbageCollectorSystem.hpp:41
void onStart(rtk::ecs::Registry &) override
Does nothing.
Definition EnemyGarbageCollectorSystem.hpp:35
void onStop(rtk::ecs::Registry &) override
Does nothing.
Definition EnemyGarbageCollectorSystem.hpp:66
EnemyGCSystem(busEvent &eventBus)
Creates the system.
Definition EnemyGarbageCollectorSystem.hpp:29
Tag marking an entity for destruction at the end of the frame.
Definition Dead.hpp:17
Marks an entity as a Bydos enemy.
Definition Enemy.hpp:17
Position, rotation and scale of an entity in world space.
Definition Transform.hpp:22
Published when an enemy leaves the screen on the left side.
Definition EnemyEvent.hpp:17