Positional battle sounds. Record player's last position when escaped, to allow for sounds to play correctly (edge case).

This commit is contained in:
Steve 2017-05-26 07:48:54 +01:00
parent d703da7d23
commit e5565c0370
2 changed files with 14 additions and 4 deletions

View File

@ -78,11 +78,10 @@ void playSound(int id)
void playBattleSound(int id, int x, int y) void playBattleSound(int id, int x, int y)
{ {
float distance; float distance, bearing, vol;
int channel; int channel;
float vol;
if (player->alive == ALIVE_ALIVE) if (player->alive == ALIVE_ALIVE || player->alive == ALIVE_ESCAPED)
{ {
lastPlayerX = player->x; lastPlayerX = player->x;
lastPlayerY = player->y; lastPlayerY = player->y;
@ -93,13 +92,22 @@ void playBattleSound(int id, int x, int y)
if (distance <= MAX_BATTLE_SOUND_DISTANCE) if (distance <= MAX_BATTLE_SOUND_DISTANCE)
{ {
channel = Mix_PlayChannel(-1, sounds[id], 0); channel = Mix_PlayChannel(-1, sounds[id], 0);
if (channel != -1) if (channel != -1)
{ {
vol = 255; vol = 255;
vol /= MAX_BATTLE_SOUND_DISTANCE; vol /= MAX_BATTLE_SOUND_DISTANCE;
vol *= 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);
}
} }
} }
} }

View File

@ -23,8 +23,10 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "SDL2/SDL_mixer.h" #include "SDL2/SDL_mixer.h"
#define MAX_BATTLE_SOUND_DISTANCE 1500 #define MAX_BATTLE_SOUND_DISTANCE 1500
#define MIN_BATTLE_SOUND_DISTANCE 100
extern int getDistance(int x1, int y1, int x2, int y2); 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 char *getFileLocation(char *filename);
extern Entity *player; extern Entity *player;