r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
gme::EnemyAISystem Class Referencefinal

Server-authoritative enemy AI system managing all enemy behaviors. More...

#include <EnemyAISystem.hpp>

+ Inheritance diagram for gme::EnemyAISystem:
+ Collaboration diagram for gme::EnemyAISystem:

Public Member Functions

 EnemyAISystem (ecs::Registry &registry, EntityManager &entityManager)
 Constructor.
 
 ~EnemyAISystem () override=default
 Destructor.
 
 EnemyAISystem (const EnemyAISystem &)=delete
 Deleted copy constructor (non-copyable)
 
EnemyAISystemoperator= (const EnemyAISystem &)=delete
 Deleted copy assignment operator (non-copyable)
 
 EnemyAISystem (EnemyAISystem &&)=delete
 Deleted move constructor (non-movable)
 
EnemyAISystemoperator= (EnemyAISystem &&)=delete
 Deleted move assignment operator (non-movable)
 
void update (ecs::Registry &registry, float deltaTime) override
 Update the enemy AI system (called each frame)
 
void setAggressiveness (float value)
 Set global enemy aggressiveness multiplier.
 
float getAggressiveness () const
 Get current aggressiveness setting.
 
- Public Member Functions inherited from ecs::ASystem
bool isEnable () override
 
void setEnable (const bool enable) override
 
- Public Member Functions inherited from ecs::ISystem
virtual ~ISystem ()=default
 

Private Member Functions

void updateBasicEnemyAI (std::uint32_t enemyId, ecs::Entity enemy, float deltaTime)
 Update AI logic for basic enemy type.
 
void updateAdvancedEnemyAI (std::uint32_t enemyId, ecs::Entity enemy, float deltaTime)
 Update AI logic for advanced enemy type.
 
void updateBossAI (std::uint32_t enemyId, ecs::Entity enemy, float deltaTime)
 Update AI logic for boss 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 applyAggressiveMovement (ecs::Entity enemy, float deltaTime, float speed=150.0f)
 Apply aggressive pursuit movement toward nearest player.
 
void applyZigzagMovement (std::uint32_t enemyId, ecs::Entity enemy, float deltaTime)
 Apply zigzag diagonal movement pattern.
 
void tryShoot (std::uint32_t enemyId, ecs::Entity enemy, float deltaTime)
 Attempt to shoot projectile at player.
 
bool canShoot (ecs::Entity enemy) const
 Check if enemy is able to shoot (cooldown and component checks)
 
ecs::Entity findNearestPlayer (float x, float y) const
 Find the nearest player entity to a given position.
 
float getDistanceToNearestPlayer (float x, float y) const
 Calculate distance to nearest player.
 
void clampToScreen (ecs::Entity enemy)
 Clamp enemy position to visible screen bounds.
 

Private Attributes

ecs::Registrym_registry
 ECS registry reference.
 
EntityManagerm_entityManager
 Entity manager for spawning projectiles.
 
float m_aggressiveness
 Global aggressiveness multiplier (affects pursuit speed)
 
std::mt19937 m_rng
 Random number generator for shooting patterns.
 
std::uniform_real_distribution< float > m_shootChance
 Random distribution for shoot probability.
 
std::unordered_map< std::uint32_t, float > m_enemyMovementTimers
 Movement timing per enemy ID.
 
std::unordered_map< std::uint32_t, float > m_bossSpreadTimers
 Boss spread shot timing per boss ID.
 

Detailed Description

Server-authoritative enemy AI system managing all enemy behaviors.

This ECS system controls all enemy logic on the game server, including:

  • Multiple movement patterns (straight, sine wave, aggressive, zigzag, stationary)
  • Per-enemy-type specialized AI behaviors
  • Shooting logic with randomized timing
  • Player tracking and pursuit
  • Screen boundary clamping
  • Per-entity timing for independent movement patterns

The system processes three enemy types:

  • Basic enemies: Simple movement patterns, occasional shooting
  • Advanced enemies: More complex patterns, more aggressive
  • Boss enemies: Special behaviors, spread shot patterns, higher durability

