R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
BoundarySystem.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** RTYPE
4** File description:
5** BoundarySystem
6*/
7
8#include "BoundarySystem.hpp"
9#include <vector>
10#include "../../../Logger/Logger.hpp"
11#include "../../Components/IComponent.hpp"
12#include "../../Components/PendingDestroy.hpp"
13#include "../../Components/Player.hpp"
14
15namespace ecs {
16 BoundarySystem::BoundarySystem(int screenWidth, int screenHeight)
17 : _screenWidth(screenWidth), _screenHeight(screenHeight) {}
18
29 void BoundarySystem::update(Registry &registry, [[maybe_unused]] float deltaTime) {
30 auto entities = registry.getEntitiesWithMask(this->getComponentMask());
31 std::vector<std::uint32_t> toMarkForDestruction;
32
33 for (auto entityId : entities) {
34 // Skip entities already marked for destruction
35 if (registry.hasComponent<PendingDestroy>(entityId)) {
36 continue;
37 }
38
39 auto &transform = registry.getComponent<Transform>(entityId);
40 auto pos = transform.getPosition();
41
42 if (pos.x < -100 || pos.x > _screenWidth + 100 || pos.y < -100 || pos.y > _screenHeight + 100) {
43 // Log if it's a player being destroyed
44 if (registry.hasComponent<Player>(entityId)) {
45 LOG_WARNING("[BOUNDARY] Player out of bounds at (", pos.x, ", ", pos.y,
46 ") - Limits: x[-100,", _screenWidth + 100, "] y[-100,", _screenHeight + 100,
47 "]");
48 }
49 toMarkForDestruction.push_back(entityId);
50 }
51 }
52
53 // Mark entities for destruction instead of destroying directly
54 for (auto entityId : toMarkForDestruction) {
56 }
57 }
58
59 void BoundarySystem::setScreenSize(int width, int height) {
60 _screenWidth = width;
61 _screenHeight = height;
62 }
63
65 return (1ULL << getComponentType<Transform>());
66 }
67} // namespace ecs
#define LOG_WARNING(...)
Definition Logger.hpp:182
BoundarySystem(int screenWidth=1920, int screenHeight=1080)
Constructs a BoundarySystem with specified screen dimensions.
void setScreenSize(int width, int height)
Updates the screen dimensions.
ComponentMask getComponentMask() const override
Gets the component mask for this system.
void update(Registry &registry, float deltaTime) override
Checks entities against screen boundaries.
Marker component indicating entity should be destroyed.
Component identifying an entity as a player with game statistics.
Definition Player.hpp:20
Manages entities, their signatures and component type registrations.
Definition Registry.hpp:68
std::vector< Address > getEntitiesWithMask(Signature requiredMask)
Get all entities matching a specific component mask.
Definition Registry.cpp:79
T & getComponent(Address address)
Get a component from an entity.
bool hasComponent(Address address)
Check if an entity has a specific component.
void setComponent(Address address, const T &component)
Set/add a component to an entity with its data.
Component representing position, rotation and scale in 2D space.
Definition Transform.hpp:20
Vector2 getPosition() const
Get the position vector.
Definition Transform.hpp:61
Maximum number of distinct component types supported by the Registry.
Definition GameLogic.hpp:26
std::uint64_t ComponentMask
Type alias for component bitmask.
Definition ISystem.hpp:24
@ OutOfBounds
Entity went outside screen boundaries.