r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
EnemyAISystem.hpp
Go to the documentation of this file.
1///
2/// @file EnemyAISystem.hpp
3/// @brief Server-side enemy AI and behavior system for R-Type
4/// @details This file contains the enemy AI system that controls all enemy behaviors,
5/// movement patterns, and combat logic on the game server. The system implements
6/// various AI patterns including straight movement, sine waves, aggressive pursuit,
7/// stationary shooting, and zigzag patterns. All enemy behavior is server-authoritative
8/// to ensure fair and synchronized gameplay.
9/// @namespace gme
10/// @author R-Type Team
11/// @date 2025
12///
13
14#pragma once
15
16#include "ECS/Component.hpp"
17#include "ECS/Entity.hpp"
19#include "ECS/Registry.hpp"
21#include <random>
22#include <unordered_map>
23
24namespace gme
25{
26 ///
27 /// @enum EnemyBehavior
28 /// @brief Enumeration of enemy movement and behavior patterns
29 /// @details Defines the various AI behavior patterns available for enemies.
30 /// Each pattern provides different challenges and gameplay variety.
31 ///
32 enum class EnemyBehavior : uint8_t
33 {
34 STRAIGHT = 0, ///< Move straight left at constant speed (basic behavior)
35 SINE_WAVE = 1, ///< Sine wave vertical oscillation pattern (intermediate)
36 AGGRESSIVE = 2, ///< Actively pursue nearest player (advanced behavior)
37 STATIONARY = 3, ///< Stay in place and shoot at players (turret-like)
38 ZIGZAG = 4 ///< Zigzag diagonal movement pattern (erratic)
39 };
40
41 ///
42 /// @class EnemyAISystem
43 /// @brief Server-authoritative enemy AI system managing all enemy behaviors
44 /// @details This ECS system controls all enemy logic on the game server, including:
45 /// - Multiple movement patterns (straight, sine wave, aggressive, zigzag, stationary)
46 /// - Per-enemy-type specialized AI behaviors
47 /// - Shooting logic with randomized timing
48 /// - Player tracking and pursuit
49 /// - Screen boundary clamping
50 /// - Per-entity timing for independent movement patterns
51 ///
52 /// The system processes three enemy types:
53 /// - Basic enemies: Simple movement patterns, occasional shooting
54 /// - Advanced enemies: More complex patterns, more aggressive
55 /// - Boss enemies: Special behaviors, spread shot patterns, higher durability
56 ///
57 /// All AI logic runs server-side to maintain authoritative gameplay state.
58 ///
59 /// @namespace gme
60 ///
61 class EnemyAISystem final : public ecs::ASystem
62 {
63 public:
64 ///
65 /// @brief Constructor
66 /// @param registry ECS registry containing all entities and components
67 /// @param entityManager Entity manager for spawning projectiles and effects
68 /// @details Initializes the AI system with random number generation for shooting patterns
69 ///
70 explicit EnemyAISystem(ecs::Registry &registry, EntityManager &entityManager);
71
72 ///
73 /// @brief Destructor
74 ///
75 ~EnemyAISystem() override = default;
76
77 /// @brief Deleted copy constructor (non-copyable)
78 EnemyAISystem(const EnemyAISystem &) = delete;
79 /// @brief Deleted copy assignment operator (non-copyable)
81 /// @brief Deleted move constructor (non-movable)
83 /// @brief Deleted move assignment operator (non-movable)
85
86 ///
87 /// @brief Update the enemy AI system (called each frame)
88 /// @param registry ECS registry containing all entities
89 /// @param deltaTime Time elapsed since last frame (in seconds)
90 /// @details Processes AI logic for all enemy entities including movement, shooting,
91 /// and behavior pattern updates
92 ///
93 void update(ecs::Registry &registry, float deltaTime) override;
94
95 ///
96 /// @brief Set global enemy aggressiveness multiplier
97 /// @param value Aggressiveness factor (0.0-2.0, default 1.0)
98 /// @details Higher values make enemies pursue players more aggressively.
99 /// Affects movement speed and shooting frequency.
100 ///
101 void setAggressiveness(float value) { m_aggressiveness = value; }
102
103 ///
104 /// @brief Get current aggressiveness setting
105 /// @return Current aggressiveness multiplier
106 ///
107 float getAggressiveness() const { return m_aggressiveness; }
108
109 private:
110 ecs::Registry &m_registry; ///< ECS registry reference
111 EntityManager &m_entityManager; ///< Entity manager for spawning projectiles
112
113 float m_aggressiveness; ///< Global aggressiveness multiplier (affects pursuit speed)
114 std::mt19937 m_rng; ///< Random number generator for shooting patterns
115 std::uniform_real_distribution<float> m_shootChance; ///< Random distribution for shoot probability
116
117 // Per-entity timing data to avoid static variable synchronization issues
118 std::unordered_map<std::uint32_t, float> m_enemyMovementTimers; ///< Movement timing per enemy ID
119 std::unordered_map<std::uint32_t, float> m_bossSpreadTimers; ///< Boss spread shot timing per boss ID
120
121 ///
122 /// @brief Update AI logic for basic enemy type
123 /// @param enemyId Network ID of the enemy
124 /// @param enemy ECS entity handle
125 /// @param deltaTime Time elapsed since last frame
126 /// @details Implements simple movement patterns and occasional shooting
127 ///
128 void updateBasicEnemyAI(std::uint32_t enemyId, ecs::Entity enemy, float deltaTime);
129
130 ///
131 /// @brief Update AI logic for advanced enemy type
132 /// @param enemyId Network ID of the enemy
133 /// @param enemy ECS entity handle
134 /// @param deltaTime Time elapsed since last frame
135 /// @details Implements more complex movement patterns and frequent shooting
136 ///
137 void updateAdvancedEnemyAI(std::uint32_t enemyId, ecs::Entity enemy, float deltaTime);
138
139 ///
140 /// @brief Update AI logic for boss enemy type
141 /// @param enemyId Network ID of the boss
142 /// @param enemy ECS entity handle
143 /// @param deltaTime Time elapsed since last frame
144 /// @details Implements special boss behaviors including spread shot patterns
145 ///
146 void updateBossAI(std::uint32_t enemyId, ecs::Entity enemy, float deltaTime);
147
148 ///
149 /// @brief Apply sine wave vertical movement pattern
150 /// @param enemyId Network ID of the enemy (for independent timing)
151 /// @param enemy ECS entity handle
152 /// @param deltaTime Time elapsed since last frame
153 /// @param frequency Oscillation frequency in Hz (default: 2.0)
154 /// @param amplitude Vertical oscillation amplitude in pixels (default: 50.0)
155 /// @details Creates smooth vertical oscillation while moving left
156 ///
157 void applySineWaveMovement(std::uint32_t enemyId, ecs::Entity enemy, float deltaTime,
158 float frequency = 2.0f, float amplitude = 50.0f);
159
160 ///
161 /// @brief Apply aggressive pursuit movement toward nearest player
162 /// @param enemy ECS entity handle
163 /// @param deltaTime Time elapsed since last frame
164 /// @param speed Movement speed in pixels per second (default: 150.0)
165 /// @details Enemy moves directly toward nearest player position
166 ///
167 void applyAggressiveMovement(ecs::Entity enemy, float deltaTime, float speed = 150.0f);
168
169 ///
170 /// @brief Apply zigzag diagonal movement pattern
171 /// @param enemyId Network ID of the enemy (for independent timing)
172 /// @param enemy ECS entity handle
173 /// @param deltaTime Time elapsed since last frame
174 /// @details Creates erratic zigzag movement pattern
175 ///
176 void applyZigzagMovement(std::uint32_t enemyId, ecs::Entity enemy, float deltaTime);
177
178 ///
179 /// @brief Attempt to shoot projectile at player
180 /// @param enemyId Network ID of the enemy
181 /// @param enemy ECS entity handle
182 /// @param deltaTime Time elapsed since last frame
183 /// @details Checks shooting conditions and spawns projectile with random chance
184 ///
185 void tryShoot(std::uint32_t enemyId, ecs::Entity enemy, float deltaTime);
186
187 ///
188 /// @brief Check if enemy is able to shoot (cooldown and component checks)
189 /// @param enemy ECS entity handle
190 /// @return True if enemy can shoot
191 /// @details Verifies entity has required components and shooting cooldown has elapsed
192 ///
193 bool canShoot(ecs::Entity enemy) const;
194
195 ///
196 /// @brief Find the nearest player entity to a given position
197 /// @param x X coordinate to search from
198 /// @param y Y coordinate to search from
199 /// @return Entity handle of nearest player, or invalid entity if none found
200 /// @details Used for aggressive AI targeting
201 ///
202 ecs::Entity findNearestPlayer(float x, float y) const;
203
204 ///
205 /// @brief Calculate distance to nearest player
206 /// @param x X coordinate to measure from
207 /// @param y Y coordinate to measure from
208 /// @return Distance to nearest player in pixels, or large value if no players
209 /// @details Used for distance-based behavior decisions
210 ///
211 float getDistanceToNearestPlayer(float x, float y) const;
212
213 ///
214 /// @brief Clamp enemy position to visible screen bounds
215 /// @param enemy ECS entity handle
216 /// @details Prevents enemies from moving off-screen vertically
217 ///
218 void clampToScreen(ecs::Entity enemy);
219 };
220
221} // namespace gme
This file contains the component definitions.
Centralized entity lifecycle management system for R-Type server.
This file contains the entity definitions.
This file contains the interface for systems.
This file contains the Registry class declaration.
Abstract class for system.
Definition ISystems.hpp:34
Class for managing entities and their components.
Definition Registry.hpp:25
Server-authoritative enemy AI system managing all enemy behaviors.
float getDistanceToNearestPlayer(float x, float y) const
Calculate distance to nearest player.
std::unordered_map< std::uint32_t, float > m_bossSpreadTimers
Boss spread shot timing per boss ID.
std::unordered_map< std::uint32_t, float > m_enemyMovementTimers
Movement timing per enemy ID.
EnemyAISystem(ecs::Registry &registry, EntityManager &entityManager)
Constructor.
void updateAdvancedEnemyAI(std::uint32_t enemyId, ecs::Entity enemy, float deltaTime)
Update AI logic for advanced enemy type.
void applySineWaveMovement(std::uint32_t enemyId, ecs::Entity enemy, float deltaTime, float frequency=2.0f, float amplitude=50.0f)
Apply sine wave vertical movement pattern.
void tryShoot(std::uint32_t enemyId, ecs::Entity enemy, float deltaTime)
Attempt to shoot projectile at player.
ecs::Registry & m_registry
ECS registry reference.
void applyAggressiveMovement(ecs::Entity enemy, float deltaTime, float speed=150.0f)
Apply aggressive pursuit movement toward nearest player.
EnemyAISystem & operator=(EnemyAISystem &&)=delete
Deleted move assignment operator (non-movable)
bool canShoot(ecs::Entity enemy) const
Check if enemy is able to shoot (cooldown and component checks)
EnemyAISystem & operator=(const EnemyAISystem &)=delete
Deleted copy assignment operator (non-copyable)
float m_aggressiveness
Global aggressiveness multiplier (affects pursuit speed)
void setAggressiveness(float value)
Set global enemy aggressiveness multiplier.
EnemyAISystem(const EnemyAISystem &)=delete
Deleted copy constructor (non-copyable)
EntityManager & m_entityManager
Entity manager for spawning projectiles.
void update(ecs::Registry &registry, float deltaTime) override
Update the enemy AI system (called each frame)
EnemyAISystem(EnemyAISystem &&)=delete
Deleted move constructor (non-movable)
void updateBasicEnemyAI(std::uint32_t enemyId, ecs::Entity enemy, float deltaTime)
Update AI logic for basic enemy type.
~EnemyAISystem() override=default
Destructor.
ecs::Entity findNearestPlayer(float x, float y) const
Find the nearest player entity to a given position.
std::uniform_real_distribution< float > m_shootChance
Random distribution for shoot probability.
void clampToScreen(ecs::Entity enemy)
Clamp enemy position to visible screen bounds.
std::mt19937 m_rng
Random number generator for shooting patterns.
void updateBossAI(std::uint32_t enemyId, ecs::Entity enemy, float deltaTime)
Update AI logic for boss enemy type.
float getAggressiveness() const
Get current aggressiveness setting.
void applyZigzagMovement(std::uint32_t enemyId, ecs::Entity enemy, float deltaTime)
Apply zigzag diagonal movement pattern.
Central entity lifecycle manager for the R-Type game server.
std::uint32_t Entity
Definition Entity.hpp:13
EnemyBehavior
Enumeration of enemy movement and behavior patterns.
@ SINE_WAVE
Sine wave vertical oscillation pattern (intermediate)
@ STATIONARY
Stay in place and shoot at players (turret-like)
@ AGGRESSIVE
Actively pursue nearest player (advanced behavior)
@ STRAIGHT
Move straight left at constant speed (basic behavior)
@ ZIGZAG
Zigzag diagonal movement pattern (erratic)