fix cr/lfs and remove trailing whitespaces...
[supertux.git] / src / badguy / willowisp.cpp
index a375543..6e069e8 100644 (file)
@@ -1,5 +1,5 @@
 //  $Id$
-// 
+//
 //  SuperTux - "Will-O-Wisp" Badguy
 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
 //
@@ -12,7 +12,7 @@
 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 //  GNU General Public License for more details.
-// 
+//
 //  You should have received a copy of the GNU General Public License
 //  along with this program; if not, write to the Free Software
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 static const float FLYSPEED = 64; /**< speed in px per second */
 static const float TRACK_RANGE = 384; /**< at what distance to start tracking the player */
 static const float VANISH_RANGE = 512; /**< at what distance to stop tracking and vanish */
+static const std::string SOUNDFILE = "sounds/willowisp.wav";
 
 WillOWisp::WillOWisp(const lisp::Lisp& reader)
-  : BadGuy(reader, "images/creatures/willowisp/willowisp.sprite", LAYER_FLOATINGOBJECTS), mystate(STATE_IDLE), target_sector("main"), target_spawnpoint("main"), soundSource(0)
+  : BadGuy(reader, "images/creatures/willowisp/willowisp.sprite", LAYER_FLOATINGOBJECTS), mystate(STATE_IDLE), target_sector("main"), target_spawnpoint("main")
 {
   reader.get("sector", target_sector);
   reader.get("spawnpoint", target_spawnpoint);
 
   countMe = false;
-}
-
-WillOWisp::WillOWisp(const WillOWisp& other) 
-       : BadGuy(other), mystate(other.mystate), target_sector(other.target_sector), target_spawnpoint(other.target_spawnpoint)
-{
-  soundSource = sound_manager->create_sound_source("sounds/willowisp.wav");
-}
-
-WillOWisp::~WillOWisp()
-{
-  delete soundSource;
+  sound_manager->preload(SOUNDFILE);
 }
 
 void
@@ -64,12 +55,12 @@ void
 WillOWisp::draw(DrawingContext& context)
 {
   sprite->draw(context, get_pos(), layer);
-  
+
   context.push_target();
   context.set_target(DrawingContext::LIGHTMAP);
 
   sprite->draw(context, get_pos(), layer);
-  
+
   context.pop_target();
 }
 
@@ -87,7 +78,7 @@ WillOWisp::active_update(float elapsed_time)
       mystate = STATE_TRACKING;
     }
   }
-  
+
   if (mystate == STATE_TRACKING) {
     if (dist.norm() <= VANISH_RANGE) {
       Vector dir = dist.unit();
@@ -96,7 +87,7 @@ WillOWisp::active_update(float elapsed_time)
       mystate = STATE_VANISHING;
       sprite->set_action("vanishing", 1);
     }
-    soundSource->set_position(get_pos());
+    sound_source->set_position(get_pos());
   }
 
   if (mystate == STATE_WARPING) {
@@ -110,7 +101,7 @@ WillOWisp::active_update(float elapsed_time)
       remove_me();
     }
   }
-  
+
 }
 
 void
@@ -118,24 +109,30 @@ WillOWisp::activate()
 {
   sprite->set_action("idle");
 
-  delete soundSource;
-  soundSource = sound_manager->create_sound_source("sounds/willowisp.wav");
-  if(!soundSource) {
-    log_warning << "Couldn't start WillOWisp sound" << std::endl;
-    return;
-  }
-  soundSource->set_position(get_pos());
-  soundSource->set_looping(true);
-  soundSource->set_gain(2.0);
-  soundSource->set_reference_distance(32);
-  soundSource->play();
+  sound_source.reset(sound_manager->create_sound_source(SOUNDFILE));
+  sound_source->set_position(get_pos());
+  sound_source->set_looping(true);
+  sound_source->set_gain(2.0);
+  sound_source->set_reference_distance(32);
+  sound_source->play();
 }
 
 void
 WillOWisp::deactivate()
 {
-  delete soundSource;
-  soundSource = 0;
+  sound_source.reset(NULL);
+
+  switch (mystate) {
+    case STATE_IDLE:
+      break;
+    case STATE_TRACKING:
+      mystate = STATE_IDLE;
+      break;
+    case STATE_WARPING:
+    case STATE_VANISHING:
+      remove_me();
+      break;
+  }
 }
 
 void
@@ -159,4 +156,3 @@ WillOWisp::collision_player(Player& player, const CollisionHit& ) {
 }
 
 IMPLEMENT_FACTORY(WillOWisp, "willowisp")
-