All AI logic runs server-side to maintain authoritative gameplay state.

Definition at line 61 of file EnemyAISystem.hpp.

Constructor & Destructor Documentation

◆ EnemyAISystem() [1/3]

gme::EnemyAISystem::EnemyAISystem ( ecs::Registry & registry,
EntityManager & entityManager )
explicit

Constructor.

Parameters
registryECS registry containing all entities and components
entityManagerEntity manager for spawning projectiles and effects

Initializes the AI system with random number generation for shooting patterns

Definition at line 16 of file EnemyAISystem.cpp.

References utl::INFO, utl::Logger::log(), and m_rng.

+ Here is the call graph for this function:

◆ ~EnemyAISystem()

gme::EnemyAISystem::~EnemyAISystem ( )
overridedefault

Destructor.

◆ EnemyAISystem() [2/3]

gme::EnemyAISystem::EnemyAISystem ( const EnemyAISystem & )
delete

Deleted copy constructor (non-copyable)

◆ EnemyAISystem() [3/3]

gme::EnemyAISystem::EnemyAISystem ( EnemyAISystem && )
delete

Deleted move constructor (non-movable)

Member Function Documentation

◆ applyAggressiveMovement()

void gme::EnemyAISystem::applyAggressiveMovement ( ecs::Entity enemy,
float deltaTime,
float speed = 150.0f )
private

Apply aggressive pursuit movement toward nearest player.

Parameters
enemyECS entity handle
deltaTimeTime elapsed since last frame
speedMovement speed in pixels per second (default: 150.0)

Enemy moves directly toward nearest player position

Definition at line 203 of file EnemyAISystem.cpp.

References findNearestPlayer(), ecs::Registry::getComponent(), ecs::INVALID_ENTITY, m_aggressiveness, m_registry, ecs::Transform::x, and ecs::Transform::y.

Referenced by updateAdvancedEnemyAI(), and updateBossAI().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ applySineWaveMovement()

void gme::EnemyAISystem::applySineWaveMovement ( std::uint32_t enemyId,
ecs::Entity enemy,
float deltaTime,
float frequency = 2.0f,
float amplitude = 50.0f )
private

Apply sine wave vertical movement pattern.

Parameters
enemyIdNetwork ID of the enemy (for independent timing)
enemyECS entity handle
deltaTimeTime elapsed since last frame
frequencyOscillation frequency in Hz (default: 2.0)
amplitudeVertical oscillation amplitude in pixels (default: 50.0)

Creates smooth vertical oscillation while moving left

Definition at line 182 of file EnemyAISystem.cpp.

References ecs::Registry::getComponent(), m_enemyMovementTimers, m_registry, ecs::Transform::x, and ecs::Transform::y.

Referenced by updateAdvancedEnemyAI(), and updateBossAI().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ applyZigzagMovement()

void gme::EnemyAISystem::applyZigzagMovement ( std::uint32_t enemyId,
ecs::Entity enemy,
float deltaTime )
private

Apply zigzag diagonal movement pattern.

Parameters
enemyIdNetwork ID of the enemy (for independent timing)
enemyECS entity handle
deltaTimeTime elapsed since last frame

Creates erratic zigzag movement pattern

Definition at line 238 of file EnemyAISystem.cpp.

References ecs::Registry::getComponent(), m_enemyMovementTimers, m_registry, ecs::Transform::x, and ecs::Transform::y.

Referenced by updateBossAI().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ canShoot()

bool gme::EnemyAISystem::canShoot ( ecs::Entity enemy) const
private

Check if enemy is able to shoot (cooldown and component checks)

Parameters
enemyECS entity handle
Returns
True if enemy can shoot

Verifies entity has required components and shooting cooldown has elapsed

Definition at line 322 of file EnemyAISystem.cpp.

References ecs::Registry::getComponent(), ecs::Enemy::last_shot_time, and m_registry.

Referenced by tryShoot().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ clampToScreen()

void gme::EnemyAISystem::clampToScreen ( ecs::Entity enemy)
private

Clamp enemy position to visible screen bounds.

Parameters
enemyECS entity handle

