}
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)
* 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);
}
void
+SoundSource::set_pitch(float pitch)
+{
+ alSourcef(source, AL_PITCH, pitch);
+}
+
+void
SoundSource::set_reference_distance(float distance)
{
alSourcef(source, AL_REFERENCE_DISTANCE, distance);
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);
#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)
{
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<int>(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();
}
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
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);