Patch by Matt McCutchen to prevent division by zero when Tux spawns exactly at a...
[supertux.git] / src / badguy / ghosttree.cpp
index 973afa8..e349f6d 100644 (file)
 #include <config.h>
 
 #include "ghosttree.hpp"
+
 #include "treewillowisp.hpp"
 #include "sprite/sprite_manager.hpp"
+#include "sprite/sprite.hpp"
 #include "root.hpp"
 #include "random_generator.hpp"
 #include "object/lantern.hpp"
+#include "object_factory.hpp"
+#include "audio/sound_manager.hpp"
+#include "sector.hpp"
+#include "object/player.hpp"
+#include "video/drawing_context.hpp"
+
+#include <math.h>
+#include <algorithm>
 
 static const size_t WILLOWISP_COUNT = 10;
 static const float ROOT_TOP_OFFSET = 64;
@@ -38,6 +48,9 @@ GhostTree::GhostTree(const lisp::Lisp& lisp)
     treecolor(0), suck_lantern(0)
 {
   glow_sprite.reset(sprite_manager->create("images/creatures/ghosttree/ghosttree-glow.sprite"));
+  set_colgroup_active(COLGROUP_TOUCHABLE);
+  sound_manager->preload("sounds/tree_howling.ogg");
+  sound_manager->preload("sounds/tree_suck.ogg");
 }
 
 GhostTree::~GhostTree()
@@ -64,7 +77,6 @@ GhostTree::activate()
   willowisp_timer.start(1.0f, true);
   colorchange_timer.start(13, true);
   root_timer.start(5, true);
-  set_group(COLGROUP_TOUCHABLE);
 }
 
 void
@@ -80,13 +92,13 @@ GhostTree::active_update(float elapsed_time)
 
       Color col;
       switch(treecolor) {
-       case 0: col = Color(1, 0, 0); break;
-       case 1: col = Color(0, 1, 0); break;
-       case 2: col = Color(0, 0, 1); break;
-       case 3: col = Color(1, 1, 0); break;
-       case 4: col = Color(1, 0, 1); break;
-       case 5: col = Color(0, 1, 1); break;
-       default: assert(false);
+        case 0: col = Color(1, 0, 0); break;
+        case 1: col = Color(0, 1, 0); break;
+        case 2: col = Color(0, 0, 1); break;
+        case 3: col = Color(1, 1, 0); break;
+        case 4: col = Color(1, 0, 1); break;
+        case 5: col = Color(0, 1, 1); break;
+        default: assert(false);
       }
       glow_sprite->set_color(col);
     }
@@ -96,50 +108,50 @@ GhostTree::active_update(float elapsed_time)
       sound_manager->play("sounds/tree_suck.ogg", get_pos());
       std::vector<TreeWillOWisp*>::iterator iter;
       for(iter = willowisps.begin(); iter != willowisps.end(); ++iter) {
-       TreeWillOWisp *willo = *iter;
-       if(willo->get_color() == col) {
-         willo->start_sucking(get_bbox().get_middle() + SUCK_TARGET_OFFSET + Vector(systemRandom.randf(-SUCK_TARGET_SPREAD, SUCK_TARGET_SPREAD), systemRandom.randf(-SUCK_TARGET_SPREAD, SUCK_TARGET_SPREAD)));
-       }
+        TreeWillOWisp *willo = *iter;
+        if(willo->get_color() == col) {
+          willo->start_sucking(get_bbox().get_middle() + SUCK_TARGET_OFFSET + Vector(systemRandom.randf(-SUCK_TARGET_SPREAD, SUCK_TARGET_SPREAD), systemRandom.randf(-SUCK_TARGET_SPREAD, SUCK_TARGET_SPREAD)));
+        }
       }
       mystate = STATE_SUCKING;
     }
 
     if(willowisp_timer.check()) {
       if(willowisps.size() < WILLOWISP_COUNT) {
-       Vector pos = Vector(bbox.get_width() / 2, bbox.get_height() / 2 + willo_spawn_y + WILLOWISP_TOP_OFFSET);
-       TreeWillOWisp *willowisp 
-               = new TreeWillOWisp(this, pos, 200 + willo_radius, willo_speed);
-
-       Sector::current()->add_object(willowisp);
-       willowisps.push_back(willowisp);
-
-       willo_spawn_y -= 40;
-       if(willo_spawn_y < -160)
-         willo_spawn_y = 0;
-
-       willo_radius += 20;
-       if(willo_radius > 120)
-         willo_radius = 0;
-
-       if(willo_speed == 1.8f) {
-         willo_speed = 1.5f;
-       } else {
-         willo_speed = 1.8f;
-       }
-
-       do {
-         willo_color = (willo_color + 1) % 3;
-       } while(willo_color == treecolor);
-
-       switch(willo_color) {
-         case 0: willowisp->set_color(Color(1, 0, 0)); break;
-         case 1: willowisp->set_color(Color(0, 1, 0)); break;
-         case 2: willowisp->set_color(Color(0, 0, 1)); break;
-         case 3: willowisp->set_color(Color(1, 1, 0)); break;
-         case 4: willowisp->set_color(Color(1, 0, 1)); break;
-         case 5: willowisp->set_color(Color(0, 1, 1)); break;
-         default: assert(false);
-       }
+        Vector pos = Vector(bbox.get_width() / 2, bbox.get_height() / 2 + willo_spawn_y + WILLOWISP_TOP_OFFSET);
+        TreeWillOWisp *willowisp 
+            = new TreeWillOWisp(this, pos, 200 + willo_radius, willo_speed);
+
+        Sector::current()->add_object(willowisp);
+        willowisps.push_back(willowisp);
+
+        willo_spawn_y -= 40;
+        if(willo_spawn_y < -160)
+          willo_spawn_y = 0;
+
+        willo_radius += 20;
+        if(willo_radius > 120)
+          willo_radius = 0;
+
+        if(willo_speed == 1.8f) {
+          willo_speed = 1.5f;
+        } else {
+          willo_speed = 1.8f;
+        }
+
+        do {
+          willo_color = (willo_color + 1) % 3;
+        } while(willo_color == treecolor);
+
+        switch(willo_color) {
+          case 0: willowisp->set_color(Color(1, 0, 0)); break;
+          case 1: willowisp->set_color(Color(0, 1, 0)); break;
+          case 2: willowisp->set_color(Color(0, 0, 1)); break;
+          case 3: willowisp->set_color(Color(1, 1, 0)); break;
+          case 4: willowisp->set_color(Color(1, 0, 1)); break;
+          case 5: willowisp->set_color(Color(0, 1, 1)); break;
+          default: assert(false);
+        }
       }
     }