Prevents enemies from moving off-screen vertically

Definition at line 383 of file EnemyAISystem.cpp.

References ecs::Registry::getComponent(), m_registry, utl::GameConfig::Server::SCREEN_HEIGHT, utl::GameConfig::Server::SCREEN_WIDTH, and ecs::Transform::x.

Referenced by update().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ findNearestPlayer()

ecs::Entity gme::EnemyAISystem::findNearestPlayer ( float x,
float y ) const
private

Find the nearest player entity to a given position.

Parameters
xX coordinate to search from
yY coordinate to search from
Returns
Entity handle of nearest player, or invalid entity if none found

Used for aggressive AI targeting

Definition at line 331 of file EnemyAISystem.cpp.

References ecs::Registry::getComponent(), gme::EntityManager::getPlayers(), ecs::INVALID_ENTITY, m_entityManager, m_registry, and ecs::Transform::x.

Referenced by applyAggressiveMovement(), and tryShoot().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getAggressiveness()

float gme::EnemyAISystem::getAggressiveness ( ) const
inline

Get current aggressiveness setting.

Returns
Current aggressiveness multiplier

Definition at line 107 of file EnemyAISystem.hpp.

References m_aggressiveness.

◆ getDistanceToNearestPlayer()

float gme::EnemyAISystem::getDistanceToNearestPlayer ( float x,
float y ) const
private

Calculate distance to nearest player.

Parameters
xX coordinate to measure from
yY coordinate to measure from
Returns
Distance to nearest player in pixels, or large value if no players

Used for distance-based behavior decisions

Definition at line 358 of file EnemyAISystem.cpp.

References ecs::Registry::getComponent(), gme::EntityManager::getPlayers(), m_entityManager, m_registry, and ecs::Transform::x.

Referenced by tryShoot(), and updateAdvancedEnemyAI().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ operator=() [1/2]

EnemyAISystem & gme::EnemyAISystem::operator= ( const EnemyAISystem & )
delete

Deleted copy assignment operator (non-copyable)

◆ operator=() [2/2]

EnemyAISystem & gme::EnemyAISystem::operator= ( EnemyAISystem && )
delete

Deleted move assignment operator (non-movable)

◆ setAggressiveness()

void gme::EnemyAISystem::setAggressiveness ( float value)
inline

Set global enemy aggressiveness multiplier.

Parameters
valueAggressiveness factor (0.0-2.0, default 1.0)

Higher values make enemies pursue players more aggressively. Affects movement speed and shooting frequency.

Definition at line 101 of file EnemyAISystem.hpp.

References m_aggressiveness.

◆ tryShoot()

void gme::EnemyAISystem::tryShoot ( std::uint32_t enemyId,
ecs::Entity enemy,
float deltaTime )
private

Attempt to shoot projectile at player.

Parameters
enemyIdNetwork ID of the enemy
enemyECS entity handle
deltaTimeTime elapsed since last frame

Checks shooting conditions and spawns projectile with random chance

Definition at line 257 of file EnemyAISystem.cpp.

References canShoot(), gme::EntityManager::createEnemyProjectile(), findNearestPlayer(), ecs::Registry::getComponent(), getDistanceToNearestPlayer(), ecs::INVALID_ENTITY, m_entityManager, m_registry, m_rng, m_shootChance, and ecs::Transform::x.

Referenced by updateAdvancedEnemyAI(), updateBasicEnemyAI(), and updateBossAI().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ update()

void gme::EnemyAISystem::update ( ecs::Registry & registry,
float deltaTime )
overridevirtual

Update the enemy AI system (called each frame)

Parameters
registryECS registry containing all entities
deltaTimeTime elapsed since last frame (in seconds)

Processes AI logic for all enemy entities including movement, shooting, and behavior pattern updates

Implements ecs::ISystem.

Definition at line 25 of file EnemyAISystem.cpp.

References gme::BOSS, clampToScreen(), gme::ENEMY_ADVANCED, gme::ENEMY_BASIC, gme::EntityManager::getEnemies(), gme::EntityManager::getEntityMetadata(), m_entityManager, updateAdvancedEnemyAI(), updateBasicEnemyAI(), and updateBossAI().

