Air-Trap 1.0.0
A multiplayer R-Type clone game engine built with C++23 and ECS architecture
Loading...
Searching...
No Matches
ShieldSystem.cpp
Go to the documentation of this file.
1
14#include "RType/Logger.hpp"
15
16#include <cmath>
17
18namespace rtp::client {
19
21 : _registry(registry)
22{
23}
24
26{
27 auto shieldVisualsOpt = _registry.get<rtp::ecs::components::ShieldVisual>();
28 if (!shieldVisualsOpt) return;
29
30 auto& shieldVisuals = shieldVisualsOpt.value().get();
31
32 for (auto entity : shieldVisuals.entities()) {
33 if (!shieldVisuals.has(entity)) continue;
34
35 auto& shield = shieldVisuals[entity];
36 shield.animationTime += dt;
37
38 const float pulse = std::sin(shield.animationTime * 3.0f) * 0.5f + 0.5f;
39 shield.alpha = 150.0f + pulse * 105.0f;
40 }
41
42 auto playerTransformsOpt = _registry.get<ecs::components::Transform>();
43 auto playerBoxesOpt = _registry.get<ecs::components::BoundingBox>();
44 auto playerTypesOpt = _registry.get<ecs::components::EntityType>();
45 auto bulletTransformsOpt = _registry.get<ecs::components::Transform>();
46 auto bulletBoxesOpt = _registry.get<ecs::components::BoundingBox>();
47 auto entityTypesOpt = _registry.get<ecs::components::EntityType>();
48
49 if (!playerTransformsOpt || !playerBoxesOpt || !playerTypesOpt ||
50 !bulletTransformsOpt || !bulletBoxesOpt || !entityTypesOpt) {
51 return;
52 }
53
54 auto& playerTransforms = playerTransformsOpt.value().get();
55 auto& playerBoxes = playerBoxesOpt.value().get();
56 auto& playerTypes = playerTypesOpt.value().get();
57 auto& bulletTransforms = bulletTransformsOpt.value().get();
58 auto& bulletBoxes = bulletBoxesOpt.value().get();
59 auto& entityTypes = entityTypesOpt.value().get();
60
61 std::vector<ecs::Entity> shieldsToRemove;
62 std::vector<ecs::Entity> bulletsToDestroy;
63
64 for (auto playerEntity : shieldVisuals.entities()) {
65 if (!shieldVisuals.has(playerEntity)) continue;
66 if (!playerTypes.has(playerEntity)) continue;
67 if (playerTypes[playerEntity].type != net::EntityType::Player) continue;
68
69 if (!playerTransforms.has(playerEntity)) continue;
70 if (!playerBoxes.has(playerEntity)) continue;
71
72 const auto& playerTrans = playerTransforms[playerEntity];
73 const auto& playerBox = playerBoxes[playerEntity];
74
75 for (auto bulletEntity : entityTypes.entities()) {
76 if (!entityTypes.has(bulletEntity)) continue;
77 if (entityTypes[bulletEntity].type != net::EntityType::EnemyBullet) continue;
78 if (!bulletTransforms.has(bulletEntity)) continue;
79 if (!bulletBoxes.has(bulletEntity)) continue;
80
81 const auto& bulletTrans = bulletTransforms[bulletEntity];
82 const auto& bulletBox = bulletBoxes[bulletEntity];
83
84 const bool collisionX = playerTrans.position.x < bulletTrans.position.x + bulletBox.width &&
85 playerTrans.position.x + playerBox.width > bulletTrans.position.x;
86
87 const bool collisionY = playerTrans.position.y < bulletTrans.position.y + bulletBox.height &&
88 playerTrans.position.y + playerBox.height > bulletTrans.position.y;
89
90 if (collisionX && collisionY) {
91 shieldsToRemove.push_back(playerEntity);
92 bulletsToDestroy.push_back(bulletEntity);
93 log::info("🛡️ Shield absorbed bullet!");
94 break;
95 }
96 }
97 }
98
99
100 for (const auto& entity : shieldsToRemove) {
102 }
103
104 for (const auto& entity : bulletsToDestroy) {
105 _registry.kill(entity);
106 }
107}
108
109} // namespace rtp::client
Logger declaration with support for multiple log levels.
ShieldSystem(ecs::Registry &registry)
Construct a new Shield System.
void update(float dt) override
Update shield animations and handle collision detection.
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 remove(Entity entity) noexcept
R-Type client namespace.
void info(LogFmt< std::type_identity_t< Args >... > fmt, Args &&...args) noexcept
Log an informational message.
float width
Width of the rectangle.
Component for visual shield effect displayed around the player.
float animationTime
Time accumulator for pulse animation.
Component representing position, rotation, and scale of an entity.
Definition Transform.hpp:23