R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
OrbitalSystem.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** RTYPE
4** File description:
5** OrbitalSystem - Implementation
6*/
7
8#include "OrbitalSystem.hpp"
9#include <cmath>
10#include "../../Components/IComponent.hpp"
11#include "../../Components/PendingDestroy.hpp"
13
14namespace ecs {
18 void OrbitalSystem::update(Registry &registry, float deltaTime) {
19 auto entities = registry.getEntitiesWithMask(this->getComponentMask());
20
21 for (auto entityId : entities) {
22 auto &orbital = registry.getComponent<OrbitalModule>(entityId);
23 auto &transform = registry.getComponent<Transform>(entityId);
24
25 updateOrbitalPosition(registry, entityId, orbital, transform, deltaTime);
26 }
27 }
28
33 OrbitalModule &orbital, Transform &transform, float deltaTime) {
34 uint32_t parentId = orbital.getParentEntityId();
35
36 // Check if parent still exists
37 if (!registry.hasComponent<Transform>(parentId)) {
38 // Parent destroyed, destroy this module too
39 if (!registry.hasComponent<PendingDestroy>(moduleEntity)) {
40 registry.setComponent(moduleEntity, PendingDestroy());
41 LOG_DEBUG("[OrbitalSystem] Parent entity ", parentId, " destroyed, removing orbital module ",
42 moduleEntity);
43 }
44 return;
45 }
46
47 // Get parent position
48 const Transform &parentTransform = registry.getComponent<Transform>(parentId);
49 auto parentPos = parentTransform.getPosition();
50
51 // Update orbital angle
52 float newAngle = orbital.getCurrentAngle() + (orbital.getOrbitSpeed() * deltaTime);
53
54 // Normalize angle to [0, 2π]
55 const float TWO_PI = 2.0f * 3.14159265359f;
56 if (newAngle >= TWO_PI) {
57 newAngle -= TWO_PI;
58 } else if (newAngle < 0.0f) {
59 newAngle += TWO_PI;
60 }
61
62 orbital.setCurrentAngle(newAngle);
63
64 // Calculate new position using circular motion
65 float radius = orbital.getOrbitRadius();
66 float newX = parentPos.x + radius * std::cos(newAngle);
67 float newY = parentPos.y + radius * std::sin(newAngle);
68
69 transform.setPosition(newX, newY);
70 }
71
73 return (1ULL << getComponentType<OrbitalModule>()) | (1ULL << getComponentType<Transform>());
74 }
75} // namespace ecs
#define LOG_DEBUG(...)
Definition Logger.hpp:180
Component for entities that orbit around a parent entity (like drones in Isaac).
void setCurrentAngle(float angle)
Set current angle.
uint32_t getParentEntityId() const
Get parent entity ID.
float getOrbitRadius() const
Get orbit radius.
float getCurrentAngle() const
Get current angle.
float getOrbitSpeed() const
Get orbit speed.
void update(Registry &registry, float deltaTime) override
Updates all orbital module positions.
void updateOrbitalPosition(Registry &registry, Address moduleEntity, OrbitalModule &orbital, Transform &transform, float deltaTime)
Updates a single orbital module's position.
ComponentMask getComponentMask() const override
Gets the component mask for this system.
Marker component indicating entity should be destroyed.
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
void setPosition(float posX, float posY)
Set the position.
Definition Transform.hpp:80
Maximum number of distinct component types supported by the Registry.
Definition GameLogic.hpp:26
std::uint32_t Address
Type used to represent an entity address/ID.
std::uint64_t ComponentMask
Type alias for component bitmask.
Definition ISystem.hpp:24