+ Here is the call graph for this function:

◆ updateAdvancedEnemyAI()

void gme::EnemyAISystem::updateAdvancedEnemyAI ( std::uint32_t enemyId,
ecs::Entity enemy,
float deltaTime )
private

Update AI logic for advanced enemy type.

Parameters
enemyIdNetwork ID of the enemy
enemyECS entity handle
deltaTimeTime elapsed since last frame

Implements more complex movement patterns and frequent shooting

Definition at line 78 of file EnemyAISystem.cpp.

References applyAggressiveMovement(), applySineWaveMovement(), ecs::Registry::getComponent(), getDistanceToNearestPlayer(), m_enemyMovementTimers, m_registry, and tryShoot().

Referenced by update().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ updateBasicEnemyAI()

void gme::EnemyAISystem::updateBasicEnemyAI ( std::uint32_t enemyId,
ecs::Entity enemy,
float deltaTime )
private

Update AI logic for basic enemy type.

Parameters
enemyIdNetwork ID of the enemy
enemyECS entity handle
deltaTimeTime elapsed since last frame

Implements simple movement patterns and occasional shooting

Definition at line 57 of file EnemyAISystem.cpp.

References ecs::Registry::getComponent(), m_enemyMovementTimers, m_registry, tryShoot(), and ecs::Velocity::x.

Referenced by update().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ updateBossAI()

void gme::EnemyAISystem::updateBossAI ( std::uint32_t enemyId,
ecs::Entity enemy,
float deltaTime )
private

Update AI logic for boss enemy type.

Parameters
enemyIdNetwork ID of the boss
enemyECS entity handle
deltaTimeTime elapsed since last frame

Implements special boss behaviors including spread shot patterns

Definition at line 107 of file EnemyAISystem.cpp.

References applyAggressiveMovement(), applySineWaveMovement(), applyZigzagMovement(), gme::EntityManager::createEnemyProjectile(), ecs::Registry::getComponent(), gme::EntityManager::getNetworkIdForEntity(), m_bossSpreadTimers, m_enemyMovementTimers, m_entityManager, m_registry, tryShoot(), and ecs::Transform::x.

Referenced by update().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ m_aggressiveness

float gme::EnemyAISystem::m_aggressiveness
private

Global aggressiveness multiplier (affects pursuit speed)

Definition at line 113 of file EnemyAISystem.hpp.

Referenced by applyAggressiveMovement(), getAggressiveness(), and setAggressiveness().

◆ m_bossSpreadTimers

std::unordered_map<std::uint32_t, float> gme::EnemyAISystem::m_bossSpreadTimers
private

Boss spread shot timing per boss ID.

Definition at line 119 of file EnemyAISystem.hpp.

Referenced by updateBossAI().

◆ m_enemyMovementTimers

std::unordered_map<std::uint32_t, float> gme::EnemyAISystem::m_enemyMovementTimers
private

Movement timing per enemy ID.

Definition at line 118 of file EnemyAISystem.hpp.

Referenced by applySineWaveMovement(), applyZigzagMovement(), updateAdvancedEnemyAI(), updateBasicEnemyAI(), and updateBossAI().

◆ m_entityManager

EntityManager& gme::EnemyAISystem::m_entityManager
private

Entity manager for spawning projectiles.

Definition at line 111 of file EnemyAISystem.hpp.

Referenced by findNearestPlayer(), getDistanceToNearestPlayer(), tryShoot(), update(), and updateBossAI().

◆ m_registry

◆ m_rng

std::mt19937 gme::EnemyAISystem::m_rng
private

Random number generator for shooting patterns.

Definition at line 114 of file EnemyAISystem.hpp.

Referenced by EnemyAISystem(), and tryShoot().

◆ m_shootChance

std::uniform_real_distribution<float> gme::EnemyAISystem::m_shootChance
private

Random distribution for shoot probability.

Definition at line 115 of file EnemyAISystem.hpp.

Referenced by tryShoot().


The documentation for this class was generated from the following files: