@ -58,8 +58,8 @@ void RepeatingSoundEffect::StopAllSounds(){
playingSoundEffects . clear ( ) ;
playingSoundEffects . clear ( ) ;
}
}
SoundEffect : : SoundEffect ( const std : : string_view filename , const float & vol , const float & minPitch , const float & maxPitch , const bool combatSound )
SoundEffect : : SoundEffect ( const std : : string_view filename , const float & vol , const float & minPitch , const float & maxPitch , const bool combatSound , const bool treatAsBGM )
: filename ( filename ) , vol ( vol ) , minPitch ( minPitch ) , maxPitch ( maxPitch ) , combatSound ( combatSound ) {
: filename ( filename ) , vol ( vol ) , minPitch ( minPitch ) , maxPitch ( maxPitch ) , combatSound ( combatSound ) , treatAsBGM ( treatAsBGM ) {
if ( vol < 0.f | | vol > 1.f ) ERR ( std : : format ( " WARNING! Volume must be between 0.0f ~ 1.0f! Provided value {} " , vol ) ) ;
if ( vol < 0.f | | vol > 1.f ) ERR ( std : : format ( " WARNING! Volume must be between 0.0f ~ 1.0f! Provided value {} " , vol ) ) ;
}
}
@ -67,16 +67,20 @@ void SoundEffect::Initialize(){
for ( auto & [ key , size ] : DATA [ " Events " ] [ " SFX " ] ) {
for ( auto & [ key , size ] : DATA [ " Events " ] [ " SFX " ] ) {
int counter = 0 ;
int counter = 0 ;
bool combatSound = false ;
bool combatSound = false ;
bool treatAsBGM { false } ;
if ( DATA [ " Events " ] [ " SFX " ] [ key ] . HasProperty ( " CombatSound " ) ) {
if ( DATA [ " Events " ] [ " SFX " ] [ key ] . HasProperty ( " CombatSound " ) ) {
combatSound = DATA [ " Events " ] [ " SFX " ] [ key ] [ " CombatSound " ] . GetBool ( ) ;
combatSound = DATA [ " Events " ] [ " SFX " ] [ key ] [ " CombatSound " ] . GetBool ( ) ;
}
}
if ( DATA [ " Events " ] [ " SFX " ] [ key ] . HasProperty ( " Treat as BGM " ) ) {
treatAsBGM = DATA [ " Events " ] [ " SFX " ] [ key ] [ " Treat as BGM " ] . GetBool ( ) ;
}
while ( DATA [ " Events " ] [ " SFX " ] [ key ] . HasProperty ( std : : format ( " File[{}] " , counter ) ) ) {
while ( DATA [ " Events " ] [ " SFX " ] [ key ] . HasProperty ( std : : format ( " File[{}] " , counter ) ) ) {
utils : : datafile & data = DATA [ " Events " ] [ " SFX " ] [ key ] [ std : : format ( " File[{}] " , counter ) ] ;
utils : : datafile & data = DATA [ " Events " ] [ " SFX " ] [ key ] [ std : : format ( " File[{}] " , counter ) ] ;
float minPitch = 0.9f ;
float minPitch = 0.9f ;
float maxPitch = 1.1f ;
float maxPitch = 1.1f ;
if ( data . GetValueCount ( ) > = 3 ) { minPitch = data . GetInt ( 2 ) / 100.f ; }
if ( data . GetValueCount ( ) > = 3 ) { minPitch = data . GetInt ( 2 ) / 100.f ; }
if ( data . GetValueCount ( ) > = 4 ) { maxPitch = data . GetInt ( 3 ) / 100.f ; }
if ( data . GetValueCount ( ) > = 4 ) { maxPitch = data . GetInt ( 3 ) / 100.f ; }
SOUND_EFFECTS . insert ( { key , SoundEffect { data . GetString ( 0 ) , data . GetInt ( 1 ) / 100.f , minPitch , maxPitch , combatSound } } ) ;
SOUND_EFFECTS . insert ( { key , SoundEffect { data . GetString ( 0 ) , data . GetInt ( 1 ) / 100.f , minPitch , maxPitch , combatSound , treatAsBGM } } ) ;
counter + + ;
counter + + ;
}
}
auto itr = SOUND_EFFECTS . equal_range ( key ) ;
auto itr = SOUND_EFFECTS . equal_range ( key ) ;
@ -96,7 +100,8 @@ void SoundEffect::PlaySFX(const std::string&eventName,const vf2d&pos){
float pitch = util : : random ( pitchDiff ) + sfx . minPitch ;
float pitch = util : : random ( pitchDiff ) + sfx . minPitch ;
if ( pos = = CENTERED ) {
if ( pos = = CENTERED ) {
Audio : : Engine ( ) . Play ( operator " " _SFX ( sfx . filename . c_str ( ) , sfx . filename . length ( ) ) , Audio : : GetCalculatedSFXVolume ( sfx . vol * Audio : : GetSFXVolume ( ) ) , 0.0f , pitch ) ;
float vol { Audio : : GetCalculatedSFXVolume ( sfx ) } ;
Audio : : Engine ( ) . Play ( operator " " _SFX ( sfx . filename . c_str ( ) , sfx . filename . length ( ) ) , vol , 0.0f , pitch ) ;
} else {
} else {
const float soundActivationRange = " Audio.Environmental Audio Activation Range " _F ;
const float soundActivationRange = " Audio.Environmental Audio Activation Range " _F ;
@ -105,7 +110,7 @@ void SoundEffect::PlaySFX(const std::string&eventName,const vf2d&pos){
float distRatio = 1 - distanceFromPlayer / soundActivationRange ; //0-1 where 1 is full volume.
float distRatio = 1 - distanceFromPlayer / soundActivationRange ; //0-1 where 1 is full volume.
float xDistRatio = ( pos . x - game - > GetPlayer ( ) - > GetX ( ) ) / soundActivationRange ; //0-1 where 1 is full volume.
float xDistRatio = ( pos . x - game - > GetPlayer ( ) - > GetX ( ) ) / soundActivationRange ; //0-1 where 1 is full volume.
float vol = distRatio * sfx . vol * Audio : : GetSFXVolume ( ) * Audio : : GetMuteMult ( ) ;
float vol = distRatio * Audio : : GetCalculated SFXVolume ( sfx ) ;
float pan = xDistRatio ;
float pan = xDistRatio ;
Audio : : Engine ( ) . Play ( operator " " _SFX ( sfx . filename . c_str ( ) , sfx . filename . length ( ) ) , vol , pan , pitch ) ;
Audio : : Engine ( ) . Play ( operator " " _SFX ( sfx . filename . c_str ( ) , sfx . filename . length ( ) ) , vol , pan , pitch ) ;
}
}
@ -143,3 +148,10 @@ SoundEffect&SoundEffect::GetRandomSFXFromFile(const std::string&eventName){
return ( * it ) . second ;
return ( * it ) . second ;
}
}
const float & SoundEffect : : GetVolume ( ) const {
return vol ;
}
const bool & SoundEffect : : TreatAsBPM ( ) const {
return treatAsBGM ;
}