Air-Trap 1.0.0
A multiplayer R-Type clone game engine built with C++23 and ECS architecture
Loading...
Searching...
No Matches
BoomerangSystem.cpp
Go to the documentation of this file.
1
7#include "RType/Logger.hpp"
8
9namespace rtp::server
10{
12 : _registry(registry)
13 {}
14
16 {
20
21 auto boomerRes = _registry.get<Boomerang>();
22 auto tfRes = _registry.get<Transform>();
23 auto velRes = _registry.get<Velocity>();
24
25 if (!boomerRes || !tfRes || !velRes)
26 return;
27
28 auto &boomers = boomerRes->get();
29 auto &transforms = tfRes->get();
30 auto &vels = velRes->get();
31
32 for (auto e : boomers.entities()) {
33 if (!boomers.has(e) || !transforms.has(e) || !vels.has(e))
34 continue;
35
36 auto &b = boomers[e];
37 auto &btf = transforms[e];
38 auto &bvel = vels[e];
39
40 // If not yet returning, check distance from start
41 if (!b.returning) {
42 const float dx = btf.position.x - b.startPos.x;
43 const float dy = btf.position.y - b.startPos.y;
44 const float d2 = dx * dx + dy * dy;
45 const float threshold = b.maxDistance * b.maxDistance;
46 if (d2 >= threshold) {
47 b.returning = true;
48 log::info("Boomerang {} reached maxDistance (d2={} threshold={}), starting return", e.index(), d2, threshold);
49 }
50 }
51
52 // If returning, steer towards owner
53 if (b.returning) {
54 // Find the owner entity by index in the transforms storage
55 ecs::Entity owner;
56 bool ownerFound = false;
57 for (auto pe : transforms.entities()) {
58 if (static_cast<uint32_t>(pe.index()) == b.ownerIndex) {
59 owner = pe;
60 ownerFound = true;
61 break;
62 }
63 }
64 if (!ownerFound) {
65 // Owner no longer exists: remove projectile
66 log::info("Boomerang {} owner not found (ownerIndex={}), despawning", e.index(), b.ownerIndex);
67 _registry.kill(e);
68 continue;
69 }
70
71 const auto &otf = transforms[owner];
72 rtp::Vec2f desired{otf.position.x - btf.position.x, otf.position.y - btf.position.y};
73 if (bvel.speed > 0.0f) {
74 static_cast<void>(desired.normalize());
75 bvel.direction = desired; // maintain speed scalar
76 } else {
77 const float sp = bvel.direction.length();
78 if (sp == 0.0f) {
79 // fallback to a normalized vector
80 desired = desired.normalized();
81 bvel.direction = desired;
82 } else {
83 desired = desired.normalized();
84 bvel.direction = desired * sp;
85 }
86 log::debug("Boomerang {} steering towards owner {}: boomerang=({},{}), owner=({},{}), vel=({},{} )",
87 e.index(), owner.index(), btf.position.x, btf.position.y, otf.position.x, otf.position.y, bvel.direction.x, bvel.direction.y);
88 }
89 }
90 }
91 }
92
93} // namespace rtp::server
Logger declaration with support for multiple log levels.
Represents an entity in the ECS (Entity-Component-System) architecture.
Definition Entity.hpp:63
constexpr std::uint32_t index(void) const
void kill(Entity entity)
Definition Registry.cpp:73
auto get(this const Self &self) -> std::expected< std::reference_wrapper< ConstLike< Self, SparseArray< T > > >, rtp::Error >
void update(float dt) override
Update system logic for one frame.
BoomerangSystem(ecs::Registry &registry)
void info(LogFmt< std::type_identity_t< Args >... > fmt, Args &&...args) noexcept
Log an informational message.
void debug(LogFmt< std::type_identity_t< Args >... > fmt, Args &&...args) noexcept
Log a debug message.
File : GameManager.hpp License: MIT Author : Elias Josué HAJJAR LLAUQUEN elias-josue....
Marks a projectile as a boomerang and stores state for return logic.
Definition Boomerang.hpp:16
Component representing position, rotation, and scale of an entity.
Definition Transform.hpp:23
Component representing a 2D velocity.
Definition Velocity.hpp:17