Replaced Ref and RefCounter with std::shared_ptr<>
[supertux.git] / src / object / coin.cpp
index a1eea59..a298454 100644 (file)
 #include "supertux/sector.hpp"
 
 Coin::Coin(const Vector& pos)
-  : MovingSprite(pos, "images/objects/coin/coin.sprite", LAYER_TILES, COLGROUP_MOVING),
+  : MovingSprite(pos, "images/objects/coin/coin.sprite", LAYER_OBJECTS - 1, COLGROUP_TOUCHABLE),
     path(),
     walker(),
     offset(),
     from_tilemap(false),
     physic()
 {
-  sound_manager->preload("sounds/coin.wav");
+  SoundManager::current()->preload("sounds/coin.wav");
 }
 
 Coin::Coin(const Vector& pos, TileMap* tilemap)
-  : MovingSprite(pos, "images/objects/coin/coin.sprite", LAYER_TILES, COLGROUP_TOUCHABLE),
-    path(boost::shared_ptr<Path>(tilemap->get_path())),
-    walker(boost::shared_ptr<PathWalker>(tilemap->get_walker())),
+  : MovingSprite(pos, "images/objects/coin/coin.sprite", LAYER_OBJECTS - 1, COLGROUP_TOUCHABLE),
+    path(std::shared_ptr<Path>(tilemap->get_path())),
+    walker(std::shared_ptr<PathWalker>(tilemap->get_walker())),
     offset(),
     from_tilemap(true),
     physic()
@@ -49,11 +49,11 @@ Coin::Coin(const Vector& pos, TileMap* tilemap)
     offset = pos - v;
   }
 
-  sound_manager->preload("sounds/coin.wav");
+  SoundManager::current()->preload("sounds/coin.wav");
 }
 
 Coin::Coin(const Reader& reader)
-  : MovingSprite(reader, "images/objects/coin/coin.sprite", LAYER_TILES, COLGROUP_TOUCHABLE),
+  : MovingSprite(reader, "images/objects/coin/coin.sprite", LAYER_OBJECTS - 1, COLGROUP_TOUCHABLE),
     path(),
     walker(),
     offset(),
@@ -69,7 +69,7 @@ Coin::Coin(const Reader& reader)
     set_pos(v);
   }
 
-  sound_manager->preload("sounds/coin.wav");
+  SoundManager::current()->preload("sounds/coin.wav");
 }
 
 void
@@ -149,14 +149,14 @@ Coin::collect()
     }
     sound_timer.start(1);
 
-    SoundSource* soundSource = sound_manager->create_sound_source("sounds/coin.wav");
+    SoundSource* soundSource = SoundManager::current()->create_sound_source("sounds/coin.wav");
     soundSource->set_position(get_pos());
     soundSource->set_pitch(pitch);
     soundSource->play();
-    sound_manager->manage_source(soundSource);
+    SoundManager::current()->manage_source(soundSource);
   */
   Sector::current()->player->get_status()->add_coins(1);
-  Sector::current()->add_object(new BouncyCoin(get_pos()));
+  Sector::current()->add_object(std::make_shared<BouncyCoin>(get_pos()));
   Sector::current()->get_level()->stats.coins++;
   remove_me();
 }
@@ -178,10 +178,20 @@ HeavyCoin::HeavyCoin(const Vector& pos, const Vector& init_velocity)
   physic()
 {
   physic.enable_gravity(true);
-  sound_manager->preload("sounds/coin.wav");
+  SoundManager::current()->preload("sounds/coin2.ogg");
+  set_group(COLGROUP_MOVING);
   physic.set_velocity(init_velocity);
 }
 
+HeavyCoin::HeavyCoin(const Reader& reader)
+  : Coin(reader),
+  physic()
+{
+  physic.enable_gravity(true);
+  SoundManager::current()->preload("sounds/coin2.ogg");
+  set_group(COLGROUP_MOVING);
+}
+
 void
 HeavyCoin::update(float elapsed_time)
 {
@@ -192,12 +202,29 @@ HeavyCoin::update(float elapsed_time)
 void
 HeavyCoin::collision_solid(const CollisionHit& hit)
 {
+  int clink_threshold = 100; // sets the minimum speed needed to result in collision noise
+  //TODO: colliding HeavyCoins should have their own unique sound
+
   if(hit.bottom) {
-    physic.set_velocity_y(0);
+    if(physic.get_velocity_y() > clink_threshold)
+      SoundManager::current()->play("sounds/coin2.ogg");
+    if(physic.get_velocity_y() > 200) {// lets some coins bounce
+      physic.set_velocity_y(-99);
+    }else{
+      physic.set_velocity_y(0);
+      physic.set_velocity_x(0);
+    }
   }
   if(hit.right || hit.left) {
+    if(physic.get_velocity_x() > clink_threshold || physic.get_velocity_x() < clink_threshold)
+      SoundManager::current()->play("sounds/coin2.ogg");
     physic.set_velocity_x(-physic.get_velocity_x());
   }
+  if(hit.top) {
+    if(physic.get_velocity_y() < clink_threshold)
+      SoundManager::current()->play("sounds/coin2.ogg");
+    physic.set_velocity_y(-physic.get_velocity_y());
+  }
 }
 
 /* EOF */