Skip to content

Commit

Permalink
Merge pull request #296 from NVlabs/LoopWeaponAudio
Browse files Browse the repository at this point in the history
Add loopFireSound parameter to weapon
  • Loading branch information
jspjutNV authored Mar 18, 2021
2 parents 86f9431 + 0918e86 commit d75716d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
3 changes: 3 additions & 0 deletions docs/weaponConfigReadme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
|`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 |
Expand All @@ -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
"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
Expand Down Expand Up @@ -155,6 +157,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,
"fireSoundLoop": false, // Don't loop fire audio by default
"renderModel": true, // Default is false,
"modelSpec": ArticulatedModel::Specification{ // Default model
filename = "model/sniper/sniper.obj";
Expand Down
22 changes: 10 additions & 12 deletions source/Weapon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ WeaponConfig::WeaponConfig(const Any& any) {
reader.getIfPresent("damagePerSecond", damagePerSecond);
reader.getIfPresent("fireSound", fireSound);
reader.getIfPresent("fireSoundVol", fireSoundVol);
reader.getIfPresent("fireSoundLoop", fireSoundLoop);
reader.getIfPresent("hitScan", hitScan);

reader.getIfPresent("renderModel", renderModel);
Expand Down Expand Up @@ -74,6 +75,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.fireSoundLoop != fireSoundLoop) a["fireSoundLoop"] = fireSoundLoop;
if (forceAll || def.hitScan != hitScan) a["hitScan"] = hitScan;

if (forceAll || def.renderModel != renderModel) a["renderModel"] = renderModel;
Expand Down Expand Up @@ -441,21 +443,17 @@ shared_ptr<TargetEntity> Weapon::fire(
}

void Weapon::playSound(bool shotFired, bool shootButtonUp) {
if (m_config->isContinuous()) {
if (notNull(m_fireAudio)) {
if (shootButtonUp) {
m_fireAudio->setPaused(true);
}
else if (shotFired && m_fireAudio->paused()) {
m_fireAudio->setPaused(false);
}
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
}
}

Expand Down
4 changes: 3 additions & 1 deletion source/Weapon.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 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

Expand Down Expand Up @@ -116,6 +117,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() || (fireSoundLoop && autoFire); }

WeaponConfig() {}
WeaponConfig(const Any& any);
Expand Down Expand Up @@ -217,7 +219,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
Expand Down

0 comments on commit d75716d

Please sign in to comment.