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

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
 
CollisionSystemoperator= (const CollisionSystem &)=delete
 
 CollisionSystem (CollisionSystem &&)=delete
 
CollisionSystemoperator= (const CollisionSystem &&)=delete
 
bool isEnable () override
 
void setEnable (bool enable) override
 
void update (ecs::Registry &registry, float dt) override
 
 CollisionSystem (const std::shared_ptr< eng::IRenderer > &renderer, bool &showDebug)
 
 ~CollisionSystem () override=default
 
 CollisionSystem (const CollisionSystem &)=delete
 
CollisionSystemoperator= (const CollisionSystem &)=delete
 
 CollisionSystem (CollisionSystem &&)=delete
 
CollisionSystemoperator= (CollisionSystem &&)=delete
 
bool isEnable () override
 
void setEnable (const bool enable) override
 
void update (ecs::Registry &registry, float dt) override
 
 CollisionSystem (ecs::Registry &registry, EntityManager &entityManager)
 Constructor.
 
 ~CollisionSystem () override=default
 Destructor.
 
 CollisionSystem (const CollisionSystem &)=delete
 Deleted copy constructor (non-copyable)
 
CollisionSystemoperator= (const CollisionSystem &)=delete
 Deleted copy assignment operator (non-copyable)
 
 CollisionSystem (CollisionSystem &&)=delete
 Deleted move constructor (non-movable)
 
CollisionSystemoperator= (CollisionSystem &&)=delete
 Deleted move assignment operator (non-movable)
 
void update (ecs::Registry &registry, 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 &registry, const std::size_t channelIndex)
 
void playEnemyDeathSound (ecs::Registry &registry)
 
void ensurePlayerDeathAudio (ecs::Registry &registry)
 
void playPlayerDeathSound (ecs::Registry &registry)
 
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 &registry, ecs::Entity entity)
 
static void removeEnemy (ecs::Registry &registry, ecs::Entity entity)
 
