Skip to content

Commit

Permalink
Merge pull request #1569 from Semphriss/fix-coin-sound-bug
Browse files Browse the repository at this point in the history
Fixed several problems with the sound system
  • Loading branch information
serano01 authored Nov 5, 2020
2 parents 3882410 + f55f85b commit 916f8bc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
22 changes: 13 additions & 9 deletions src/audio/openal_sound_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,11 @@ OpenALSoundSource::OpenALSoundSource() :
m_volume(1.0f)
{
alGenSources(1, &m_source);
try
{
SoundManager::check_al_error("Couldn't create audio source: ");
}
catch(std::exception& e)
{
log_warning << e.what() << std::endl;
}

// Don't catch anything here: force the caller to catch the error, so that
// the caller won't handle an object in an invalid state thinking it's clean
SoundManager::check_al_error("Couldn't create audio source: ");

set_reference_distance(128);
}

Expand Down Expand Up @@ -86,7 +83,14 @@ void
OpenALSoundSource::pause()
{
alSourcePause(m_source);
SoundManager::check_al_error("Couldn't pause audio source: ");
try
{
SoundManager::check_al_error("Couldn't pause audio source: ");
}
catch(const std::exception& e)
{
log_warning << e.what() << std::endl;
}
}

void
Expand Down
21 changes: 15 additions & 6 deletions src/object/coin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ Coin::collision(GameObject& other, const CollisionHit& )
/* The following defines a coin subject to gravity */
HeavyCoin::HeavyCoin(const Vector& pos, const Vector& init_velocity) :
Coin(pos),
m_physic()
m_physic(),
m_last_hit()
{
m_physic.enable_gravity(true);
SoundManager::current()->preload("sounds/coin2.ogg");
Expand All @@ -203,7 +204,8 @@ HeavyCoin::HeavyCoin(const Vector& pos, const Vector& init_velocity) :

HeavyCoin::HeavyCoin(const ReaderMapping& reader) :
Coin(reader),
m_physic()
m_physic(),
m_last_hit()
{
m_physic.enable_gravity(true);
SoundManager::current()->preload("sounds/coin2.ogg");
Expand All @@ -222,9 +224,10 @@ HeavyCoin::collision_solid(const CollisionHit& hit)
{
float clink_threshold = 100.0f; // sets the minimum speed needed to result in collision noise
//TODO: colliding HeavyCoins should have their own unique sound

if (hit.bottom) {
if (m_physic.get_velocity_y() > clink_threshold)
SoundManager::current()->play("sounds/coin2.ogg");
if (m_physic.get_velocity_y() > clink_threshold && !m_last_hit.bottom)
SoundManager::current()->play("sounds/coin2.ogg");
if (m_physic.get_velocity_y() > 200) {// lets some coins bounce
m_physic.set_velocity_y(-99);
} else {
Expand All @@ -233,15 +236,21 @@ HeavyCoin::collision_solid(const CollisionHit& hit)
}
}
if (hit.right || hit.left) {
if (m_physic.get_velocity_x() > clink_threshold || m_physic.get_velocity_x() < clink_threshold)
if ((m_physic.get_velocity_x() > clink_threshold ||
m_physic.get_velocity_x()< -clink_threshold) &&
hit.right != m_last_hit.right && hit.left != m_last_hit.left)
SoundManager::current()->play("sounds/coin2.ogg");
m_physic.set_velocity_x(-m_physic.get_velocity_x());
}
if (hit.top) {
if (m_physic.get_velocity_y() < clink_threshold)
if (m_physic.get_velocity_y() < -clink_threshold && !m_last_hit.top)
SoundManager::current()->play("sounds/coin2.ogg");
m_physic.set_velocity_y(-m_physic.get_velocity_y());
}

// Only make a sound if the coin wasn't hittin anything last frame (A coin
// stuck in solid matter would flood the sound manager - see #1555 on GitHub)
m_last_hit = hit;
}

void
Expand Down
1 change: 1 addition & 0 deletions src/object/coin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class HeavyCoin final : public Coin

private:
Physic m_physic;
CollisionHit m_last_hit;

private:
HeavyCoin(const HeavyCoin&) = delete;
Expand Down

0 comments on commit 916f8bc

Please sign in to comment.