summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/being.cpp17
-rw-r--r--src/sound.cpp27
-rw-r--r--src/sound.h4
3 files changed, 39 insertions, 9 deletions
diff --git a/src/being.cpp b/src/being.cpp
index 570d8b22..4d682ab8 100644
--- a/src/being.cpp
+++ b/src/being.cpp
@@ -362,7 +362,18 @@ void Being::takeDamage(Being *attacker, int amount, AttackType type)
if (amount > 0)
{
- sound.playSfx(mInfo->getSound(SOUND_EVENT_HURT));
+ if (mInfo)
+ {
+ if (attacker)
+ {
+ sound.playSfx(mInfo->getSound(SOUND_EVENT_HURT),
+ attacker->getTileX(), attacker->getTileY());
+ }
+ else
+ {
+ sound.playSfx(mInfo->getSound(SOUND_EVENT_HURT));
+ }
+ }
if (getType() == MONSTER)
{
@@ -394,7 +405,7 @@ void Being::handleAttack(Being *victim, int damage, AttackType type)
}
sound.playSfx(mInfo->getSound((damage > 0) ?
- SOUND_EVENT_HIT : SOUND_EVENT_MISS));
+ SOUND_EVENT_HIT : SOUND_EVENT_MISS), mX, mY);
}
void Being::setName(const std::string &name)
@@ -619,7 +630,7 @@ void Being::setAction(Action action, int attackType)
break;
case DEAD:
currentAction = SpriteAction::DEAD;
- sound.playSfx(mInfo->getSound(SOUND_EVENT_DIE));
+ sound.playSfx(mInfo->getSound(SOUND_EVENT_DIE), mX, mY);
break;
case STAND:
currentAction = SpriteAction::STAND;
diff --git a/src/sound.cpp b/src/sound.cpp
index 6fe9668d..a8090f84 100644
--- a/src/sound.cpp
+++ b/src/sound.cpp
@@ -21,14 +21,14 @@
#include <SDL.h>
+#include "configuration.h"
+#include "localplayer.h"
#include "log.h"
#include "sound.h"
#include "resources/resourcemanager.h"
#include "resources/soundeffect.h"
-#include "configuration.h"
-
Sound::Sound():
mInstalled(false),
mSfxVolume(100),
@@ -228,17 +228,34 @@ void Sound::fadeOutMusic(int ms)
}
}
-void Sound::playSfx(const std::string &path)
+void Sound::playSfx(const std::string &path, int x, int y)
{
if (!mInstalled || path.empty())
return;
+ std::string tmpPath;
+ if (!path.find("sfx/"))
+ tmpPath = path;
+ else
+ tmpPath = paths.getValue("sfx", "sfx/") + path;
ResourceManager *resman = ResourceManager::getInstance();
- SoundEffect *sample = resman->getSoundEffect(path);
+ SoundEffect *sample = resman->getSoundEffect(tmpPath);
if (sample)
{
logger->log("Sound::playSfx() Playing: %s", path.c_str());
- sample->play(0, 120);
+ int vol = 120;
+ if (player_node && x > 0 && y > 0)
+ {
+ int dx = player_node->getTileX() - x;
+ int dy = player_node->getTileY() - y;
+ if (dx < 0)
+ dx = -dx;
+ if (dy < 0)
+ dy = -dy;
+ int dist = dx > dy ? dx : dy;
+ vol -= dist * 8;
+ }
+ sample->play(0, vol);
}
}
diff --git a/src/sound.h b/src/sound.h
index bf5dc3f6..bfb3837b 100644
--- a/src/sound.h
+++ b/src/sound.h
@@ -86,8 +86,10 @@ class Sound
* Plays an item.
*
* @param path The resource path to the sound file.
+ * @param x The vertical distance of the sound in tiles.
+ * @param y The horizontal distance of the sound in tiles.
*/
- void playSfx(const std::string &path);
+ void playSfx(const std::string &path, int x = 0, int y = 0);
private:
/** Logs various info about sound device. */