The most senseless commit ever
authorChristoph Sommer <mail@christoph-sommer.de>
Fri, 28 Apr 2006 19:38:41 +0000 (19:38 +0000)
committerChristoph Sommer <mail@christoph-sommer.de>
Fri, 28 Apr 2006 19:38:41 +0000 (19:38 +0000)
SVN-Revision: 3462

src/audio/sound_manager.cpp
src/audio/sound_manager.hpp
src/audio/sound_source.cpp
src/audio/sound_source.hpp
src/object/coin.cpp
src/player_status.cpp
src/player_status.hpp

index d51a617..0576fe6 100644 (file)
@@ -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)
index 7d96af3..c87f890 100644 (file)
@@ -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);
index 01243be..9864894 100644 (file)
@@ -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);
index 91a0917..d878ef4 100644 (file)
@@ -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);
index b89f200..36b035a 100644 (file)
 #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<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();
index c39c6fa..e9dd84d 100644 (file)
@@ -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
index 3df230b..16cb906 100644 (file)
@@ -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);