From: Christoph Sommer Date: Fri, 28 Apr 2006 19:38:41 +0000 (+0000) Subject: The most senseless commit ever X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=d99e2443318865c0f379170b4ef2aa30f515b235;p=supertux.git The most senseless commit ever SVN-Revision: 3462 --- diff --git a/src/audio/sound_manager.cpp b/src/audio/sound_manager.cpp index d51a61779..0576fe6ba 100644 --- a/src/audio/sound_manager.cpp +++ b/src/audio/sound_manager.cpp @@ -157,6 +157,21 @@ SoundManager::play(const std::string& filename, const Vector& pos) } void +SoundManager::play_and_delete(SoundSource* source) +{ + if (!source) { + log_debug << "ignoring NULL SoundSource" << std::endl; + return; + } + try { + source->play(); + sources.push_back(source); + } catch(std::exception& e) { + log_warning << "Couldn't play SoundSource: " << e.what() << std::endl; + } +} + +void SoundManager::enable_sound(bool enable) { if(device == NULL) diff --git a/src/audio/sound_manager.hpp b/src/audio/sound_manager.hpp index 7d96af3ff..c87f89013 100644 --- a/src/audio/sound_manager.hpp +++ b/src/audio/sound_manager.hpp @@ -50,6 +50,7 @@ public: * Convenience function to simply play a sound at a given position. */ void play(const std::string& name, const Vector& pos = Vector(-1, -1)); + void play_and_delete(SoundSource* source); void set_listener_position(const Vector& position); void set_listener_velocity(const Vector& velocity); diff --git a/src/audio/sound_source.cpp b/src/audio/sound_source.cpp index 01243beeb..986489483 100644 --- a/src/audio/sound_source.cpp +++ b/src/audio/sound_source.cpp @@ -88,6 +88,12 @@ SoundSource::set_gain(float gain) } void +SoundSource::set_pitch(float pitch) +{ + alSourcef(source, AL_PITCH, pitch); +} + +void SoundSource::set_reference_distance(float distance) { alSourcef(source, AL_REFERENCE_DISTANCE, distance); diff --git a/src/audio/sound_source.hpp b/src/audio/sound_source.hpp index 91a091749..d878ef42a 100644 --- a/src/audio/sound_source.hpp +++ b/src/audio/sound_source.hpp @@ -38,6 +38,7 @@ public: void set_looping(bool looping); /// Set volume (0.0 is silent, 1.0 is normal) void set_gain(float gain); + void set_pitch(float pitch); void set_position(Vector position); void set_velocity(Vector position); void set_reference_distance(float distance); diff --git a/src/object/coin.cpp b/src/object/coin.cpp index b89f2006d..36b035abb 100644 --- a/src/object/coin.cpp +++ b/src/object/coin.cpp @@ -30,6 +30,10 @@ #include "statistics.hpp" #include "object_factory.hpp" #include "level.hpp" +#include "random_generator.hpp" +#include "audio/sound_source.hpp" +#include "audio/sound_manager.hpp" +#include "timer.hpp" Coin::Coin(const Vector& pos) { @@ -67,7 +71,74 @@ Coin::draw(DrawingContext& context) void Coin::collect() { - Sector::current()->player->get_status()->add_coins(1); + static Timer sound_timer; /**< time since last Coin was collected */ + static int pitch_one = 128; + static float last_pitch = 1; + float pitch = 1; + + int tile = static_cast(get_pos().y / 32); + + if (!sound_timer.started()) { + pitch_one = tile; + pitch = 1; + last_pitch = 1; + } + else if (sound_timer.get_timegone() < 0.02) { + pitch = last_pitch; + } + else + { + switch ((pitch_one - tile) % 7) { + case -6: + pitch = 1.0/2; + break; + case -5: + pitch = 5.0/8; + break; + case -4: + pitch = 4.0/6; + break; + case -3: + pitch = 3.0/4; + break; + case -2: + pitch = 5.0/6; + break; + case -1: + pitch = 9.0/10; + break; + case 0: + pitch = 1.0; + break; + case 1: + pitch = 9.0/8; + break; + case 2: + pitch = 5.0/4; + break; + case 3: + pitch = 4.0/3; + break; + case 4: + pitch = 3.0/2; + break; + case 5: + pitch = 5.0/3; + break; + case 6: + pitch = 9.0/5; + break; + } + last_pitch = pitch; + } + sound_timer.start(1); + + SoundSource* soundSource = sound_manager->create_sound_source("sounds/coin.wav"); + soundSource->set_position(get_pos()); + soundSource->set_pitch(pitch); + sound_manager->play_and_delete(soundSource); + + Sector::current()->player->get_status()->add_coins(1, false); Sector::current()->add_object(new BouncyCoin(get_pos())); Sector::current()->get_level()->stats.coins++; remove_me(); diff --git a/src/player_status.cpp b/src/player_status.cpp index c39c6faf4..e9dd84d86 100644 --- a/src/player_status.cpp +++ b/src/player_status.cpp @@ -64,13 +64,15 @@ void PlayerStatus::reset() } void -PlayerStatus::add_coins(int count) +PlayerStatus::add_coins(int count, bool play_sound) { coins = std::min(coins + count, MAX_COINS); - if(count >= 100) - sound_manager->play("sounds/lifeup.wav"); - else - sound_manager->play("sounds/coin.wav"); + if(play_sound) { + if(count >= 100) + sound_manager->play("sounds/lifeup.wav"); + else + sound_manager->play("sounds/coin.wav"); + } } void diff --git a/src/player_status.hpp b/src/player_status.hpp index 3df230be3..16cb9069b 100644 --- a/src/player_status.hpp +++ b/src/player_status.hpp @@ -46,7 +46,7 @@ public: PlayerStatus(); ~PlayerStatus(); void reset(); - void add_coins(int count); + void add_coins(int count, bool play_sound = true); void write(lisp::Writer& writer); void read(const lisp::Lisp& lisp);