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

System managing weapon firing and cooldowns. More...

#include <WeaponSystem.hpp>

Inheritance diagram for ecs::WeaponSystem:
Inheritance graph
Collaboration diagram for ecs::WeaponSystem:
Collaboration graph

Public Types

using ProjectileCreatedCallback = std::function< void(uint32_t, uint32_t, float, float, float, float, float, int, bool)>
 Callback for projectile creation events Parameters: projectileId, ownerId, posX, posY, dirX, dirY, speed, damage, friendly.
 

Public Member Functions

 WeaponSystem ()=default
 Default constructor.
 
 WeaponSystem (ProjectileCreatedCallback callback)
 Constructor with callback for projectile creation events.
 
 ~WeaponSystem () override=default
 Default destructor.
 
void setProjectileCreatedCallback (ProjectileCreatedCallback callback)
 Set the callback for projectile creation events.
 
void update (Registry &registry, float deltaTime) override
 Manages weapon cooldowns for all entities.
 
std::uint32_t fireWeapon (Registry &registry, std::uint32_t ownerId, bool isFriendly)
 Fire a weapon from an entity, spawning a projectile.
 
std::uint32_t fireChargedShot (Registry &registry, std::uint32_t ownerId, float chargeLevel, bool isFriendly)
 Fire a charged shot if charge is sufficient, otherwise fire normal shot.
 
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

Velocity calculateProjectileVelocity (float baseSpeed, bool isFriendly)
 Calculate projectile initial velocity based on owner's velocity.
 
Transform calculateProjectileTransform (Registry &registry, std::uint32_t ownerId, bool isFriendly)
 Calculate projectile spawn position based on owner position.
 
std::uint32_t createProjectile (Registry &registry, std::uint32_t ownerId, const Transform &transform, const Velocity &velocity, float damage, bool isFriendly, bool isCharged)
 Create a single projectile with specified properties.
 
void fireMultipleShots (Registry &registry, std::uint32_t ownerId, float damage, float speed, bool isFriendly, bool isCharged, int shotCount)
 Fire multiple projectiles based on multishot buff.
 

Private Attributes

ProjectileCreatedCallback _projectileCreatedCallback
 

Detailed Description

System managing weapon firing and cooldowns.

Handles weapon cooldown timers, projectile spawning, and weapon mechanics. Requires Weapon and Transform components. Optionally uses Velocity for weapon direction.

Definition at line 29 of file WeaponSystem.hpp.

Member Typedef Documentation

◆ ProjectileCreatedCallback

using ecs::WeaponSystem::ProjectileCreatedCallback = std::function<void(uint32_t, uint32_t, float, float, float, float, float, int, bool)>

Callback for projectile creation events Parameters: projectileId, ownerId, posX, posY, dirX, dirY, speed, damage, friendly.

Definition at line 35 of file WeaponSystem.hpp.

Constructor & Destructor Documentation

◆ WeaponSystem() [1/2]

ecs::WeaponSystem::WeaponSystem ( )
default

Default constructor.

◆ WeaponSystem() [2/2]

ecs::WeaponSystem::WeaponSystem ( ProjectileCreatedCallback  callback)
inlineexplicit

Constructor with callback for projectile creation events.

Parameters
callbackFunction to call when a projectile is created

Definition at line 48 of file WeaponSystem.hpp.

◆ ~WeaponSystem()

ecs::WeaponSystem::~WeaponSystem ( )
overridedefault

Default destructor.

Member Function Documentation

◆ calculateProjectileTransform()

Transform ecs::WeaponSystem::calculateProjectileTransform ( Registry registry,
std::uint32_t  ownerId,
bool  isFriendly 
)
private

Calculate projectile spawn position based on owner position.

Parameters
registryReference to the ECS registry
ownerIdEntity ID of the weapon owner
isFriendlyWhether this is a friendly projectile (affects spawn offset)
Returns
Transform component for the projectile initial position

Definition at line 133 of file WeaponSystem.cpp.

References ecs::Registry::getComponent(), ecs::Transform::getPosition(), and ecs::Registry::hasComponent().

Referenced by fireChargedShot(), fireMultipleShots(), and fireWeapon().

Here is the call graph for this function:

◆ calculateProjectileVelocity()

Velocity ecs::WeaponSystem::calculateProjectileVelocity ( float  baseSpeed,
bool  isFriendly 
)
private

Calculate projectile initial velocity based on owner's velocity.

Parameters
baseSpeedBase projectile speed from weapon
isFriendlyWhether this is a friendly projectile (affects direction)
Returns
Velocity component for the projectile

Definition at line 127 of file WeaponSystem.cpp.

Referenced by fireChargedShot(), and fireWeapon().

◆ createProjectile()

std::uint32_t ecs::WeaponSystem::createProjectile ( Registry registry,
std::uint32_t  ownerId,
const Transform transform,
const Velocity velocity,
float  damage,
bool  isFriendly,
bool  isCharged 
)
private

Create a single projectile with specified properties.

Parameters
registryReference to the ECS registry
ownerIdEntity ID of the weapon owner
transformProjectile spawn position
velocityProjectile velocity
damageProjectile damage
isFriendlyWhether this is a friendly projectile
isChargedWhether this is a charged shot (affects visuals)
Returns
Address ID of the spawned projectile entity

