R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
ecs::CollisionSystem Class Reference

System handling collision detection between entities. More...

#include <CollisionSystem.hpp>

Inheritance diagram for ecs::CollisionSystem:
Inheritance graph
Collaboration diagram for ecs::CollisionSystem:
Collaboration graph

Public Member Functions

 CollisionSystem ()=default
 Default constructor.
 
 ~CollisionSystem () override=default
 Default destructor.
 
void update (Registry &registry, float deltaTime) override
 Detects and handles collisions between entities.
 
ComponentMask getComponentMask () const override
 Gets the component mask for this system.
 
- Public Member Functions inherited from ecs::ISystem
virtual ~ISystem ()=default
 Virtual destructor.
 

Private Member Functions

bool checkAABB (const Transform::Vector2 &pos1, const Collider::Vector2 &size1, const Collider::Vector2 &offset1, const Transform::Vector2 &pos2, const Collider::Vector2 &size2, const Collider::Vector2 &offset2) const
 Checks AABB collision between two entities.
 
void handlePickup (Address playerAddr, Address collectibleAddr, Registry &registry, std::vector< Address > &entitiesToDestroy)
 Handle collision between player and collectible.
 
bool canCollide (std::uint32_t layer1, std::uint32_t mask1, std::uint32_t layer2, std::uint32_t mask2) const
 Checks if two entities can collide based on layers.
 
void resolveWallCollision (Address playerAddr, Address wallAddr, Transform &playerTransform, Collider &playerCollider, Transform &wallTransform, Collider &wallCollider, Registry &registry)
 Resolves collision between player and wall by instantly killing the player.
 
void handleModuleEnemyCollision (Address moduleAddr, Address enemyAddr, Registry &registry)
 Handle collision between orbital module and enemy.
 
void handleModuleProjectileCollision (Address moduleAddr, Address projectileAddr, Registry &registry, std::vector< Address > &entitiesToDestroy)
 Handle collision between orbital module and projectile.
 
void handleProjectileCollision (Registry &registry, std::uint32_t entity1, std::uint32_t entity2, std::vector< Address > &entitiesToDestroy)
 Handle projectile collision with other entities.
 

Detailed Description

System handling collision detection between entities.

Detects AABB collisions and manages layer-based filtering. Requires Transform and Collider components.

Definition at line 26 of file CollisionSystem.hpp.

Constructor & Destructor Documentation

◆ CollisionSystem()

ecs::CollisionSystem::CollisionSystem ( )
default

Default constructor.

◆ ~CollisionSystem()

ecs::CollisionSystem::~CollisionSystem ( )
overridedefault

Default destructor.

Member Function Documentation

◆ canCollide()

bool ecs::CollisionSystem::canCollide ( std::uint32_t  layer1,
std::uint32_t  mask1,
std::uint32_t  layer2,
std::uint32_t  mask2 
) const
private

Checks if two entities can collide based on layers.

Checks if two entities can collide based on layer masks.

Uses bitwise operations to determine if collision layers are compatible. Entity A can collide with Entity B if:

  • A's mask includes B's layer AND
  • B's mask includes A's layer
Parameters
layer1Collision layer of first entity
mask1Collision mask of first entity
layer2Collision layer of second entity
mask2Collision mask of second entity
Returns
true if entities should collide, false otherwise

Definition at line 320 of file CollisionSystem.cpp.

Referenced by update().

◆ checkAABB()

bool ecs::CollisionSystem::checkAABB ( const Transform::Vector2 pos1,
const Collider::Vector2 size1,
const Collider::Vector2 offset1,
const Transform::Vector2 pos2,
const Collider::Vector2 size2,
const Collider::Vector2 offset2 
) const
private

Checks AABB collision between two entities.

Performs AABB (Axis-Aligned Bounding Box) collision test.

Tests if two axis-aligned bounding boxes overlap.

Parameters
pos1Position of first entity
size1Size of first collider
offset1Offset of first collider from position
pos2Position of second entity
size2Size of second collider
offset2Offset of second collider from position
Returns
true if boxes overlap, false otherwise

Definition at line 276 of file CollisionSystem.cpp.

References ecs::Collider::Vector2::x, ecs::Transform::Vector2::x, ecs::Collider::Vector2::y, and ecs::Transform::Vector2::y.

Referenced by update().

◆ getComponentMask()

ComponentMask ecs::CollisionSystem::getComponentMask ( ) const
overridevirtual

Gets the component mask for this system.

Returns
ComponentMask requiring Transform and Collider components

Implements ecs::ISystem.

Definition at line 408 of file CollisionSystem.cpp.

Referenced by update().

◆ handleModuleEnemyCollision()

