# pragma region License
/*
License ( OLC - 3 )
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
Copyright 2024 Joshua Sigona < sigonasr2 @ gmail . com >
Redistribution and use in source and binary forms , with or without modification ,
are permitted provided that the following conditions are met :
1. Redistributions or derivations of source code must retain the above copyright
notice , this list of conditions and the following disclaimer .
2. Redistributions or derivative works in binary form must reproduce the above
copyright notice . This list of conditions and the following disclaimer must be
reproduced in the documentation and / or other materials provided with the distribution .
3. Neither the name of the copyright holder nor the names of its contributors may
be used to endorse or promote products derived from this software without specific
prior written permission .
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS " AS IS " AND ANY
EXPRESS OR IMPLIED WARRANTIES , INCLUDING , BUT NOT LIMITED TO , THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED . IN NO EVENT
SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT , INDIRECT ,
INCIDENTAL , SPECIAL , EXEMPLARY , OR CONSEQUENTIAL DAMAGES ( INCLUDING , BUT NOT LIMITED
TO , PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ; LOSS OF USE , DATA , OR PROFITS ; OR
BUSINESS INTERRUPTION ) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY , WHETHER IN
CONTRACT , STRICT LIABILITY , OR TORT ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE .
Portions of this software are copyright © 2024 The FreeType
Project ( www . freetype . org ) . Please see LICENSE_FT . txt for more information .
All rights reserved .
*/
# pragma endregion
# include "EnvironmentalAudio.h"
# include "Audio.h"
# include "AdventuresInLestoria.h"
INCLUDE_game
INCLUDE_DATA
float EnvironmentalAudio : : ACTIVATION_RANGE ;
std : : map < EnvironmentalAudioSoundName , EnvironmentalAudioData > EnvironmentalAudio : : SOUND_DATA ;
void EnvironmentalAudio : : Initialize ( ) {
ACTIVATION_RANGE = " Audio.Environmental Audio Activation Range " _F ;
for ( auto & [ key , size ] : DATA [ " Environmental Audio " ] ) {
SOUND_DATA [ key ] . file = DATA [ " Environmental Audio " ] [ key ] [ " File " ] . GetString ( ) ;
if ( ! std : : filesystem : : exists ( operator " " _SFX ( SOUND_DATA [ key ] . file . c_str ( ) , SOUND_DATA [ key ] . file . length ( ) ) ) ) ERR ( std : : format ( " WARNING! Could not load Environmental Audio {} due to missing file {} in {} " , key , SOUND_DATA [ key ] . file , " sfx_directory " _S ) ) ;
SOUND_DATA [ key ] . volume = DATA [ " Environmental Audio " ] [ key ] [ " Volume " ] . GetReal ( ) / 100.f ;
}
}
EnvironmentalAudio : : EnvironmentalAudio ( )
: audioName ( " " ) , pos ( { } ) { }
EnvironmentalAudio : : EnvironmentalAudio ( const std : : string_view audioName , const vf2d pos )
: audioName ( audioName ) , pos ( pos ) { }
void EnvironmentalAudio : : SetPos ( const vf2d & pos ) {
this - > pos = pos ;
}
void EnvironmentalAudio : : SetAudioName ( const std : : string_view audioName ) {
this - > audioName = audioName ;
}
void EnvironmentalAudio : : Activate ( ) {
if ( activated ) return ;
soundInstance = Audio : : LoadAndPlaySFX ( operator " " _SFX ( SOUND_DATA [ audioName ] . file . c_str ( ) , SOUND_DATA [ audioName ] . file . length ( ) ) , true ) ;
activated = true ;
}
void EnvironmentalAudio : : Deactivate ( ) {
if ( ! activated ) return ;
if ( soundInstance ! = std : : numeric_limits < size_t > : : max ( ) ) {
Audio : : Engine ( ) . UnloadSound ( soundInstance ) ;
}
soundInstance = std : : numeric_limits < size_t > : : max ( ) ;
activated = false ;
}
void EnvironmentalAudio : : UpdateEnvironmentalAudio ( ) {
for ( const EnvironmentalAudio & aud : game - > GetCurrentMap ( ) . GetEnvironmentalAudio ( ) ) {
EnvironmentalAudio & audio = const_cast < EnvironmentalAudio & > ( aud ) ;
audio . Update ( ) ;
}
}
void EnvironmentalAudio : : Update ( ) {
float distanceFromPlayer = geom2d : : line < float > ( game - > GetPlayer ( ) - > GetPos ( ) , pos ) . length ( ) ;
if ( distanceFromPlayer < ACTIVATION_RANGE ) Activate ( ) ;
if ( distanceFromPlayer > ACTIVATION_RANGE ) {
Deactivate ( ) ;
return ; //Do not need to continue processing if the sound is not even going to be heard.
}
float distRatio = 1 - distanceFromPlayer / ACTIVATION_RANGE ; //0-1 where 1 is full volume.
float xDistRatio = ( pos . x - game - > GetPlayer ( ) - > GetX ( ) ) / ACTIVATION_RANGE ; //0-1 where 1 is full volume.
Audio : : Engine ( ) . SetVolume ( soundInstance , distRatio * SOUND_DATA [ audioName ] . volume * Audio : : GetSFXVolume ( ) * Audio : : GetMuteMult ( ) ) ;
Audio : : Engine ( ) . SetPan ( soundInstance , xDistRatio ) ;
}