17 : m_registry(registry), m_entityManager(entityManager), m_aggressiveness(1.0f), m_shootChance(0.0f, 1.0f)
19 auto seed =
static_cast<unsigned int>(std::chrono::system_clock::now().time_since_epoch().count());
30 for (
const auto &[enemyId, enemyEntity] : enemies)
33 if (!metadata || !metadata->isActive)
37 switch (metadata->type)
64 velocity->
x = -200.0f;
98 if (distanceToPlayer < 400.0f)
104 tryShoot(enemyId, enemy, deltaTime);
113 if (!transform || !velocity || !enemyComp)
129 float healthPercent = enemyComp->health / enemyComp->max_health;
131 if (healthPercent > 0.66f)
134 velocity->
x = -30.0f;
138 enemyComp->shoot_cooldown = 1.5f;
140 else if (healthPercent > 0.33f)
146 enemyComp->shoot_cooldown = 1.0f;
151 velocity->
x = -50.0f;
155 enemyComp->shoot_cooldown = 0.5f;
159 tryShoot(enemyId, enemy, deltaTime);
166 for (
int i = -1; i <= 1; i++)
171 float angle = i * 15.0f * (3.14159f / 180.0f);
172 float vx = -300.0f * std::cos(angle);
173 float vy = -300.0f * std::sin(angle);
176 transform->y, vx, vy);
183 float frequency,
float amplitude)
188 if (!transform || !velocity)
192 float baseVelocityX = velocity->
x;
193 if (baseVelocityX == 0.0f)
194 baseVelocityX = -150.0f;
198 float sineValue = std::sin(time * frequency);
199 velocity->
y = sineValue * amplitude;
200 velocity->
x = baseVelocityX;
208 if (!transform || !velocity)
216 velocity->
x = -speed;
222 if (!playerTransform)
226 float dx = playerTransform->
x - transform->x;
227 float dy = playerTransform->y - transform->y;
228 float distance = std::sqrt(dx * dx + dy * dy);
230 if (distance > 0.01f)
243 if (!transform || !velocity)
251 float phase = std::fmod(zigzagTime, period * 2.0f);
253 velocity->
x = -150.0f;
254 velocity->
y = (phase < period) ? 100.0f : -100.0f;
262 if (!enemyComp || !transform)
266 enemyComp->last_shot_time += deltaTime;
274 if (randomValue > 0.3f)
279 if (distanceToPlayer > 1500.0f)
283 float projectileVx = -400.0f;
284 float projectileVy = 0.0f;
293 float dx = playerTransform->
x - transform->x;
294 float dy = playerTransform->y - transform->y;
295 float distance = std::sqrt(dx * dx + dy * dy);
298 if (distance > 0.01f && dx < 0.0f)
301 float projectileSpeed = 500.0f;
302 projectileVx = (dx / distance) * projectileSpeed;
303 projectileVy = (dy / distance) * projectileSpeed;
316 enemyComp->last_shot_time = 0.0f;
336 float nearestDistance = std::numeric_limits<float>::max();
338 for (
const auto &[playerId, playerEntity] : players)
344 float dx = transform->
x - x;
345 float dy = transform->y - y;
346 float distance = std::sqrt(dx * dx + dy * dy);
348 if (distance < nearestDistance)
350 nearestDistance = distance;
351 nearestPlayer = playerEntity;
355 return nearestPlayer;
362 float nearestDistance = std::numeric_limits<float>::max();
364 for (
const auto &[playerId, playerEntity] : players)
370 float dx = transform->
x - x;
371 float dy = transform->y - y;
372 float distance = std::sqrt(dx * dx + dy * dy);
374 if (distance < nearestDistance)
376 nearestDistance = distance;
380 return nearestDistance;
389 const float MARGIN = 50.0f;
Server-side enemy AI and behavior system for R-Type.
Configuration constants for the multiplayer game.
This file contains the Logger class.
Class for managing entities and their components.
T * getComponent(Entity e)
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 ®istry, 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.
bool canShoot(ecs::Entity enemy) const
Check if enemy is able to shoot (cooldown and component checks)
float m_aggressiveness
Global aggressiveness multiplier (affects pursuit speed)
EntityManager & m_entityManager
Entity manager for spawning projectiles.
void update(ecs::Registry ®istry, float deltaTime) override
Update the enemy AI system (called each frame)
void updateBasicEnemyAI(std::uint32_t enemyId, ecs::Entity enemy, float deltaTime)
Update AI logic for basic enemy type.
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.
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.
const std::unordered_map< std::uint32_t, ecs::Entity > & getEnemies() const
Get all enemy entities.
std::uint32_t getNetworkIdForEntity(ecs::Entity entity) const
Get network ID for an entity handle.
EntityMetadata * getEntityMetadata(std::uint32_t networkId)
Get entity metadata by network ID.
const std::unordered_map< std::uint32_t, ecs::Entity > & getPlayers() const
Get all player entities.
ecs::Entity createEnemyProjectile(std::uint32_t enemyId, float x, float y, float vx, float vy)
Create an enemy projectile entity.
static void log(const std::string &message, const LogLevel &logLevel)
constexpr Entity INVALID_ENTITY
@ ENEMY_ADVANCED
Advanced enemy type (complex behavior)
@ ENEMY_BASIC
Basic enemy type (simple behavior)
@ BOSS
Boss enemy (high health, special patterns)
constexpr float SCREEN_WIDTH
constexpr float SCREEN_HEIGHT