From 499b0b4bd905da3fdd27a493917549cf249ef76d Mon Sep 17 00:00:00 2001 From: Ben Boudaoud Date: Wed, 17 Mar 2021 19:19:59 -0400 Subject: [PATCH 1/3] Add loopFireSound parameter to weapon --- docs/weaponConfigReadme.md | 3 +++ source/Weapon.cpp | 9 ++++++--- source/Weapon.h | 4 +++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/docs/weaponConfigReadme.md b/docs/weaponConfigReadme.md index f346a12f..6bfe926b 100644 --- a/docs/weaponConfigReadme.md +++ b/docs/weaponConfigReadme.md @@ -46,6 +46,7 @@ Controls specific to the sound/view model for the weapon are provided below: |-----------------------|-----------|-----------------------------------------------------------------------------------------------------------| |`fireSound` |file | The filename/location of the audio clip to use for the weapon firing (for no sound use an empty string) | |`fireSoundVol` |ratio | The volume to play the `fireSound` clip with | +|`loopFireSound` |`bool` | Whether or not to loop the `fireSound` clip for non-continuous weapons (weapon must be auto to apply) | |`renderModel` |`bool` | Whether or not a weapon model is rendered in the first-person view | |`modelSpec` |`ArticulatedModel::Specification` | Any-based specification for the weapon being used | |`kickAngleDegrees` |`float` | The angle (in degrees) the weapon model should kick after fire | @@ -54,6 +55,7 @@ Controls specific to the sound/view model for the weapon are provided below: ``` "fireSound" : "sound/fpsci_fire_100ms.wav" // This comes w/ FPSci "fireSoundVol" : 1.0; // Play the fire sound at 1/2 volume + "loopFireSound": false; // Don't loop fire sound by default "renderModel" : false; // Don't render a weapon model "modelSpec" : []; // No default model spec provided (see the example config below for more info) "kickAngleDegrees": 0; // Weapons don't kick by default @@ -143,6 +145,7 @@ The config below provides an example for each of the fields above (along with th "fireSound": "sound/fpsci_fire_100ms.wav", // The sound to fire "fireSoundVol": 1.0f, +"loopFireAudio": false, // Don't loop fire audio by default "renderModel": true, // Default is false, "modelSpec": ArticulatedModel::Specification{ // Default model filename = "model/sniper/sniper.obj"; diff --git a/source/Weapon.cpp b/source/Weapon.cpp index fab34da6..427887c9 100644 --- a/source/Weapon.cpp +++ b/source/Weapon.cpp @@ -15,6 +15,7 @@ WeaponConfig::WeaponConfig(const Any& any) { reader.getIfPresent("damagePerSecond", damagePerSecond); reader.getIfPresent("fireSound", fireSound); reader.getIfPresent("fireSoundVol", fireSoundVol); + reader.getIfPresent("loopFireSound", loopFireSound); reader.getIfPresent("hitScan", hitScan); reader.getIfPresent("renderModel", renderModel); @@ -72,6 +73,7 @@ Any WeaponConfig::toAny(const bool forceAll) const { if (forceAll || def.damagePerSecond != damagePerSecond) a["damagePerSecond"] = damagePerSecond; if (forceAll || def.fireSound != fireSound) a["fireSound"] = fireSound; if (forceAll || def.fireSoundVol != fireSoundVol) a["fireSoundVol"] = fireSoundVol; + if (forceAll || def.loopFireSound != loopFireSound) a["loopFireSound"] = loopFireSound; if (forceAll || def.hitScan != hitScan) a["hitScan"] = hitScan; if (forceAll || def.renderModel != renderModel) a["renderModel"] = renderModel; @@ -419,13 +421,14 @@ shared_ptr Weapon::fire( } void Weapon::playSound(bool shotFired, bool shootButtonUp) { - if (m_config->isContinuous()) { + if (m_config->loopAudio()) { if (notNull(m_fireAudio)) { if (shootButtonUp) { - m_fireAudio->setPaused(true); + m_fireAudio->setPaused(true); // Pause looped audio on mouse up } else if (shotFired && m_fireAudio->paused()) { - m_fireAudio->setPaused(false); + if (m_config->isContinuous()) m_fireAudio->setPaused(false); // Handle laser case (can just un-pause) + else m_fireAudio = m_fireSound->play(m_config->fireSoundVol); // For loopFireSound case start a new sound here } } else if (shotFired && notNull(m_fireSound)) { diff --git a/source/Weapon.h b/source/Weapon.h index 76f06065..f6c4139c 100644 --- a/source/Weapon.h +++ b/source/Weapon.h @@ -76,6 +76,7 @@ class WeaponConfig { float damagePerSecond = 2.0f; ///< Damage per second delivered (compute shot damage as damagePerSecond/firePeriod) String fireSound = "sound/fpsci_fire_100ms.wav"; ///< Sound to play on fire float fireSoundVol = 1.0f; ///< Volume for fire sound + bool loopFireSound = false; ///< Loop weapon audio (override for auto fire weapons) bool renderModel = false; ///< Render a model for the weapon? bool hitScan = true; ///< Is the weapon a projectile or hitscan @@ -114,6 +115,7 @@ class WeaponConfig { /** Returns true if firePeriod == 0 and autoFire == true */ inline bool isContinuous() const { return firePeriod == 0 && autoFire; } + inline bool loopAudio() const { return isContinuous() || (loopFireSound && autoFire); } WeaponConfig() {} WeaponConfig(const Any& any); @@ -214,7 +216,7 @@ class Weapon : Entity { void loadSounds() { // Check for play mode specific parameters if (notNull(m_fireAudio)) { m_fireAudio->stop(); } - if(!m_config->fireSound.empty()) m_fireSound = Sound::create(System::findDataFile(m_config->fireSound), m_config->isContinuous()); + if(!m_config->fireSound.empty()) m_fireSound = Sound::create(System::findDataFile(m_config->fireSound), m_config->loopAudio()); else { m_fireSound = nullptr; } } // Plays the sound based on the weapon fire mode From 12bf2de36e15668d34741f3242c9ddd4e41d96e0 Mon Sep 17 00:00:00 2001 From: Josef Spjut Date: Thu, 18 Mar 2021 11:13:16 -0400 Subject: [PATCH 2/3] rename parameter to `fireSoundLoop` --- docs/weaponConfigReadme.md | 6 +++--- source/Weapon.cpp | 6 +++--- source/Weapon.h | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/weaponConfigReadme.md b/docs/weaponConfigReadme.md index 6bfe926b..366ef34c 100644 --- a/docs/weaponConfigReadme.md +++ b/docs/weaponConfigReadme.md @@ -46,7 +46,7 @@ Controls specific to the sound/view model for the weapon are provided below: |-----------------------|-----------|-----------------------------------------------------------------------------------------------------------| |`fireSound` |file | The filename/location of the audio clip to use for the weapon firing (for no sound use an empty string) | |`fireSoundVol` |ratio | The volume to play the `fireSound` clip with | -|`loopFireSound` |`bool` | Whether or not to loop the `fireSound` clip for non-continuous weapons (weapon must be auto to apply) | +|`fireSoundLoop` |`bool` | Whether or not to loop the `fireSound` clip for non-continuous weapons (weapon must be auto to apply) | |`renderModel` |`bool` | Whether or not a weapon model is rendered in the first-person view | |`modelSpec` |`ArticulatedModel::Specification` | Any-based specification for the weapon being used | |`kickAngleDegrees` |`float` | The angle (in degrees) the weapon model should kick after fire | @@ -55,7 +55,7 @@ Controls specific to the sound/view model for the weapon are provided below: ``` "fireSound" : "sound/fpsci_fire_100ms.wav" // This comes w/ FPSci "fireSoundVol" : 1.0; // Play the fire sound at 1/2 volume - "loopFireSound": false; // Don't loop fire sound by default + "fireSoundLoop": false; // Don't loop fire sound by default "renderModel" : false; // Don't render a weapon model "modelSpec" : []; // No default model spec provided (see the example config below for more info) "kickAngleDegrees": 0; // Weapons don't kick by default @@ -145,7 +145,7 @@ The config below provides an example for each of the fields above (along with th "fireSound": "sound/fpsci_fire_100ms.wav", // The sound to fire "fireSoundVol": 1.0f, -"loopFireAudio": false, // Don't loop fire audio by default +"fireSoundLoop": false, // Don't loop fire audio by default "renderModel": true, // Default is false, "modelSpec": ArticulatedModel::Specification{ // Default model filename = "model/sniper/sniper.obj"; diff --git a/source/Weapon.cpp b/source/Weapon.cpp index 427887c9..049ada0a 100644 --- a/source/Weapon.cpp +++ b/source/Weapon.cpp @@ -15,7 +15,7 @@ WeaponConfig::WeaponConfig(const Any& any) { reader.getIfPresent("damagePerSecond", damagePerSecond); reader.getIfPresent("fireSound", fireSound); reader.getIfPresent("fireSoundVol", fireSoundVol); - reader.getIfPresent("loopFireSound", loopFireSound); + reader.getIfPresent("fireSoundLoop", fireSoundLoop); reader.getIfPresent("hitScan", hitScan); reader.getIfPresent("renderModel", renderModel); @@ -73,7 +73,7 @@ Any WeaponConfig::toAny(const bool forceAll) const { if (forceAll || def.damagePerSecond != damagePerSecond) a["damagePerSecond"] = damagePerSecond; if (forceAll || def.fireSound != fireSound) a["fireSound"] = fireSound; if (forceAll || def.fireSoundVol != fireSoundVol) a["fireSoundVol"] = fireSoundVol; - if (forceAll || def.loopFireSound != loopFireSound) a["loopFireSound"] = loopFireSound; + if (forceAll || def.fireSoundLoop != fireSoundLoop) a["fireSoundLoop"] = fireSoundLoop; if (forceAll || def.hitScan != hitScan) a["hitScan"] = hitScan; if (forceAll || def.renderModel != renderModel) a["renderModel"] = renderModel; @@ -428,7 +428,7 @@ void Weapon::playSound(bool shotFired, bool shootButtonUp) { } else if (shotFired && m_fireAudio->paused()) { if (m_config->isContinuous()) m_fireAudio->setPaused(false); // Handle laser case (can just un-pause) - else m_fireAudio = m_fireSound->play(m_config->fireSoundVol); // For loopFireSound case start a new sound here + else m_fireAudio = m_fireSound->play(m_config->fireSoundVol); // For fireSoundLoop case start a new sound here } } else if (shotFired && notNull(m_fireSound)) { diff --git a/source/Weapon.h b/source/Weapon.h index f6c4139c..6e41e661 100644 --- a/source/Weapon.h +++ b/source/Weapon.h @@ -76,7 +76,7 @@ class WeaponConfig { float damagePerSecond = 2.0f; ///< Damage per second delivered (compute shot damage as damagePerSecond/firePeriod) String fireSound = "sound/fpsci_fire_100ms.wav"; ///< Sound to play on fire float fireSoundVol = 1.0f; ///< Volume for fire sound - bool loopFireSound = false; ///< Loop weapon audio (override for auto fire weapons) + bool fireSoundLoop = false; ///< Loop weapon audio (override for auto fire weapons) bool renderModel = false; ///< Render a model for the weapon? bool hitScan = true; ///< Is the weapon a projectile or hitscan @@ -115,7 +115,7 @@ class WeaponConfig { /** Returns true if firePeriod == 0 and autoFire == true */ inline bool isContinuous() const { return firePeriod == 0 && autoFire; } - inline bool loopAudio() const { return isContinuous() || (loopFireSound && autoFire); } + inline bool loopAudio() const { return isContinuous() || (fireSoundLoop && autoFire); } WeaponConfig() {} WeaponConfig(const Any& any); From c620c7f230f0fd9bc1dc792f0c15f9d703df0b1b Mon Sep 17 00:00:00 2001 From: Ben Boudaoud Date: Thu, 18 Mar 2021 11:50:30 -0400 Subject: [PATCH 3/3] Restructure/simplify Weapon::playSound() --- source/Weapon.cpp | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/source/Weapon.cpp b/source/Weapon.cpp index 427887c9..406a8879 100644 --- a/source/Weapon.cpp +++ b/source/Weapon.cpp @@ -421,22 +421,17 @@ shared_ptr Weapon::fire( } void Weapon::playSound(bool shotFired, bool shootButtonUp) { - if (m_config->loopAudio()) { - if (notNull(m_fireAudio)) { - if (shootButtonUp) { - m_fireAudio->setPaused(true); // Pause looped audio on mouse up - } - else if (shotFired && m_fireAudio->paused()) { - if (m_config->isContinuous()) m_fireAudio->setPaused(false); // Handle laser case (can just un-pause) - else m_fireAudio = m_fireSound->play(m_config->fireSoundVol); // For loopFireSound case start a new sound here - } + if (m_config->loopAudio()){ // Continuous weapon/looped audio + if (notNull(m_fireAudio) && shootButtonUp) { // Sound is playing and mouse is up + m_fireAudio->stop(); // Stop looped audio on mouse up + m_fireAudio = nullptr; } - else if (shotFired && notNull(m_fireSound)) { - m_fireAudio = m_fireSound->play(m_config->fireSoundVol); + else if (shotFired && isNull(m_fireAudio)) { // Shots fired and sound isn't playing + m_fireAudio = m_fireSound->play(m_config->fireSoundVol); // Start a new sound } } - else if (shotFired && notNull(m_fireSound)) { - m_fireSound->play(m_config->fireSoundVol); + else if (shotFired && notNull(m_fireSound)) { // Discrete weapon (no looped audio) + m_fireAudio = m_fireSound->play(m_config->fireSoundVol); // Start a new sound } }