void ecs::CollisionSystem::handleModuleEnemyCollision ( Address  moduleAddr,
Address  enemyAddr,
Registry registry 
)
private

Handle collision between orbital module and enemy.

Applies damage from the module to the enemy on contact.

Parameters
moduleAddrOrbital module entity address
enemyAddrEnemy entity address
registryReference to the ECS registry

Definition at line 230 of file CollisionSystem.cpp.

References ecs::Registry::getComponent(), ecs::Health::getCurrentHealth(), ecs::Registry::hasComponent(), LOG_DEBUG, and ecs::Health::setCurrentHealth().

Referenced by update().

Here is the call graph for this function:

◆ handleModuleProjectileCollision()

void ecs::CollisionSystem::handleModuleProjectileCollision ( Address  moduleAddr,
Address  projectileAddr,
Registry registry,
std::vector< Address > &  entitiesToDestroy 
)
private

Handle collision between orbital module and projectile.

Blocks enemy projectiles by destroying them on contact with the module.

Parameters
moduleAddrOrbital module entity address
projectileAddrProjectile entity address
registryReference to the ECS registry
entitiesToDestroyVector to collect entities that should be destroyed

Definition at line 253 of file CollisionSystem.cpp.

References ecs::Registry::getComponent(), ecs::Registry::hasComponent(), ecs::Projectile::isFriendly(), and LOG_DEBUG.

Referenced by update().

Here is the call graph for this function:

◆ handlePickup()

void ecs::CollisionSystem::handlePickup ( Address  playerAddr,
Address  collectibleAddr,
Registry registry,
std::vector< Address > &  entitiesToDestroy 
)
private

◆ handleProjectileCollision()

void ecs::CollisionSystem::handleProjectileCollision ( Registry registry,
std::uint32_t  entity1,
std::uint32_t  entity2,
std::vector< Address > &  entitiesToDestroy 
)
private

Handle projectile collision with other entities.

Handles projectile-entity collisions and applies damage.

Checks if a collision involves a projectile and a damageable entity (enemy/player). Applies damage based on projectile properties and marks projectile for destruction. Respects friendly fire rules: friendly projectiles damage enemies, enemy projectiles damage players.

Parameters
registryReference to the ECS registry
entity1First entity
entity2Second entity
entitiesToDestroyVector to collect entities (projectiles) that should be destroyed

This method implements clean damage logic:

  • Friendly projectiles (player bullets) damage enemies only
  • Enemy projectiles damage players only
  • Projectiles are destroyed on impact (unless piercing)
  • Damage is applied through Health component
  • Invincibility frames are respected

Definition at line 335 of file CollisionSystem.cpp.

References ecs::Registry::getComponent(), ecs::Health::getCurrentHealth(), ecs::Projectile::getDamage(), ecs::Health::getMaxHealth(), ecs::Registry::hasComponent(), ecs::Projectile::isFriendly(), LOG_INFO, and ecs::Health::takeDamage().

Referenced by update().

Here is the call graph for this function:

◆ resolveWallCollision()

void ecs::CollisionSystem::resolveWallCollision ( Address  playerAddr,
Address  wallAddr,
Transform playerTransform,
Collider playerCollider,
Transform wallTransform,
Collider wallCollider,
Registry registry 
)
private

Resolves collision between player and wall by instantly killing the player.

Resolves player-wall collision by instantly killing the player.

Walls are instant death obstacles - deals massive damage to kill player on contact.

Parameters
playerAddrPlayer entity address
wallAddrWall entity address
playerTransformPlayer's transform component
playerColliderPlayer's collider component
wallTransformWall's transform component
wallColliderWall's collider component
registryReference to the ECS registry

Definition at line 297 of file CollisionSystem.cpp.

References ecs::Registry::getComponent(), ecs::Registry::hasComponent(), LOG_INFO, and ecs::Health::setCurrentHealth().

Referenced by update().

Here is the call graph for this function:

◆ update()

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

Detects and handles collisions between entities.

Performs collision detection between all collidable entities.

Performs N² collision detection between all entities with colliders. Uses AABB (Axis-Aligned Bounding Box) collision detection and layer-based filtering to determine valid collisions.

Parameters
registryReference to the ECS registry
deltaTimeTime elapsed since last frame (in seconds)

Implements ecs::ISystem.

Definition at line 20 of file CollisionSystem.cpp.

References canCollide(), checkAABB(), ecs::Registry::destroyEntity(), ecs::Registry::getComponent(), getComponentMask(), ecs::Registry::getEntitiesWithMask(), handleModuleEnemyCollision(), handleModuleProjectileCollision(), handlePickup(), handleProjectileCollision(), ecs::Registry::hasComponent(), LOG_DEBUG, and resolveWallCollision().

Here is the call graph for this function:

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