Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / badguy / ghosttree.cpp
index 973afa8..cc8e492 100644 (file)
@@ -1,12 +1,10 @@
-//  $Id$
-//
 //  SuperTux - Boss "GhostTree"
 //  Copyright (C) 2007 Matthias Braun <matze@braunis.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>
+//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-#include "ghosttree.hpp"
-#include "treewillowisp.hpp"
-#include "sprite/sprite_manager.hpp"
-#include "root.hpp"
-#include "random_generator.hpp"
+#include "badguy/ghosttree.hpp"
+
+#include "audio/sound_manager.hpp"
+#include "badguy/root.hpp"
+#include "badguy/treewillowisp.hpp"
+#include "math/random_generator.hpp"
 #include "object/lantern.hpp"
+#include "object/player.hpp"
+#include "sprite/sprite.hpp"
+#include "sprite/sprite_manager.hpp"
+#include "supertux/object_factory.hpp"
+#include "supertux/sector.hpp"
+
+#include <algorithm>
+#include <math.h>
 
 static const size_t WILLOWISP_COUNT = 10;
 static const float ROOT_TOP_OFFSET = 64;
@@ -31,13 +36,27 @@ static const float WILLOWISP_TOP_OFFSET = -64;
 static const Vector SUCK_TARGET_OFFSET = Vector(-16,-16);
 static const float SUCK_TARGET_SPREAD = 8;
 
-GhostTree::GhostTree(const lisp::Lisp& lisp)
-  : BadGuy(lisp, "images/creatures/ghosttree/ghosttree.sprite",
-           LAYER_OBJECTS - 10), mystate(STATE_IDLE),
-    willo_spawn_y(0), willo_radius(200), willo_speed(1.8f), willo_color(0),
-    treecolor(0), suck_lantern(0)
+GhostTree::GhostTree(const Reader& lisp) :
+  BadGuy(lisp, "images/creatures/ghosttree/ghosttree.sprite", LAYER_OBJECTS - 10), 
+  mystate(STATE_IDLE),
+  willowisp_timer(),
+  willo_spawn_y(0),
+  willo_radius(200), 
+  willo_speed(1.8f), 
+  willo_color(0),
+  glow_sprite(),
+  colorchange_timer(),
+  suck_timer(),
+  root_timer(),
+  treecolor(0), 
+  suck_lantern_color(),
+  suck_lantern(0),
+  willowisps()
 {
-  glow_sprite.reset(sprite_manager->create("images/creatures/ghosttree/ghosttree-glow.sprite"));
+  glow_sprite = 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 +83,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 +98,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,58 +114,60 @@ 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);
+        }
       }
     }
 
     if(root_timer.check()) {
       /* TODO indicate root with an animation */
       Player* player = get_nearest_player();
-      Root* root = new Root(Vector(player->get_bbox().get_left(), get_bbox().get_bottom()+ROOT_TOP_OFFSET));
-      Sector::current()->add_object(root);
+      if (player) {
+        Root* root = new Root(Vector(player->get_bbox().get_left(), get_bbox().get_bottom()+ROOT_TOP_OFFSET));
+        Sector::current()->add_object(root);
+      }
     }
   } else if (mystate == STATE_SWALLOWING) {
     if (suck_lantern) {
@@ -251,3 +271,4 @@ GhostTree::spawn_lantern() {
 
 IMPLEMENT_FACTORY(GhostTree, "ghosttree");
 
+/* EOF */