Air-Trap 1.0.0
A multiplayer R-Type clone game engine built with C++23 and ECS architecture
Loading...
Searching...
No Matches
EnemyShootSystem.cpp
Go to the documentation of this file.
1
9#include "RType/Logger.hpp"
11
12#include <vector>
13
14namespace rtp::server
15{
17 // Public API
19
21 RoomSystem& roomSystem,
22 NetworkSyncSystem& networkSync)
23 : _registry(registry), _roomSystem(roomSystem), _networkSync(networkSync)
24 {
25 }
26
28 {
29 // Tuple: (transform, roomId, isBoomerang, shooterIndex)
30 std::vector<std::tuple<ecs::components::Transform,
32 bool,
33 uint32_t>> pendingSpawns;
34
35 auto view =
40
41 for (auto &&[tf, type, roomId, weapon] : view) {
42 if (type.type != net::EntityType::Scout &&
43 type.type != net::EntityType::Tank &&
44 type.type != net::EntityType::Boss &&
45 type.type != net::EntityType::Boss2 &&
46 type.type != net::EntityType::BossShield) {
47 continue;
48 }
49
50 if (weapon.fireRate <= 0.0f) {
51 continue;
52 }
53
54 weapon.lastShotTime += dt;
55 const float fireInterval = (1.0f / weapon.fireRate);
56
57 if (weapon.lastShotTime >= fireInterval) {
58 weapon.lastShotTime = 0.0f;
59 // Boss2 uses boomerang projectiles
60 bool isBoomerang = (type.type == net::EntityType::Boss2);
61 // Get entity index for boomerang tracking - using a simple position hash
62 uint32_t shooterIdx = static_cast<uint32_t>(tf.position.x * 1000 + tf.position.y);
63 pendingSpawns.emplace_back(tf, roomId, isBoomerang, shooterIdx);
64 }
65 }
66
67 for (const auto& [tf, roomId, isBoomerang, shooterIdx] : pendingSpawns) {
68 spawnBullet(tf, roomId, isBoomerang, shooterIdx);
69 }
70 }
71
73 // Private API
75
78 const ecs::components::RoomId& roomId,
79 bool isBoomerang,
80 uint32_t shooterIndex)
81 {
82 auto entityRes = _registry.spawn();
83 if (!entityRes) {
84 log::error("Failed to spawn enemy bullet entity: {}", entityRes.error().message());
85 return;
86 }
87
88 ecs::Entity bullet = entityRes.value();
89
90 const float x = tf.position.x + _spawnOffsetX;
91 const float y = tf.position.y;
92
94 bullet,
95 ecs::components::Transform{ {x, y}, 0.f, {1.f, 1.f} }
96 );
97
98 // Boomerang bullets go slower and curve back
99 float bulletSpeed = isBoomerang ? -200.0f : _bulletSpeed;
101 bullet,
102 ecs::components::Velocity{ {bulletSpeed, 0.f}, 0.f }
103 );
104
105 // Larger hitbox for boomerang
106 float bboxW = isBoomerang ? 24.0f : 8.0f;
107 float bboxH = isBoomerang ? 24.0f : 4.0f;
109 bullet,
110 ecs::components::BoundingBox{ bboxW, bboxH }
111 );
112
113 // More damage for boomerang
114 int damage = isBoomerang ? 25 : 10;
116 bullet,
118 );
119
121 bullet,
122 ecs::components::NetworkId{ static_cast<uint32_t>(bullet.index()) }
123 );
124
127 bullet,
128 ecs::components::EntityType{ bulletType }
129 );
130
132 bullet,
134 );
135
136 // Add boomerang component for Boss2 bullets
137 if (isBoomerang) {
139 boom.ownerIndex = shooterIndex;
140 boom.startPos = {x, y};
141 boom.maxDistance = 500.0f; // Travel distance before returning
142 boom.returning = false;
144 }
145
146 auto room = _roomSystem.getRoom(roomId.id);
147 if (!room)
148 return;
149 if (room->getState() != Room::State::InGame)
150 return;
151
152 const auto players = room->getPlayers();
153 std::vector<uint32_t> sessions;
154 sessions.reserve(players.size());
155 for (const auto& player : players) {
156 sessions.push_back(player->getId());
157 }
158
160 net::EntitySpawnPayload payload = {
161 static_cast<uint32_t>(bullet.index()),
162 static_cast<uint8_t>(bulletType),
163 x,
164 y
165 };
166 packet << payload;
168 }
169} // 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
auto spawn(void) -> std::expected< Entity, rtp::Error >
Definition Registry.cpp:51
auto add(Entity entity, Args &&...args) -> std::expected< std::reference_wrapper< T >, rtp::Error >
auto zipView(this Self &self)
Network packet with header and serializable body.
Definition Packet.hpp:471
EnemyShootSystem(ecs::Registry &registry, RoomSystem &roomSystem, NetworkSyncSystem &networkSync)
Constructor for EnemyShootSystem.
float _bulletSpeed
Speed of the spawned bullets.
ecs::Registry & _registry
Reference to the entity registry.
NetworkSyncSystem & _networkSync
Reference to the NetworkSyncSystem.
RoomSystem & _roomSystem
Reference to the RoomSystem.
void spawnBullet(const ecs::components::Transform &tf, const ecs::components::RoomId &roomId, bool isBoomerang=false, uint32_t shooterIndex=0)
Spawn a bullet entity at the given transform and room.
float _spawnOffsetX
X offset for bullet spawn position.
void update(float dt) override
Update enemy shooting system logic for one frame.
System to handle network-related operations on the server side.
void sendPacketToSessions(const std::vector< uint32_t > &sessions, const net::Packet &packet, net::NetworkMode mode)
Send a packet to multiple sessions.
System to handle room-related operations on the server side.
std::shared_ptr< Room > getRoom(uint32_t roomId)
Get a room by its ID.
@ InGame
Game in progress.
Definition Room.hpp:43
constexpr Entity NullEntity
Definition Entity.hpp:109
void error(LogFmt< std::type_identity_t< Args >... > fmt, Args &&...args) noexcept
Log an error message.
EntityType
Types of entities in the game.
Definition Packet.hpp:144
@ EntitySpawn
Entity spawn notification.
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
bool returning
Whether the boomerang is on its way back.
Definition Boomerang.hpp:20
uint32_t ownerIndex
ECS entity index of the owner who fired this boomerang.
Definition Boomerang.hpp:17
rtp::Vec2f startPos
Spawn position to compute travel distance.
Definition Boomerang.hpp:18
float maxDistance
Distance before returning.
Definition Boomerang.hpp:19
Component representing damage value.
Definition Damage.hpp:16
Component representing a network identifier for an entity.
Definition NetworkId.hpp:22
Component representing a network identifier for an entity.
Definition RoomId.hpp:22
uint32_t id
Unique network identifier for the entity.
Definition RoomId.hpp:23
Component representing a weapon configuration.
float lastShotTime
Time since last shot.
Component representing position, rotation, and scale of an entity.
Definition Transform.hpp:23
Vec2f position
X and Y coordinates.
Definition Transform.hpp:24
Component representing a 2D velocity.
Definition Velocity.hpp:17
Entity spawn notification data.
Definition Packet.hpp:348