From e5565c03705a4d14d5e03b56000754c4c7b40f68 Mon Sep 17 00:00:00 2001 From: Steve Date: Fri, 26 May 2017 07:48:54 +0100 Subject: [PATCH] Positional battle sounds. Record player's last position when escaped, to allow for sounds to play correctly (edge case). --- src/system/sound.c | 16 ++++++++++++---- src/system/sound.h | 2 ++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/system/sound.c b/src/system/sound.c index ed2727f..504862a 100644 --- a/src/system/sound.c +++ b/src/system/sound.c @@ -78,11 +78,10 @@ void playSound(int id) void playBattleSound(int id, int x, int y) { - float distance; + float distance, bearing, vol; int channel; - float vol; - if (player->alive == ALIVE_ALIVE) + if (player->alive == ALIVE_ALIVE || player->alive == ALIVE_ESCAPED) { lastPlayerX = player->x; lastPlayerY = player->y; @@ -93,13 +92,22 @@ void playBattleSound(int id, int x, int y) if (distance <= MAX_BATTLE_SOUND_DISTANCE) { channel = Mix_PlayChannel(-1, sounds[id], 0); + if (channel != -1) { vol = 255; vol /= MAX_BATTLE_SOUND_DISTANCE; vol *= distance; - Mix_SetDistance(channel, vol); + if (distance >= MIN_BATTLE_SOUND_DISTANCE) + { + bearing = 360 - getAngle(x, y, lastPlayerX, lastPlayerY); + Mix_SetPosition(channel, (Sint16)bearing, (Uint8)vol); + } + else + { + Mix_SetDistance(channel, vol); + } } } } diff --git a/src/system/sound.h b/src/system/sound.h index f6b911f..9e6f6c6 100644 --- a/src/system/sound.h +++ b/src/system/sound.h @@ -23,8 +23,10 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "SDL2/SDL_mixer.h" #define MAX_BATTLE_SOUND_DISTANCE 1500 +#define MIN_BATTLE_SOUND_DISTANCE 100 extern int getDistance(int x1, int y1, int x2, int y2); +extern float getAngle(int x1, int y1, int x2, int y2); extern char *getFileLocation(char *filename); extern Entity *player;