R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
MapSystem.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** RTYPE
4** File description:
5** MapSystem.cpp - System managing map scrolling, transitions and state
6*/
7
8#include "MapSystem.hpp"
9#include "../../Components/Player.hpp"
11
12namespace ecs {
13
14 void MapSystem::update(Registry &registry, float deltaTime) {
15 auto mapEntities = registry.getEntitiesWithMask(this->getComponentMask());
16
17 // Early exit if no map is active
18 if (mapEntities.empty()) {
19 return;
20 }
21
22 // Process each map entity (typically there should be only one active map)
23 for (auto mapEntityId : mapEntities) {
24 auto &mapData = registry.getComponent<MapData>(mapEntityId);
25
26 // Skip if map is already completed
27 if (mapData.isCompleted()) {
28 continue;
29 }
30
31 // Update elapsed time
32 mapData.updateElapsedTime(deltaTime);
33
34 // Apply scrolling to all entities (except players)
35 float scrollSpeed = mapData.getScrollSpeed();
36 if (scrollSpeed > 0.0f) {
37 _applyScrolling(registry, scrollSpeed, deltaTime);
38 }
39
40 // Check if map duration is reached (0 = infinite)
41 float duration = mapData.getDuration();
42 if (duration > 0.0f && mapData.getElapsedTime() >= duration) {
43 LOG_INFO("Map '", mapData.getMapId(), "' completed after ", mapData.getElapsedTime(),
44 " seconds");
45 mapData.markCompleted();
46
47 // TODO: Trigger map transition event here
48 // Can be done via EventBus or GameStateManager
49 const std::string &nextMap = mapData.getNextMapId();
50 if (!nextMap.empty()) {
51 LOG_INFO("Next map: ", nextMap);
52 // Transition logic will be handled by MapLoader/GameLogic
53 }
54 }
55 }
56 }
57
58 void MapSystem::_applyScrolling(Registry &registry, float scrollSpeed, float deltaTime) {
59 // Get all entities with Transform component
60 ComponentMask transformMask = (1ULL << getComponentType<Transform>());
61 auto entities = registry.getEntitiesWithMask(transformMask);
62
63 // Calculate scroll offset for this frame
64 float scrollOffset = -scrollSpeed * deltaTime;
65
66 for (auto entityId : entities) {
67 // Skip player entities - they move independently
68 if (registry.hasComponent<Player>(entityId)) {
69 continue;
70 }
71
72 auto &transform = registry.getComponent<Transform>(entityId);
73 auto pos = transform.getPosition();
74 transform.setPosition(pos.x + scrollOffset, pos.y);
75 }
76 }
77
79 return (1ULL << getComponentType<MapData>());
80 }
81
82} // namespace ecs
#define LOG_INFO(...)
Definition Logger.hpp:181
Component storing information about the current map/level.
Definition MapData.hpp:24
void updateElapsedTime(float deltaTime)
Update the elapsed time.
Definition MapData.hpp:222
void update(Registry &registry, float deltaTime) override
Updates map state and handles scrolling.
Definition MapSystem.cpp:14
void _applyScrolling(Registry &registry, float scrollSpeed, float deltaTime)
Apply scrolling to entities (moves them left based on map speed).
Definition MapSystem.cpp:58
ComponentMask getComponentMask() const override
Gets the component mask for this system.
Definition MapSystem.cpp:78
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.
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