diff --git a/main.c b/main.c index b2a2e14..7539bc8 100755 --- a/main.c +++ b/main.c @@ -1358,11 +1358,24 @@ void SFG_explodeBarrel(uint8_t itemIndex, RCL_Unit x, RCL_Unit y, RCL_Unit z) SFG_createExplosion(x,y,z); } +uint8_t SFG_distantSoundVolume(RCL_Unit x, RCL_Unit y, RCL_Unit z) +{ + RCL_Unit distance = SFG_taxicabDistance(x,y,z, + SFG_player.camera.position.x, + SFG_player.camera.position.y, + SFG_player.camera.height); + + if (distance >= SFG_SFX_MAX_DISTANCE) + return 0; + + return 255 - (distance * 255) / SFG_SFX_MAX_DISTANCE; +} + void SFG_createExplosion(RCL_Unit x, RCL_Unit y, RCL_Unit z) { SFG_ProjectileRecord explosion; - SFG_playSound(2,255); + SFG_playSound(2,SFG_distantSoundVolume(x,y,z)); explosion.type = SFG_PROJECTILE_EXPLOSION; diff --git a/platform_sdl.h b/platform_sdl.h index 9d75706..d55d938 100644 --- a/platform_sdl.h +++ b/platform_sdl.h @@ -192,8 +192,10 @@ void audioFillCallback(void *userdata, uint8_t *s, int l) void SFG_playSound(uint8_t soundIndex, uint8_t volume) { + uint8_t volumeStep = volume / 16; + for (int i = 0; i < SFG_SFX_SAMPLE_COUNT; ++i) - audioBuff[i] = SFG_GET_SFX_SAMPLE(soundIndex,i) * 16; + audioBuff[i] = SFG_GET_SFX_SAMPLE(soundIndex,i) * volumeStep; audioPos = 0; diff --git a/settings.h b/settings.h index 4ea9b6a..1d9b857 100644 --- a/settings.h +++ b/settings.h @@ -175,6 +175,12 @@ */ #define SFG_ELEMENT_DISTANCES_CHECKED_PER_FRAME 8 +/** + Maximum distance at which sound effects (SFX) will be played. The SFX volume + will gradually drop towards this distance. +*/ +#define SFG_SFX_MAX_DISTANCE (1024 * 20) + /** Says the intensity of background image blur. 0 means no blur, improves performance and lowers memory usage.