static void createExplosion (ecs::Registry &registry, 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::Registrym_registry
 ECS registry reference.
 
EntityManagerm_entityManager
 Entity manager reference for spawning/destruction.
 
size_t m_collisionCount = 0
 Counter for collisions detected this frame.
 

Detailed Description

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:

  • Detects collisions between player projectiles and enemies
  • Detects collisions between enemy projectiles and players
  • Detects collisions between players and enemies (ramming)
  • Detects collisions between players and powerups
  • Applies damage to entities based on collision type
  • Destroys entities when appropriate
  • Tracks collision statistics for debugging

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.

Constructor & Destructor Documentation

◆ CollisionSystem() [1/9]

gme::CollisionSystem::CollisionSystem ( const std::shared_ptr< eng::IRenderer > & renderer,
bool & showDebug )
inlineexplicit

Definition at line 15 of file Collision.hpp.

◆ ~CollisionSystem() [1/3]

gme::CollisionSystem::~CollisionSystem ( )
overridedefault

◆ CollisionSystem() [2/9]

gme::CollisionSystem::CollisionSystem ( const CollisionSystem & )
delete

◆ CollisionSystem() [3/9]

gme::CollisionSystem::CollisionSystem ( CollisionSystem && )
delete

◆ CollisionSystem() [4/9]

gme::CollisionSystem::CollisionSystem ( const std::shared_ptr< eng::IRenderer > & renderer,
bool & showDebug )
inlineexplicit

Definition at line 28 of file Collision.hpp.

References ecs::INVALID_ENTITY, and m_enemyDeathAudioEntities.

◆ ~CollisionSystem() [2/3]

gme::CollisionSystem::~CollisionSystem ( )
overridedefault

◆ CollisionSystem() [5/9]

gme::CollisionSystem::CollisionSystem ( const CollisionSystem & )
delete

◆ CollisionSystem() [6/9]

gme::CollisionSystem::CollisionSystem ( CollisionSystem && )
delete

◆ CollisionSystem() [7/9]

gme::CollisionSystem::CollisionSystem ( ecs::Registry & registry,
EntityManager & entityManager )
inlineexplicit

Constructor.

Parameters
registryECS registry containing all entities and components
entityManagerEntity manager for spawning/destroying entities

Initializes the collision system with references to required managers

Definition at line 66 of file CollisionSystem.hpp.

◆ ~CollisionSystem() [3/3]

gme::CollisionSystem::~CollisionSystem ( )
overridedefault

Destructor.

◆ CollisionSystem() [8/9]

gme::CollisionSystem::CollisionSystem ( const CollisionSystem & )
delete

Deleted copy constructor (non-copyable)

◆ CollisionSystem() [9/9]

gme::CollisionSystem::CollisionSystem ( CollisionSystem && )
delete

Deleted move constructor (non-movable)

Member Function Documentation

◆ applyDamageToEnemy()

void gme::CollisionSystem::applyDamageToEnemy ( std::uint32_t enemyId,
float damage,
std::uint32_t attackerPlayerId = 0 )
private

Apply damage to an enemy entity.

Parameters
enemyIdNetwork ID of the enemy entity
damageAmount of damage to apply
attackerPlayerIdSession 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:

◆ applyDamageToPlayer()

void gme::CollisionSystem::applyDamageToPlayer ( std::uint32_t playerId,
float damage )
private

Apply damage to a player entity.

Parameters
playerIdSession ID of the player
damageAmount 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:

◆ checkCircleCollision()

bool gme::CollisionSystem::checkCircleCollision ( float x1,
float y1,
float r1,
float x2,
float y2,
float r2,
float * overlapDist = nullptr ) const
inlineprivate

Check for circular collision between two entities.

Inline implementation of circular collision detection.

Parameters
x1X position of first entity center
y1Y position of first entity center
r1Radius of first entity
x2X position of second entity center
y2Y position of second entity center
r2Radius of second entity
overlapDistOptional output parameter for overlap distance
Returns
True if entities are colliding

Uses distance formula: sqrt((x2-x1)² + (y2-y1)²) < (r1 + r2)

Parameters
x1X position of first circle center
y1Y position of first circle center
r1Radius of first circle
x2X position of second circle center
y2Y position of second circle center
r2Radius of second circle
overlapDistOptional pointer to store overlap distance (radiusSum - actualDistance)
Returns
True if circles are overlapping (distance < radiusSum)

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:

◆ checkCircularCollision()

static bool gme::CollisionSystem::checkCircularCollision ( const ecs::Transform & transform1,
const ecs::Hitbox & hitbox1,
const ecs::Transform & transform2,
const ecs::Hitbox & hitbox2 )
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:

◆ createExplosion()

◆ ensureEnemyDeathChannel()

void gme::CollisionSystem::ensureEnemyDeathChannel ( ecs::Registry & registry,
const std::size_t channelIndex )
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:

◆ ensurePlayerDeathAudio()

void gme::CollisionSystem::ensurePlayerDeathAudio ( ecs::Registry & registry)
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:

◆ getCollisionCount()

size_t gme::CollisionSystem::getCollisionCount ( ) const
inline

Get the number of collisions detected this frame.

Returns
Total collision count

Useful for debugging and performance monitoring

Definition at line 99 of file CollisionSystem.hpp.

References m_collisionCount.

◆ getCollisionInfo()

bool gme::CollisionSystem::getCollisionInfo ( ecs::Entity entity,
float & x,
float & y,
float & radius ) const
inlineprivate

Extract collision information from an entity.

Inline implementation of collision info extraction.

Parameters
entityEntity to get collision info from
xOutput: X position of collision center
yOutput: Y position of collision center
radiusOutput: Collision radius
Returns
True if entity has valid Transform and Hitbox components

Calculates collision center from transform position and hitbox offset

Parameters
entityEntity to extract collision info from
xOutput parameter for collision center X position
yOutput parameter for collision center Y position
radiusOutput parameter for collision radius
Returns
True if entity has both Transform and Hitbox components, false otherwise

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:

◆ handleEnemyProjectilePlayerCollision()

void gme::CollisionSystem::handleEnemyProjectilePlayerCollision ( )
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:

◆ handlePlayerEnemyCollision()

void gme::CollisionSystem::handlePlayerEnemyCollision ( )
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:

◆ handlePlayerPowerupCollision()

void gme::CollisionSystem::handlePlayerPowerupCollision ( )
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.

◆ handlePlayerProjectileEnemyCollision()

void gme::CollisionSystem::handlePlayerProjectileEnemyCollision ( )
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:

◆ isEnable() [1/2]

bool gme::CollisionSystem::isEnable ( )
inlineoverridevirtual

Reimplemented from ecs::ASystem.

Definition at line 26 of file Collision.hpp.

◆ isEnable() [2/2]

bool gme::CollisionSystem::isEnable ( )
inlineoverridevirtual

Reimplemented from ecs::ASystem.

Definition at line 40 of file Collision.hpp.

◆ operator=() [1/6]

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

◆ operator=() [2/6]

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

Deleted move assignment operator (non-movable)

◆ operator=() [3/6]

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

◆ operator=() [4/6]

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

◆ operator=() [5/6]

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

◆ operator=() [6/6]

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

Deleted copy assignment operator (non-copyable)

◆ playEnemyDeathSound()

void gme::CollisionSystem::playEnemyDeathSound ( ecs::Registry & registry)
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:

◆ playPlayerDeathSound()

void gme::CollisionSystem::playPlayerDeathSound ( ecs::Registry & registry)
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:

◆ removeEnemy()

static void gme::CollisionSystem::removeEnemy ( ecs::Registry & registry,
ecs::Entity entity )
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:

◆ removeProjectile()

static void gme::CollisionSystem::removeProjectile ( ecs::Registry & registry,
ecs::Entity entity )
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:

◆ resetCollisionCount()

void gme::CollisionSystem::resetCollisionCount ( )
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.

◆ setEnable() [1/2]

void gme::CollisionSystem::setEnable ( bool enable)
inlineoverridevirtual

Reimplemented from ecs::ASystem.

Definition at line 27 of file Collision.hpp.

◆ setEnable() [2/2]

void gme::CollisionSystem::setEnable ( const bool enable)
inlineoverridevirtual

Reimplemented from ecs::ASystem.

Definition at line 41 of file Collision.hpp.

◆ update() [1/3]

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

Update the collision system (called each frame)

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

Checks for collisions between all relevant entity pairs and applies appropriate responses (damage, destruction, etc.)

Implements ecs::ISystem.

◆ update() [2/3]

void gme::CollisionSystem::update ( ecs::Registry & registry,
float dt )
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:

◆ update() [3/3]

void gme::CollisionSystem::update ( ecs::Registry & registry,
float dt )
inlineoverridevirtual

Member Data Documentation

◆ m_collisionCount

size_t gme::CollisionSystem::m_collisionCount = 0
private

◆ m_enemyDeathAudioEntities

std::array<ecs::Entity, 4> gme::CollisionSystem::m_enemyDeathAudioEntities {}
private

Definition at line 197 of file Collision.hpp.

Referenced by CollisionSystem(), ensureEnemyDeathChannel(), and playEnemyDeathSound().

◆ m_entityManager

EntityManager& gme::CollisionSystem::m_entityManager
private

◆ m_nextEnemyDeathChannel

std::size_t gme::CollisionSystem::m_nextEnemyDeathChannel = 0
private

Definition at line 199 of file Collision.hpp.

Referenced by playEnemyDeathSound().

◆ m_playerDeathAudioEntity

ecs::Entity gme::CollisionSystem::m_playerDeathAudioEntity = ecs::INVALID_ENTITY
private

Definition at line 198 of file Collision.hpp.

Referenced by ensurePlayerDeathAudio(), and playPlayerDeathSound().

◆ m_registry

◆ m_renderer

const std::shared_ptr< eng::IRenderer > & gme::CollisionSystem::m_renderer
private

Definition at line 99 of file Collision.hpp.

◆ m_showDebug

bool & gme::CollisionSystem::m_showDebug
private

Definition at line 100 of file Collision.hpp.

◆ m_wasPlayerPresent

bool gme::CollisionSystem::m_wasPlayerPresent = false
private

Definition at line 200 of file Collision.hpp.

Referenced by update().


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