Definition at line 210 of file WeaponSystem.cpp.

References _projectileCreatedCallback, AnimDB::createPlayerBulletAnimations(), ecs::Registry::getComponent(), ecs::Velocity::getDirection(), ecs::Weapon::getFireRate(), ecs::Transform::getPosition(), ecs::Velocity::getSpeed(), ecs::Registry::hasComponent(), ecs::Registry::newEntity(), and ecs::Registry::setComponent().

Referenced by fireChargedShot(), fireMultipleShots(), and fireWeapon().

Here is the call graph for this function:

◆ fireChargedShot()

std::uint32_t ecs::WeaponSystem::fireChargedShot ( Registry registry,
std::uint32_t  ownerId,
float  chargeLevel,
bool  isFriendly 
)

Fire a charged shot if charge is sufficient, otherwise fire normal shot.

If the provided charge level meets the minimum threshold (0.5f or 50%), fires a charged projectile with increased damage and speed. Otherwise, fires a normal projectile.

Parameters
registryReference to the ECS registry
ownerIdEntity ID of the weapon owner
chargeLevelCurrent charge level (0.0f to 1.0f)
isFriendlyWhether this is a friendly projectile
Returns
Address ID of the spawned projectile entity

Definition at line 150 of file WeaponSystem.cpp.

References calculateProjectileTransform(), calculateProjectileVelocity(), createProjectile(), ecs::DoubleShot, fireMultipleShots(), fireWeapon(), ecs::Registry::getComponent(), ecs::Weapon::getDamage(), ecs::Buff::hasBuff(), ecs::Registry::hasComponent(), ecs::MultiShot, and ecs::TripleShot.

Referenced by update().

Here is the call graph for this function:

◆ fireMultipleShots()

void ecs::WeaponSystem::fireMultipleShots ( Registry registry,
std::uint32_t  ownerId,
float  damage,
float  speed,
bool  isFriendly,
bool  isCharged,
int  shotCount 
)
private

Fire multiple projectiles based on multishot buff.

Parameters
registryReference to the ECS registry
ownerIdEntity ID of the weapon owner
damageProjectile damage
speedProjectile speed
isFriendlyWhether projectiles are friendly
isChargedWhether this is a charged shot
shotCountNumber of projectiles to fire

Definition at line 258 of file WeaponSystem.cpp.

References calculateProjectileTransform(), createProjectile(), ecs::Transform::getPosition(), ecs::Registry::hasComponent(), and ecs::Transform::setPosition().

Referenced by fireChargedShot(), and fireWeapon().

Here is the call graph for this function:

◆ fireWeapon()

std::uint32_t ecs::WeaponSystem::fireWeapon ( Registry registry,
std::uint32_t  ownerId,
bool  isFriendly 
)

Fire a weapon from an entity, spawning a projectile.

Creates a new projectile entity with appropriate components. Resets the weapon cooldown timer based on fire rate.

Parameters
registryReference to the ECS registry
ownerIdEntity ID of the weapon owner
isFriendlyWhether this is a friendly projectile
Returns
Address ID of the spawned projectile entity

Definition at line 85 of file WeaponSystem.cpp.

References calculateProjectileTransform(), calculateProjectileVelocity(), createProjectile(), ecs::DoubleShot, fireMultipleShots(), ecs::Registry::getComponent(), ecs::Weapon::getDamage(), ecs::Buff::hasBuff(), ecs::Registry::hasComponent(), ecs::MultiShot, and ecs::TripleShot.

Referenced by fireChargedShot(), and update().

Here is the call graph for this function:

◆ getComponentMask()

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

Gets the component mask for this system.

Returns
ComponentMask requiring Weapon and Transform components

Implements ecs::ISystem.

Definition at line 206 of file WeaponSystem.cpp.

Referenced by update().

◆ setProjectileCreatedCallback()

void ecs::WeaponSystem::setProjectileCreatedCallback ( ProjectileCreatedCallback  callback)
inline

Set the callback for projectile creation events.

Parameters
callbackFunction to call when a projectile is created

Definition at line 60 of file WeaponSystem.hpp.

References _projectileCreatedCallback.

◆ update()

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

Manages weapon cooldowns for all entities.

Updates weapon cooldowns for all entities with weapons.

Decrements weapon cooldown timers each frame. When cooldown reaches 0, the weapon is ready to fire. Actual projectile creation is handled by input or AI systems.

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

Implements ecs::ISystem.

Definition at line 24 of file WeaponSystem.cpp.

References fireChargedShot(), fireWeapon(), ecs::Registry::getComponent(), getComponentMask(), ecs::Registry::getEntitiesWithMask(), ecs::Registry::hasComponent(), and ecs::Weapon::setCooldown().

Here is the call graph for this function:

Member Data Documentation

◆ _projectileCreatedCallback

ProjectileCreatedCallback ecs::WeaponSystem::_projectileCreatedCallback
private

Definition at line 162 of file WeaponSystem.hpp.

Referenced by createProjectile(), and setProjectileCreatedCallback().


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