Server-authoritative collision detection and response system. More...
#include <Collision.hpp>
Inheritance diagram for gme::CollisionSystem:
Collaboration diagram for gme::CollisionSystem:Public Member Functions | |
| CollisionSystem (const std::shared_ptr< eng::IRenderer > &renderer, bool &showDebug) | |
| ~CollisionSystem () override=default | |
| CollisionSystem (const CollisionSystem &)=delete | |
| CollisionSystem & | operator= (const CollisionSystem &)=delete |
| CollisionSystem (CollisionSystem &&)=delete | |
| CollisionSystem & | operator= (const CollisionSystem &&)=delete |
| bool | isEnable () override |
| void | setEnable (bool enable) override |
| void | update (ecs::Registry ®istry, float dt) override |
| CollisionSystem (const std::shared_ptr< eng::IRenderer > &renderer, bool &showDebug) | |
| ~CollisionSystem () override=default | |
| CollisionSystem (const CollisionSystem &)=delete | |
| CollisionSystem & | operator= (const CollisionSystem &)=delete |
| CollisionSystem (CollisionSystem &&)=delete | |
| CollisionSystem & | operator= (CollisionSystem &&)=delete |
| bool | isEnable () override |
| void | setEnable (const bool enable) override |
| void | update (ecs::Registry ®istry, float dt) override |
| CollisionSystem (ecs::Registry ®istry, EntityManager &entityManager) | |
| Constructor. | |
| ~CollisionSystem () override=default | |
| Destructor. | |
| CollisionSystem (const CollisionSystem &)=delete | |
| Deleted copy constructor (non-copyable) | |
| CollisionSystem & | operator= (const CollisionSystem &)=delete |
| Deleted copy assignment operator (non-copyable) | |
| CollisionSystem (CollisionSystem &&)=delete | |
| Deleted move constructor (non-movable) | |
| CollisionSystem & | operator= (CollisionSystem &&)=delete |
| Deleted move assignment operator (non-movable) | |
| void | update (ecs::Registry ®istry, float deltaTime) override |
| Update the collision system (called each frame) | |
| size_t | getCollisionCount () const |
| Get the number of collisions detected this frame. | |
| void | resetCollisionCount () |
| Reset the collision counter to zero. | |
Public Member Functions inherited from ecs::ISystem | |
| virtual | ~ISystem ()=default |
Private Member Functions | |
| void | ensureEnemyDeathChannel (ecs::Registry ®istry, const std::size_t channelIndex) |
| void | playEnemyDeathSound (ecs::Registry ®istry) |
| void | ensurePlayerDeathAudio (ecs::Registry ®istry) |
| void | playPlayerDeathSound (ecs::Registry ®istry) |
| bool | checkCircleCollision (float x1, float y1, float r1, float x2, float y2, float r2, float *overlapDist=nullptr) const |
| Check for circular collision between two entities. | |
| void | handlePlayerProjectileEnemyCollision () |
| Handle collisions between player projectiles and enemies. | |
| void | handleEnemyProjectilePlayerCollision () |
| Handle collisions between enemy projectiles and players. | |
| void | handlePlayerEnemyCollision () |
| Handle collisions between players and enemies (ramming) | |
| void | handlePlayerPowerupCollision () |
| Handle collisions between players and powerups. | |
| bool | getCollisionInfo (ecs::Entity entity, float &x, float &y, float &radius) const |
| Extract collision information from an entity. | |
| void | applyDamageToEnemy (std::uint32_t enemyId, float damage, std::uint32_t attackerPlayerId=0) |
| Apply damage to an enemy entity. | |
| void | applyDamageToPlayer (std::uint32_t playerId, float damage) |
| Apply damage to a player entity. | |
Static Private Member Functions | |
| static bool | checkCircularCollision (const ecs::Transform &transform1, const ecs::Hitbox &hitbox1, const ecs::Transform &transform2, const ecs::Hitbox &hitbox2) |
| static void | removeProjectile (ecs::Registry ®istry, ecs::Entity entity) |
| static void | removeEnemy (ecs::Registry ®istry, ecs::Entity entity) |
| static void | createExplosion (ecs::Registry ®istry, float x, float y) |
Private Attributes | |
| const std::shared_ptr< eng::IRenderer > & | m_renderer |
| bool & | m_showDebug |
| std::array< ecs::Entity, 4 > | m_enemyDeathAudioEntities {} |
| ecs::Entity | m_playerDeathAudioEntity = ecs::INVALID_ENTITY |
| std::size_t | m_nextEnemyDeathChannel = 0 |
| bool | m_wasPlayerPresent = false |
| ecs::Registry & | m_registry |
| ECS registry reference. | |
| EntityManager & | m_entityManager |
| Entity manager reference for spawning/destruction. | |
| size_t | m_collisionCount = 0 |
| Counter for collisions detected this frame. | |
Server-authoritative collision detection and response system.
This ECS system manages all collision detection and resolution on the server. It performs the following operations each frame:
The system uses circular collision detection (radius-based) which is computationally efficient for the types of entities in R-Type. All collision resolution is handled server-side to maintain authoritative game state.
Definition at line 57 of file CollisionSystem.hpp.
|
inlineexplicit |
Definition at line 15 of file Collision.hpp.
|
overridedefault |
|
delete |
|
delete |
|
inlineexplicit |
Definition at line 28 of file Collision.hpp.
References ecs::INVALID_ENTITY, and m_enemyDeathAudioEntities.
|
overridedefault |
|
delete |
|
delete |
|
inlineexplicit |
Constructor.
| registry | ECS registry containing all entities and components |
| entityManager | Entity manager for spawning/destroying entities |
Initializes the collision system with references to required managers
Definition at line 66 of file CollisionSystem.hpp.
|
overridedefault |
Destructor.
|
delete |
Deleted copy constructor (non-copyable)
|
delete |
Deleted move constructor (non-movable)
|
private |
Apply damage to an enemy entity.
| enemyId | Network ID of the enemy entity |
| damage | Amount of damage to apply |
| attackerPlayerId | Session ID of player who dealt damage (for score attribution) |
Reduces enemy health and destroys entity if health reaches zero. Awards score to attacker if enemy is destroyed.
Definition at line 196 of file CollisionSystem.cpp.
References gme::EntityManager::addScore(), gme::BOSS, gme::EntityManager::destroyEnemy(), gme::ENEMY_ADVANCED, gme::ENEMY_BASIC, ecs::Registry::getComponent(), gme::EntityManager::getEnemy(), gme::EntityManager::getEntityMetadata(), ecs::Enemy::health, utl::INFO, ecs::INVALID_ENTITY, utl::Logger::log(), m_entityManager, and m_registry.
Referenced by handlePlayerEnemyCollision(), and handlePlayerProjectileEnemyCollision().
Here is the call graph for this function:
Here is the caller graph for this function:
|
private |
Apply damage to a player entity.
| playerId | Session ID of the player |
| damage | Amount of damage to apply |
Reduces player health. Player death is handled separately.
Definition at line 251 of file CollisionSystem.cpp.
References ecs::Registry::getComponent(), gme::EntityManager::getPlayerEntity(), utl::INFO, utl::Logger::log(), m_entityManager, m_registry, gme::EntityManager::markPlayerAsDead(), and utl::WARNING.
Referenced by handleEnemyProjectilePlayerCollision(), and handlePlayerEnemyCollision().
Here is the call graph for this function:
Here is the caller graph for this function:
|
inlineprivate |
Check for circular collision between two entities.
Inline implementation of circular collision detection.
| x1 | X position of first entity center |
| y1 | Y position of first entity center |
| r1 | Radius of first entity |
| x2 | X position of second entity center |
| y2 | Y position of second entity center |
| r2 | Radius of second entity |
| overlapDist | Optional output parameter for overlap distance |
Uses distance formula: sqrt((x2-x1)² + (y2-y1)²) < (r1 + r2)
| x1 | X position of first circle center |
| y1 | Y position of first circle center |
| r1 | Radius of first circle |
| x2 | X position of second circle center |
| y2 | Y position of second circle center |
| r2 | Radius of second circle |
| overlapDist | Optional pointer to store overlap distance (radiusSum - actualDistance) |
Calculates Euclidean distance between centers and compares to sum of radii. More efficient than AABB for circular entities.
Definition at line 198 of file CollisionSystem.hpp.
Referenced by handleEnemyProjectilePlayerCollision(), handlePlayerEnemyCollision(), and handlePlayerProjectileEnemyCollision().
Here is the caller graph for this function:
|
inlinestaticprivate |
Definition at line 203 of file Collision.hpp.
References ecs::Hitbox::offsetX, ecs::Hitbox::offsetY, ecs::Hitbox::radius, ecs::Transform::x, and ecs::Transform::y.
Referenced by update().
Here is the caller graph for this function:
|
inlinestaticprivate |
Definition at line 295 of file Collision.hpp.
References utl::GameConfig::Explosion::ANIMATION_DURATION, utl::GameConfig::Explosion::ANIMATION_FRAMES, ecs::Registry::createEntity(), utl::GameConfig::Explosion::FRAMES_PER_ROW, utl::GameConfig::Explosion::LIFETIME, utl::GameConfig::Explosion::SCALE, utl::GameConfig::Explosion::SPRITE_HEIGHT, utl::GameConfig::Explosion::SPRITE_WIDTH, utl::Path::Texture::TEXTURE_EXPLOSION, and ecs::Registry::EntityBuilder::with().
Referenced by update().
Here is the call graph for this function:
Here is the caller graph for this function:
|
inlineprivate |
Definition at line 313 of file Collision.hpp.
References utl::Path::Audio::AUDIO_DEATH_ENEMIES, ecs::Registry::createEntity(), ecs::Registry::hasComponent(), ecs::INVALID_ENTITY, m_enemyDeathAudioEntities, and ecs::Registry::EntityBuilder::with().
Referenced by playEnemyDeathSound().
Here is the call graph for this function:
Here is the caller graph for this function:
|
inlineprivate |
Definition at line 349 of file Collision.hpp.
References utl::Path::Audio::AUDIO_DEATH_ALLIES, ecs::Registry::createEntity(), ecs::Registry::hasComponent(), ecs::INVALID_ENTITY, m_playerDeathAudioEntity, and ecs::Registry::EntityBuilder::with().
Referenced by playPlayerDeathSound().
Here is the call graph for this function:
Here is the caller graph for this function:
|
inline |
Get the number of collisions detected this frame.
Useful for debugging and performance monitoring
Definition at line 99 of file CollisionSystem.hpp.
References m_collisionCount.
|
inlineprivate |
Extract collision information from an entity.
Inline implementation of collision info extraction.
| entity | Entity to get collision info from |
| x | Output: X position of collision center |
| y | Output: Y position of collision center |
| radius | Output: Collision radius |
Calculates collision center from transform position and hitbox offset
| entity | Entity to extract collision info from |
| x | Output parameter for collision center X position |
| y | Output parameter for collision center Y position |
| radius | Output parameter for collision radius |
Combines transform position with hitbox offset to calculate actual collision center
Definition at line 223 of file CollisionSystem.hpp.
References ecs::Registry::getComponent(), m_registry, and ecs::Transform::y.
Referenced by handleEnemyProjectilePlayerCollision(), handlePlayerEnemyCollision(), and handlePlayerProjectileEnemyCollision().
Here is the call graph for this function:
Here is the caller graph for this function:
|
private |
Handle collisions between enemy projectiles and players.
Iterates through all enemy projectiles and players, checks for collisions, applies damage to players, and destroys projectiles on hit
Definition at line 93 of file CollisionSystem.cpp.
References applyDamageToPlayer(), checkCircleCollision(), gme::EntityManager::destroyProjectile(), getCollisionInfo(), ecs::Registry::getComponent(), gme::EntityManager::getEntityMetadata(), gme::EntityManager::getPlayers(), gme::EntityManager::getProjectiles(), m_collisionCount, m_entityManager, m_registry, and gme::PROJECTILE_ENEMY.
Here is the call graph for this function:
|
private |
Handle collisions between players and enemies (ramming)
Checks for direct collisions between players and enemies, applies damage to both entities on contact
Definition at line 144 of file CollisionSystem.cpp.
References applyDamageToEnemy(), applyDamageToPlayer(), checkCircleCollision(), ecs::Enemy::damage, getCollisionInfo(), ecs::Registry::getComponent(), gme::EntityManager::getEnemies(), gme::EntityManager::getEntityMetadata(), gme::EntityManager::getPlayers(), m_collisionCount, m_entityManager, and m_registry.
Here is the call graph for this function:
|
private |
Handle collisions between players and powerups.
Checks for collisions between players and powerup items, applies powerup effects and destroys the powerup entity
Definition at line 190 of file CollisionSystem.cpp.
|
private |
Handle collisions between player projectiles and enemies.
Iterates through all player projectiles and enemies, checks for collisions, applies damage to enemies, and destroys projectiles on hit
Definition at line 27 of file CollisionSystem.cpp.
References applyDamageToEnemy(), checkCircleCollision(), gme::EntityManager::destroyProjectile(), getCollisionInfo(), ecs::Registry::getComponent(), gme::EntityManager::getEnemies(), gme::EntityManager::getEntityMetadata(), gme::EntityManager::getProjectiles(), m_collisionCount, m_entityManager, m_registry, gme::EntityMetadata::ownerId, and gme::PROJECTILE_PLAYER.
Here is the call graph for this function:
|
inlineoverridevirtual |
Reimplemented from ecs::ASystem.
Definition at line 26 of file Collision.hpp.
|
inlineoverridevirtual |
Reimplemented from ecs::ASystem.
Definition at line 40 of file Collision.hpp.
|
delete |
|
delete |
Deleted move assignment operator (non-movable)
|
delete |
|
delete |
|
delete |
|
delete |
Deleted copy assignment operator (non-copyable)
|
inlineprivate |
Definition at line 332 of file Collision.hpp.
References ensureEnemyDeathChannel(), ecs::Registry::getComponent(), ecs::INVALID_ENTITY, m_enemyDeathAudioEntities, and m_nextEnemyDeathChannel.
Referenced by update().
Here is the call graph for this function:
Here is the caller graph for this function:
|
inlineprivate |
Definition at line 363 of file Collision.hpp.
References ensurePlayerDeathAudio(), ecs::Registry::getComponent(), ecs::INVALID_ENTITY, and m_playerDeathAudioEntity.
Referenced by update().
Here is the call graph for this function:
Here is the caller graph for this function:
|
inlinestaticprivate |
Definition at line 255 of file Collision.hpp.
References ecs::Registry::hasComponent(), and ecs::Registry::removeComponent().
Referenced by update().
Here is the call graph for this function:
Here is the caller graph for this function:
|
inlinestaticprivate |
Definition at line 219 of file Collision.hpp.
References ecs::Registry::hasComponent(), and ecs::Registry::removeComponent().
Referenced by update().
Here is the call graph for this function:
Here is the caller graph for this function:
|
inline |
Reset the collision counter to zero.
Should be called periodically to prevent overflow
Definition at line 105 of file CollisionSystem.hpp.
References m_collisionCount.
|
inlineoverridevirtual |
Reimplemented from ecs::ASystem.
Definition at line 27 of file Collision.hpp.
|
inlineoverridevirtual |
Reimplemented from ecs::ASystem.
Definition at line 41 of file Collision.hpp.
|
overridevirtual |
Update the collision system (called each frame)
| registry | ECS registry containing all entities |
| deltaTime | Time elapsed since last frame (unused) |
Checks for collisions between all relevant entity pairs and applies appropriate responses (damage, destruction, etc.)
Implements ecs::ISystem.
|
inlineoverridevirtual |
Implements ecs::ISystem.
Definition at line 29 of file Collision.hpp.
References ecs::Registry::getAll(), ecs::Registry::getComponent(), ecs::Transform::y, and ecs::Velocity::y.
Here is the call graph for this function:
|
inlineoverridevirtual |
Implements ecs::ISystem.
Definition at line 43 of file Collision.hpp.
References checkCircularCollision(), createExplosion(), ecs::Registry::getAll(), ecs::Registry::getComponent(), m_wasPlayerPresent, playEnemyDeathSound(), playPlayerDeathSound(), removeEnemy(), removeProjectile(), ecs::Projectile::SUPERCHARGED, ecs::Transform::y, and ecs::Velocity::y.
Here is the call graph for this function:
|
private |
Counter for collisions detected this frame.
Definition at line 110 of file CollisionSystem.hpp.
Referenced by getCollisionCount(), handleEnemyProjectilePlayerCollision(), handlePlayerEnemyCollision(), handlePlayerProjectileEnemyCollision(), and resetCollisionCount().
|
private |
Definition at line 197 of file Collision.hpp.
Referenced by CollisionSystem(), ensureEnemyDeathChannel(), and playEnemyDeathSound().
|
private |
Entity manager reference for spawning/destruction.
Definition at line 109 of file CollisionSystem.hpp.
Referenced by applyDamageToEnemy(), applyDamageToPlayer(), handleEnemyProjectilePlayerCollision(), handlePlayerEnemyCollision(), and handlePlayerProjectileEnemyCollision().
|
private |
Definition at line 199 of file Collision.hpp.
Referenced by playEnemyDeathSound().
|
private |
Definition at line 198 of file Collision.hpp.
Referenced by ensurePlayerDeathAudio(), and playPlayerDeathSound().
|
private |
ECS registry reference.
Definition at line 108 of file CollisionSystem.hpp.
Referenced by applyDamageToEnemy(), applyDamageToPlayer(), getCollisionInfo(), handleEnemyProjectilePlayerCollision(), handlePlayerEnemyCollision(), and handlePlayerProjectileEnemyCollision().
|
private |
Definition at line 99 of file Collision.hpp.
|
private |
Definition at line 100 of file Collision.hpp.
|
private |
Definition at line 200 of file Collision.hpp.
Referenced by update().