Renamed namespaces to all lowercase
[supertux.git] / src / badguy / willowisp.cpp
index cc50d6b..7549593 100644 (file)
@@ -1,12 +1,10 @@
-//  $Id$
-//
 //  SuperTux - "Will-O-Wisp" Badguy
 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
 //
-//  This program is free software; you can redistribute it and/or
-//  modify it under the terms of the GNU General Public License
-//  as published by the Free Software Foundation; either version 2
-//  of the License, or (at your option) any later version.
+//  This program is free software: you can redistribute it and/or modify
+//  it under the terms of the GNU General Public License as published by
+//  the Free Software Foundation, either version 3 of the License, or
+//  (at your option) any later version.
 //
 //  This program is distributed in the hope that it will be useful,
 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
 //  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
-//  02111-1307, USA.
-#include <config.h>
-
-#include "willowisp.hpp"
-#include "log.hpp"
-#include "game_session.hpp"
+//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+#include "badguy/willowisp.hpp"
+
+#include "audio/sound_manager.hpp"
+#include "audio/sound_source.hpp"
 #include "object/lantern.hpp"
+#include "object/path_walker.hpp"
 #include "object/player.hpp"
 #include "scripting/squirrel_util.hpp"
+#include "sprite/sprite.hpp"
+#include "supertux/game_session.hpp"
+#include "supertux/object_factory.hpp"
+#include "supertux/sector.hpp"
+#include "util/reader.hpp"
 
 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")
+WillOWisp::WillOWisp(const Reader& reader) :
+  BadGuy(reader, "images/creatures/willowisp/willowisp.sprite", LAYER_FLOATINGOBJECTS), 
+  mystate(STATE_IDLE), 
+  target_sector("main"), 
+  target_spawnpoint("main"),
+  hit_script(),
+  sound_source(),
+  path(),
+  walker(),
+  flyspeed(),
+  track_range(),
+  vanish_range()
 {
   bool running = false;
   flyspeed     = FLYSPEED;
@@ -59,6 +71,9 @@ WillOWisp::WillOWisp(const lisp::Lisp& reader)
 
   countMe = false;
   sound_manager->preload(SOUNDFILE);
+  sound_manager->preload("sounds/warp.wav");
+
+  sprite->set_action("idle");
 }
 
 void
@@ -84,59 +99,60 @@ WillOWisp::active_update(float elapsed_time)
   Vector dist = (p2 - p1);
 
   switch(mystate) {
-  case STATE_STOPPED:
-    break;
+    case STATE_STOPPED:
+      break;
 
-  case STATE_IDLE:
-    if (dist.norm() <= track_range) {
-      mystate = STATE_TRACKING;
-    }
-    break;
+    case STATE_IDLE:
+      if (dist.norm() <= track_range) {
+        mystate = STATE_TRACKING;
+      }
+      break;
 
-  case STATE_TRACKING:
-    if (dist.norm() <= vanish_range) {
-      Vector dir = dist.unit();
-      movement = dir * elapsed_time * flyspeed;
-    } else {
-      vanish();
-    }
-    sound_source->set_position(get_pos());
-    break;
+    case STATE_TRACKING:
+      if (dist.norm() > vanish_range) {
+        vanish();
+      } else if (dist.norm() >= 1) {
+        Vector dir = dist.unit();
+        movement = dir * elapsed_time * flyspeed;
+      } else {
+        /* We somehow landed right on top of the player without colliding.
+         * Sit tight and avoid a division by zero. */
+      }
+      sound_source->set_position(get_pos());
+      break;
 
-  case STATE_WARPING:
-    if(sprite->animation_done()) {
-      remove_me();
-    }
+    case STATE_WARPING:
+      if(sprite->animation_done()) {
+        remove_me();
+      }
 
-  case STATE_VANISHING: {
-    Vector dir = dist.unit();
-    movement = dir * elapsed_time * flyspeed;
-    if(sprite->animation_done()) {
-      remove_me();
+    case STATE_VANISHING: {
+      Vector dir = dist.unit();
+      movement = dir * elapsed_time * flyspeed;
+      if(sprite->animation_done()) {
+        remove_me();
+      }
+      break;
     }
-    break;
-  }
 
-  case STATE_PATHMOVING:
-  case STATE_PATHMOVING_TRACK:
-    if(walker.get() == NULL)
-      return;
-    movement = walker->advance(elapsed_time) - get_pos();
-    if(mystate == STATE_PATHMOVING_TRACK && dist.norm() <= track_range) {
-      mystate = STATE_TRACKING;
-    }
-    break;
+    case STATE_PATHMOVING:
+    case STATE_PATHMOVING_TRACK:
+      if(walker.get() == NULL)
+        return;
+      movement = walker->advance(elapsed_time) - get_pos();
+      if(mystate == STATE_PATHMOVING_TRACK && dist.norm() <= track_range) {
+        mystate = STATE_TRACKING;
+      }
+      break;
 
-  default:
-    assert(false);
+    default:
+      assert(false);
   }
 }
 
 void
 WillOWisp::activate()
 {
-  sprite->set_action("idle");
-
   sound_source.reset(sound_manager->create_sound_source(SOUNDFILE));
   sound_source->set_position(get_pos());
   sound_source->set_looping(true);
@@ -171,7 +187,7 @@ WillOWisp::vanish()
 {
   mystate = STATE_VANISHING;
   sprite->set_action("vanishing", 1);
-  set_group(COLGROUP_DISABLED);
+  set_colgroup_active(COLGROUP_DISABLED);
 }
 
 bool
@@ -250,7 +266,7 @@ WillOWisp::set_state(const std::string& new_state)
   } else {
     std::ostringstream msg;
     msg << "Can't set unknown willowisp state '" << new_state << "', should "
-               "be stopped, move_path, move_path_track or normal";
+      "be stopped, move_path, move_path_track or normal";
     throw new std::runtime_error(msg.str());
   }
 }
@@ -262,7 +278,7 @@ WillOWisp::expose(HSQUIRRELVM vm, SQInteger table_idx)
     return;
 
   std::cout << "Expose me '" << name << "'\n";
-  Scripting::WillOWisp* interface = static_cast<Scripting::WillOWisp*> (this);
+  scripting::WillOWisp* interface = static_cast<scripting::WillOWisp*> (this);
   expose_object(vm, table_idx, interface, name);
 }
   
@@ -273,7 +289,9 @@ WillOWisp::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
     return;
 
   std::cout << "UnExpose me '" << name << "'\n";
-  Scripting::unexpose_object(vm, table_idx, name);
+  scripting::unexpose_object(vm, table_idx, name);
 }
 
-IMPLEMENT_FACTORY(WillOWisp, "willowisp")
+IMPLEMENT_FACTORY(WillOWisp, "willowisp");
+
+/* EOF */